Post

Go password encryption

Go password encryption

Go password encryption

密码加密

1
2
3
4
5
6
import "golang.org/x/crypto/bcrypt"

func HashPassword(pwd string) (string, error) {
	hash, err := bcrypt.GenerateFromPassword([]byte(pwd), 12)
	return string(hash), err
}

密码校验

1
2
3
4
func CheckPassword(hash, password string) bool {
	err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
	return err == nil
}
This post is licensed under CC BY 4.0 by the author.