Skip to content

Commit

Permalink
Added Bcrypt validator
Browse files Browse the repository at this point in the history
  • Loading branch information
pixel365 committed Nov 10, 2023
1 parent 84254ae commit 6ad2d37
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ validate := validator.New(validator.WithRequiredStructEnabled())
| sha256 | SHA256 hash |
| sha384 | SHA384 hash |
| sha512 | SHA512 hash |
| bcrypt | Bcrypt hash |
| ripemd128 | RIPEMD-128 hash |
| ripemd128 | RIPEMD-160 hash |
| tiger128 | TIGER128 hash |
Expand Down
7 changes: 7 additions & 0 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ var (
"mongodb": isMongoDB,
"cron": isCron,
"spicedb": isSpiceDB,
"bcrypt": isBcrypt,
}
)

Expand Down Expand Up @@ -2953,3 +2954,9 @@ func isCron(fl FieldLevel) bool {
cronString := fl.Field().String()
return cronRegex.MatchString(cronString)
}

// isBcrypt is the validation function for validating if the field's value is a valid Bcrypt hash.
func isBcrypt(fl FieldLevel) bool {
bcryptString := fl.Field().String()
return bcryptRegex.MatchString(bcryptString)
}
6 changes: 6 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,12 @@ This validates that a string is a valid 24 character hexadecimal string.
Usage: mongodb
# Bcrypt
Usage: bcrypt
This validates that a string value is a valid Bcrypt hash.
# Cron
This validates that a string value contains a valid cron expression.
Expand Down
2 changes: 2 additions & 0 deletions regexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const (
spicedbIDRegexString = `^(([a-zA-Z0-9/_|\-=+]{1,})|\*)$`
spicedbPermissionRegexString = "^([a-z][a-z0-9_]{1,62}[a-z0-9])?$"
spicedbTypeRegexString = "^([a-z][a-z0-9_]{1,61}[a-z0-9]/)?[a-z][a-z0-9_]{1,62}[a-z0-9]$"
bcryptRegexString = "^\\$2(?:a|b|x|y)\\$(?:[4-9]|[12][0-9]|3[01])\\$(?:[a-zA-Z\\d\\./]{22})(?:[a-zA-Z\\d\\./]{31})$"
)

var (
Expand Down Expand Up @@ -142,4 +143,5 @@ var (
spicedbIDRegex = regexp.MustCompile(spicedbIDRegexString)
spicedbPermissionRegex = regexp.MustCompile(spicedbPermissionRegexString)
spicedbTypeRegex = regexp.MustCompile(spicedbTypeRegexString)
bcryptRegex = regexp.MustCompile(bcryptRegexString)
)
40 changes: 40 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13692,3 +13692,43 @@ func TestOmitNilAndRequired(t *testing.T) {
AssertError(t, err2, "OmitNil.Inner.Str", "OmitNil.Inner.Str", "Str", "Str", "required")
})
}

func TestBcryptValidation(t *testing.T) {
tests := []struct {
param string
expected bool
}{
{"", false},
{" ", false},
{"3994f547ee74552dc417428905a77e47", false},
{"$1$12$LIBGmKZIpw3HVbZwRMIb1O/epX7BaKbW0d", false},
{"$2w$12$LIBGmKZIpw3HVbZwRMIb1O/epX7BaKbW0dG2Kwu20RXKVmS0mN8u", false},
{"$2aa$12$LIBGmKZIpw3HVbZwRMIb1O/epX7BaKbW0dG2Kwu20RXKVmS0mN8ue", false},
{"$2a$12$LIBGmKZIpw3HVbZwRMIb1O/epX7BaKbW0dG2Kwu20RXKVmS0mN8ue", true},
{"$2a$12$feratNX4Cr/Tn2VEZZHyb.JWLRwtork3nF1AL4Mlirwc3mOvZwsmi", true},
{"$2b$12$feratNX4Cr/Tn2VEZZHyb.JWLRwtork3nF1AL4Mlirwc3mOvZwsmi", true},
{"$2x$12$feratNX4Cr/Tn2VEZZHyb.JWLRwtork3nF1AL4Mlirwc3mOvZwsmi", true},
{"$2y$12$feratNX4Cr/Tn2VEZZHyb.JWLRwtork3nF1AL4Mlirwc3mOvZwsmi", true},
}

validate := New()

for i, test := range tests {

errs := validate.Var(test.param, "bcrypt")
if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d Bcrypt failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d Bcrypt failed Error: %s", i, errs)
} else {
val := getError(errs, "", "")
if val.Tag() != "bcrypt" {
t.Fatalf("Index: %d Bcrypt failed Error: %s", i, errs)
}
}
}
}
}

0 comments on commit 6ad2d37

Please sign in to comment.