one-api/common/crypto.go
seven.yu 91260bff65 refactor: common optimizations
1. rename GetOrDefault to GetOrDefaultInt
2. refactor method with variadic parameter declaration.
2024-01-10 20:34:07 +08:00

15 lines
418 B
Go

package common
import "golang.org/x/crypto/bcrypt"
func Password2Hash(password string) (string, error) {
passwordBytes := []byte(password)
hashedPassword, err := bcrypt.GenerateFromPassword(passwordBytes, bcrypt.DefaultCost)
return string(hashedPassword), err
}
func ValidatePasswordAndHash(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}