March 2019
Beginner to intermediate
324 pages
7h 17m
English
In our database layer, we will define a new type for invalid password errors. In our dblayer.go file, we will add the following line:
var ErrINVALIDPASSWORD = errors.New("Invalid password")
Then, in our orm.go file, we'll add the following function:
func hashPassword(s *string) error { if s == nil { return errors.New("Reference provided for hashing password is nil") } //convert password string to byte slice so that we can use it with the bcrypt package sBytes := []byte(*s) //Obtain hashed password via the GenerateFromPassword() method hashedBytes, err := bcrypt.GenerateFromPassword(sBytes, bcrypt.DefaultCost) if err != nil { return err } //update password string with the hashed version *s = string(hashedBytes[:]) ...Read now
Unlock full access