Compare commits
2 Commits
c899c098f6
...
d95c7abea4
| Author | SHA1 | Date | |
|---|---|---|---|
| d95c7abea4 | |||
| 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
|
||||||
|
}
|
||||||
|
|||||||
@ -356,11 +356,19 @@ func (h *Handlers) ConfirmPinChange(ctx context.Context, sym string, input []byt
|
|||||||
res.FlagReset = append(res.FlagReset, flag_pin_mismatch)
|
res.FlagReset = append(res.FlagReset, flag_pin_mismatch)
|
||||||
} else {
|
} else {
|
||||||
res.FlagSet = append(res.FlagSet, flag_pin_mismatch)
|
res.FlagSet = append(res.FlagSet, flag_pin_mismatch)
|
||||||
|
return res, nil
|
||||||
}
|
}
|
||||||
// If matched, save the confirmed PIN as the new account PIN
|
|
||||||
err = store.WriteEntry(ctx, sessionId, common.DATA_ACCOUNT_PIN, []byte(temporaryPin))
|
// Hash the PIN
|
||||||
|
hashedPIN, err := common.HashPIN(string(temporaryPin))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logg.ErrorCtxf(ctx, "failed to write temporaryPin entry with", "key", common.DATA_ACCOUNT_PIN, "value", temporaryPin, "error", err)
|
logg.ErrorCtxf(ctx, "failed to hash temporaryPin", "error", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// save the hashed PIN as the new account PIN
|
||||||
|
err = store.WriteEntry(ctx, sessionId, common.DATA_ACCOUNT_PIN, []byte(hashedPIN))
|
||||||
|
if err != nil {
|
||||||
|
logg.ErrorCtxf(ctx, "failed to write DATA_ACCOUNT_PIN entry with", "key", common.DATA_ACCOUNT_PIN, "hashedPIN value", hashedPIN, "error", err)
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
return res, nil
|
return res, nil
|
||||||
@ -392,11 +400,18 @@ func (h *Handlers) VerifyCreatePin(ctx context.Context, sym string, input []byte
|
|||||||
res.FlagSet = append(res.FlagSet, flag_pin_set)
|
res.FlagSet = append(res.FlagSet, flag_pin_set)
|
||||||
} else {
|
} else {
|
||||||
res.FlagSet = []uint32{flag_pin_mismatch}
|
res.FlagSet = []uint32{flag_pin_mismatch}
|
||||||
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
err = store.WriteEntry(ctx, sessionId, common.DATA_ACCOUNT_PIN, []byte(temporaryPin))
|
// Hash the PIN
|
||||||
|
hashedPIN, err := common.HashPIN(string(temporaryPin))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logg.ErrorCtxf(ctx, "failed to write temporaryPin entry with", "key", common.DATA_ACCOUNT_PIN, "value", temporaryPin, "error", err)
|
logg.ErrorCtxf(ctx, "failed to hash temporaryPin", "error", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = store.WriteEntry(ctx, sessionId, common.DATA_ACCOUNT_PIN, []byte(hashedPIN))
|
||||||
|
if err != nil {
|
||||||
|
logg.ErrorCtxf(ctx, "failed to write DATA_ACCOUNT_PIN entry with", "key", common.DATA_ACCOUNT_PIN, "value", hashedPIN, "error", err)
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user