golib/gauth/helper.go
2023-06-15 21:22:51 +08:00

26 lines
547 B
Go

//
// helper.go
// Copyright (C) 2022 tiglog <me@tiglog.com>
//
// Distributed under terms of the MIT license.
//
package gauth
import "golang.org/x/crypto/bcrypt"
// 加密密码
func EncryptPassword(password string) (string, error) {
bt, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return "", err
}
return string(bt), nil
}
// 检查密码
func CheckPassword(pwd_plain, pwd_hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(pwd_hash), []byte(pwd_plain))
return err == nil
}