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) Â Â } Â Â ...