mirror of
https://github.com/chaitin/MonkeyCode.git
synced 2026-11-22 19:39:31 +08:00
25 lines
587 B
Go
25 lines
587 B
Go
package crypto
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
// HashPassword 使用 bcrypt 加密密码
|
|
func HashPassword(password string) (string, error) {
|
|
if len(password) > 32 {
|
|
return "", errors.New("password must be less than 32 characters")
|
|
}
|
|
hashedBytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(hashedBytes), nil
|
|
}
|
|
|
|
// VerifyPassword 验证密码
|
|
func VerifyPassword(dbPassword, password string) error {
|
|
return bcrypt.CompareHashAndPassword([]byte(dbPassword), []byte(password))
|
|
}
|