forked from urdt/ussd
add code to Hash and Verify the PIN
This commit is contained in:
parent
c899c098f6
commit
fd1ac85a1b
@ -1,6 +1,10 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import "regexp"
|
import (
|
||||||
|
"regexp"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
)
|
||||||
|
|
||||||
// Define the regex pattern as a constant
|
// Define the regex pattern as a constant
|
||||||
const (
|
const (
|
||||||
@ -12,3 +16,18 @@ func IsValidPIN(pin string) bool {
|
|||||||
match, _ := regexp.MatchString(pinPattern, pin)
|
match, _ := regexp.MatchString(pinPattern, pin)
|
||||||
return match
|
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
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user