goipam/utils/passwordMatch.go

30 lines
885 B
Go

package utils
import "golang.org/x/crypto/bcrypt"
// adapted from https://www.gregorygaines.com/blog/how-to-properly-hash-and-salt-passwords-in-golang-bcrypt/
// hashPassword Hash password using bcrypt and
func HashPassword(password string) (string, error) {
// Convert password string to byte slice
var passwordBytes = []byte(password)
// Hash password with Bcrypt's default cost
hashedPasswordBytes, err := bcrypt.
GenerateFromPassword(passwordBytes, bcrypt.DefaultCost)
return string(hashedPasswordBytes), err
}
// DoPasswordsMatch Check if two passwords match using Bcrypt's CompareHashAndPassword
// which return nil on success and an error on failure.
func DoPasswordsMatch(hashedPassword, currPassword string) bool {
err := bcrypt.CompareHashAndPassword(
[]byte(hashedPassword), []byte(currPassword))
if err == nil {
return true
} else {
return false
}
}