feat: 优化web
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package codec
|
||||
|
||||
import "golang.org/x/crypto/bcrypt"
|
||||
|
||||
// HashPassword 将明文密码生成为 bcrypt 哈希字符串(与业务中 bcrypt.DefaultCost 一致)。
|
||||
func HashPassword(password string) (string, error) {
|
||||
b, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(b), nil
|
||||
}
|
||||
|
||||
// VerifyPassword 校验明文是否与 bcrypt 哈希匹配。
|
||||
// password 明文密码
|
||||
// hashedPassword 哈希密码
|
||||
func VerifyPassword(password,hashedPassword string) error {
|
||||
return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
|
||||
}
|
||||
Reference in New Issue
Block a user