This commit is contained in:
wwweww
2026-02-23 20:36:21 +08:00
parent 4898aecd3b
commit fdbcde13b2
52 changed files with 11263 additions and 194 deletions
+25
View File
@@ -0,0 +1,25 @@
package utils
import (
"golang.org/x/crypto/bcrypt"
)
const (
// bcrypt 密钥成本
bcryptCost = bcrypt.DefaultCost
)
// HashPassword 对密码进行哈希加密
func HashPassword(password string) (string, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcryptCost)
if err != nil {
return "", err
}
return string(hash), nil
}
// VerifyPassword 验证密码是否正确
func VerifyPassword(hashedPassword, password string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
return err == nil
}