18. Security
Activity 18.01: Authenticating Users on the Application Using Hashed Passwords
Solution:
- Create a
main.go
file and import the following packages:crypto/sha512
: This package will provide the hashing required to encrypt the password.database/sql
: The database to store user details will be created using this package.github.com/mattn/go-sqlite3
: This is a third-party library used to create asqlite
instance for testing.package main import ( "crypto/sha512" "database/sql" "fmt" "os" _ "github.com/mattn/go-sqlite3" )
- Define a function called
getConnection()
to initialize a database connection:func getConnection() (*sql.DB, error) { conn, err := sql.Open("sqlite3", "test.DB") if err != nil { return nil, fmt.Errorf("could not open db connection %v", err) } ...