Files
Agent-System/internal/auth/crypto.go
T

21 lines
536 B
Go

package auth
import (
"golang.org/x/crypto/bcrypt"
)
// HashPassword 使用 bcrypt 对明文密码进行安全加盐哈希
func HashPassword(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return "", err
}
return string(bytes), nil
}
// CheckPassword 校验明文密码与 bcrypt 哈希是否匹配
func CheckPassword(hashed, password string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hashed), []byte(password))
return err == nil
}