Skip to content

Commit 4f55647

Browse files
authored
Added some hash validation (#875)
1 parent 39aa2e3 commit 4f55647

File tree

4 files changed

+449
-0
lines changed

4 files changed

+449
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,16 @@ Baked-in Validations
190190
| uuid5 | Universally Unique Identifier UUID v5 |
191191
| uuid5_rfc4122 | Universally Unique Identifier UUID v5 RFC4122 |
192192
| uuid_rfc4122 | Universally Unique Identifier UUID RFC4122 |
193+
| md4 | MD4 hash |
194+
| md5 | MD5 hash |
195+
| sha256 | SHA256 hash |
196+
| sha384 | SHA384 hash |
197+
| sha512 | SHA512 hash |
198+
| ripemd128 | RIPEMD-128 hash |
199+
| ripemd128 | RIPEMD-160 hash |
200+
| tiger128 | TIGER128 hash |
201+
| tiger160 | TIGER160 hash |
202+
| tiger192 | TIGER192 hash |
193203
| semver | Semantic Versioning 2.0.0 |
194204
| ulid | Universally Unique Lexicographically Sortable Identifier ULID |
195205

baked_in.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ var (
151151
"uuid4_rfc4122": isUUID4RFC4122,
152152
"uuid5_rfc4122": isUUID5RFC4122,
153153
"ulid": isULID,
154+
"md4": isMD4,
155+
"md5": isMD5,
156+
"sha256": isSHA256,
157+
"sha384": isSHA384,
158+
"sha512": isSHA512,
159+
"ripemd128": isRIPEMD128,
160+
"ripemd160": isRIPEMD160,
161+
"tiger128": isTIGER128,
162+
"tiger160": isTIGER160,
163+
"tiger192": isTIGER192,
154164
"ascii": isASCII,
155165
"printascii": isPrintableASCII,
156166
"multibyte": hasMultiByteCharacter,
@@ -500,6 +510,56 @@ func isULID(fl FieldLevel) bool {
500510
return uLIDRegex.MatchString(fl.Field().String())
501511
}
502512

513+
// isMD4 is the validation function for validating if the field's value is a valid MD4.
514+
func isMD4(fl FieldLevel) bool {
515+
return md4Regex.MatchString(fl.Field().String())
516+
}
517+
518+
// isMD5 is the validation function for validating if the field's value is a valid MD5.
519+
func isMD5(fl FieldLevel) bool {
520+
return md5Regex.MatchString(fl.Field().String())
521+
}
522+
523+
// isSHA256 is the validation function for validating if the field's value is a valid SHA256.
524+
func isSHA256(fl FieldLevel) bool {
525+
return sha256Regex.MatchString(fl.Field().String())
526+
}
527+
528+
// isSHA384 is the validation function for validating if the field's value is a valid SHA384.
529+
func isSHA384(fl FieldLevel) bool {
530+
return sha384Regex.MatchString(fl.Field().String())
531+
}
532+
533+
// isSHA512 is the validation function for validating if the field's value is a valid SHA512.
534+
func isSHA512(fl FieldLevel) bool {
535+
return sha512Regex.MatchString(fl.Field().String())
536+
}
537+
538+
// isRIPEMD128 is the validation function for validating if the field's value is a valid PIPEMD128.
539+
func isRIPEMD128(fl FieldLevel) bool {
540+
return ripemd128Regex.MatchString(fl.Field().String())
541+
}
542+
543+
// isRIPEMD160 is the validation function for validating if the field's value is a valid PIPEMD160.
544+
func isRIPEMD160(fl FieldLevel) bool {
545+
return ripemd160Regex.MatchString(fl.Field().String())
546+
}
547+
548+
// isTIGER128 is the validation function for validating if the field's value is a valid TIGER128.
549+
func isTIGER128(fl FieldLevel) bool {
550+
return tiger128Regex.MatchString(fl.Field().String())
551+
}
552+
553+
// isTIGER160 is the validation function for validating if the field's value is a valid TIGER160.
554+
func isTIGER160(fl FieldLevel) bool {
555+
return tiger160Regex.MatchString(fl.Field().String())
556+
}
557+
558+
// isTIGER192 is the validation function for validating if the field's value is a valid isTIGER192.
559+
func isTIGER192(fl FieldLevel) bool {
560+
return tiger192Regex.MatchString(fl.Field().String())
561+
}
562+
503563
// isISBN is the validation function for validating if the field's value is a valid v10 or v13 ISBN.
504564
func isISBN(fl FieldLevel) bool {
505565
return isISBN10(fl) || isISBN13(fl)

regexes.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ const (
3030
uUID5RFC4122RegexString = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-5[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$"
3131
uUIDRFC4122RegexString = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
3232
uLIDRegexString = "^[A-HJKMNP-TV-Z0-9]{26}$"
33+
md4RegexString = "^[0-9a-f]{32}$"
34+
md5RegexString = "^[0-9a-f]{32}$"
35+
sha256RegexString = "^[0-9a-f]{64}$"
36+
sha384RegexString = "^[0-9a-f]{96}$"
37+
sha512RegexString = "^[0-9a-f]{128}$"
38+
ripemd128RegexString = "^[0-9a-f]{32}$"
39+
ripemd160RegexString = "^[0-9a-f]{40}$"
40+
tiger128RegexString = "^[0-9a-f]{32}$"
41+
tiger160RegexString = "^[0-9a-f]{40}$"
42+
tiger192RegexString = "^[0-9a-f]{48}$"
3343
aSCIIRegexString = "^[\x00-\x7F]*$"
3444
printableASCIIRegexString = "^[\x20-\x7E]*$"
3545
multibyteRegexString = "[^\x00-\x7F]"
@@ -84,6 +94,16 @@ var (
8494
uUID5RFC4122Regex = regexp.MustCompile(uUID5RFC4122RegexString)
8595
uUIDRFC4122Regex = regexp.MustCompile(uUIDRFC4122RegexString)
8696
uLIDRegex = regexp.MustCompile(uLIDRegexString)
97+
md4Regex = regexp.MustCompile(md4RegexString)
98+
md5Regex = regexp.MustCompile(md5RegexString)
99+
sha256Regex = regexp.MustCompile(sha256RegexString)
100+
sha384Regex = regexp.MustCompile(sha384RegexString)
101+
sha512Regex = regexp.MustCompile(sha512RegexString)
102+
ripemd128Regex = regexp.MustCompile(ripemd128RegexString)
103+
ripemd160Regex = regexp.MustCompile(ripemd160RegexString)
104+
tiger128Regex = regexp.MustCompile(tiger128RegexString)
105+
tiger160Regex = regexp.MustCompile(tiger160RegexString)
106+
tiger192Regex = regexp.MustCompile(tiger192RegexString)
87107
aSCIIRegex = regexp.MustCompile(aSCIIRegexString)
88108
printableASCIIRegex = regexp.MustCompile(printableASCIIRegexString)
89109
multibyteRegex = regexp.MustCompile(multibyteRegexString)

0 commit comments

Comments
 (0)