add code to Hash and Verify the PIN

This commit is contained in:
Alfred Kamanda 2025-01-02 13:43:38 +03:00
parent c899c098f6
commit fd1ac85a1b
Signed by untrusted user: Alfred-mk
GPG Key ID: 7EA3D01708908703

View File

@ -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
}