diff --git a/common/pin.go b/common/pin.go index 4d46f12..6db9d15 100644 --- a/common/pin.go +++ b/common/pin.go @@ -1,6 +1,10 @@ package common -import "regexp" +import ( + "regexp" + + "golang.org/x/crypto/bcrypt" +) // Define the regex pattern as a constant const ( @@ -12,3 +16,18 @@ func IsValidPIN(pin string) bool { match, _ := regexp.MatchString(pinPattern, pin) return match } + +// HashPIN uses bcrypt with 8 salt rounds to hash the PIN +func HashPIN(pin string) (string, error) { + hash, err := bcrypt.GenerateFromPassword([]byte(pin), 8) + if err != nil { + return "", err + } + return string(hash), nil +} + +// VerifyPIN compareS the hashed PIN with the plaintext PIN +func VerifyPIN(hashedPIN, pin string) bool { + err := bcrypt.CompareHashAndPassword([]byte(hashedPIN), []byte(pin)) + return err == nil +}