https://pkg.go.dev/golang.org/x/crypto@v0.0.0-20200414173820-0848c9571904/bcrypt?tab=doc#pkg-variables
    使用go标准库”golang.org/x/crypto/bcrypt”,快速轻松实现密码加密校验

    1. import (
    2. "golang.org/x/crypto/bcrypt"
    3. )
    4. const PassWordCost = 12
    5. // SetPassword 设置密码
    6. func SetPassword(password string) (encoderPwd string, err error) {
    7. bytes, err := bcrypt.GenerateFromPassword([]byte(password), PassWordCost)
    8. return string(bytes), err
    9. }
    10. // CheckPassword 校验密码
    11. func CheckPassword(dbPwd, password string) bool {
    12. err := bcrypt.CompareHashAndPassword([]byte(dbPwd), []byte(password))
    13. return err == nil
    14. }