https://pkg.go.dev/golang.org/x/crypto@v0.0.0-20200414173820-0848c9571904/bcrypt?tab=doc#pkg-variables
使用go标准库”golang.org/x/crypto/bcrypt”,快速轻松实现密码加密校验
import (
"golang.org/x/crypto/bcrypt"
)
const PassWordCost = 12
// SetPassword 设置密码
func SetPassword(password string) (encoderPwd string, err error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), PassWordCost)
return string(bytes), err
}
// CheckPassword 校验密码
func CheckPassword(dbPwd, password string) bool {
err := bcrypt.CompareHashAndPassword([]byte(dbPwd), []byte(password))
return err == nil
}