-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Email, ip, ipv4, ipv6, md5, sha1, sha256 helpers (#36)
* Add hostname helper * email helper * Examples * ipv4 * IP * Check hashes * Example_ipv4Match
- Loading branch information
Showing
12 changed files
with
897 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -334,3 +334,64 @@ func Example_compositeInReadme() { | |
// 123: true | ||
// hello.123: false | ||
} | ||
|
||
func Example_emailMatch() { | ||
re := rex.New( | ||
rex.Chars.Begin(), | ||
rex.Helper.Email(), | ||
rex.Chars.End(), | ||
).MustCompile() | ||
|
||
fmt.Println("regular expression:", re.String()) | ||
fmt.Println("[email protected]:", re.MatchString("[email protected]")) | ||
fmt.Println("@example.com:", re.MatchString("@example.com")) | ||
|
||
// Output: | ||
// regular expression: ^(?:(?:[[:alnum:]!#\$\x25&'\*\+\x2D/=\?\^_`\{\|\}~](?:[[:alnum:]!#\$\x25&'\*\+\x2D/=\?\^_`\{\|\}~\.]?[[:alnum:]!#\$\x25&'\*\+\x2D/=\?\^_`\{\|\}~]){0,31})@(?:[[:alnum:]][[:alnum:]\x2D]{0,62}(?:\.*[[:alnum:]][[:alnum:]\x2D]{0,62})*[[:alnum:]]))$ | ||
// [email protected]: true | ||
// @example.com: false | ||
} | ||
|
||
func Example_emailFind() { | ||
re := rex.New( | ||
rex.Group.Define( | ||
rex.Helper.Email(), | ||
).WithName("email"), | ||
).MustCompile() | ||
|
||
const text = ` | ||
Duyen: [email protected] | ||
Rex: [email protected] | ||
` | ||
|
||
fmt.Println("regular expression:", re.String()) | ||
submatches := re.FindAllStringSubmatch(text, -1) | ||
|
||
for i, sub := range submatches { | ||
fmt.Printf("submatches[%d]: %s\n", i, sub[0]) | ||
} | ||
|
||
// Output: | ||
// regular expression: (?P<email>(?:(?:[[:alnum:]!#\$\x25&'\*\+\x2D/=\?\^_`\{\|\}~](?:[[:alnum:]!#\$\x25&'\*\+\x2D/=\?\^_`\{\|\}~\.]?[[:alnum:]!#\$\x25&'\*\+\x2D/=\?\^_`\{\|\}~]){0,31})@(?:[[:alnum:]][[:alnum:]\x2D]{0,62}(?:\.*[[:alnum:]][[:alnum:]\x2D]{0,62})*[[:alnum:]]))) | ||
// submatches[0]: [email protected] | ||
// submatches[1]: [email protected] | ||
} | ||
|
||
func Example_ipv4Match() { | ||
re := rex.New( | ||
rex.Chars.Begin(), | ||
rex.Helper.IPv4(), | ||
rex.Chars.End(), | ||
).MustCompile() | ||
|
||
fmt.Println("regular expression:", re.String()) | ||
fmt.Println("127.0.0.1:", re.MatchString("127.0.0.1")) | ||
fmt.Println("172.217.16.14:", re.MatchString("172.217.16.14")) | ||
fmt.Println("github.com:", re.MatchString("github.com")) | ||
|
||
// Output: | ||
// regular expression: ^(?:(?:(?:(?:25[0-5])|(?:2[0-4]\d)|(?:[01]?\d\d?))\.){3}(?:(?:25[0-5])|(?:2[0-4]\d)|(?:[01]?\d\d?)))$ | ||
// 127.0.0.1: true | ||
// 172.217.16.14: true | ||
// github.com: false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package base | ||
|
||
import "github.com/hedhyw/rex/pkg/dialect" | ||
|
||
// MD5Hex is a pattern for a cryptographic hash function MD5 in hex representation. | ||
// | ||
// Example: d41d8cd98f00b204e9800998ecf8427e. | ||
func (h HelperDialect) MD5Hex() dialect.Token { | ||
return h.hex(32) | ||
} | ||
|
||
// SHA1Hex is a pattern for a cryptographic hash function SHA1 in hex representation. | ||
// | ||
// Example: da39a3ee5e6b4b0d3255bfef95601890afd80709. | ||
func (h HelperDialect) SHA1Hex() dialect.Token { | ||
return h.hex(40) | ||
} | ||
|
||
// MD5 is a pattern for a cryptographic hash function SHA256 in hex representation. | ||
// | ||
// Example: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855. | ||
func (h HelperDialect) SHA256Hex() dialect.Token { | ||
return h.hex(64) | ||
} | ||
|
||
func (HelperDialect) hex(length int) dialect.Token { | ||
return Group.Define( | ||
Chars.HexDigits().Repeat().Exactly(length), | ||
).NonCaptured() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// nolint: gosec // It is a test. | ||
package base_test | ||
|
||
import ( | ||
"crypto/md5" | ||
"crypto/sha1" | ||
"crypto/sha256" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hedhyw/rex/internal/test" | ||
"github.com/hedhyw/rex/pkg/dialect/base" | ||
) | ||
|
||
func getMD5ValidTestCases() test.MatchTestCaseSlice { | ||
return test.MatchTestCaseSlice{{ | ||
Name: "md5_ok_example", | ||
Value: "d41d8cd98f00b204e9800998ecf8427e", | ||
}, { | ||
Name: "md5_ok_upper", | ||
Value: "D41D8CD98F00B204E9800998ECF8427E", | ||
}, { | ||
Name: "md5_ok_3", | ||
Value: fmt.Sprintf("%x", md5.Sum([]byte("3"))), | ||
}, { | ||
Name: "md5_ok_4", | ||
Value: fmt.Sprintf("%x", md5.Sum([]byte("4"))), | ||
}, { | ||
Name: "md5_ok_5", | ||
Value: fmt.Sprintf("%x", md5.Sum([]byte("5"))), | ||
}, { | ||
Name: "md5_ok_6", | ||
Value: fmt.Sprintf("%x", md5.Sum([]byte("6"))), | ||
}} | ||
} | ||
|
||
func getMD5InvalidTestCases() test.MatchTestCaseSlice { | ||
return test.MatchTestCaseSlice{{ | ||
Name: "md5_non_hex_p", | ||
Value: "p41d8cd98f00b204e9800998ecf8427e", | ||
}, { | ||
Name: "md5_short", | ||
Value: fmt.Sprintf("%x", md5.Sum([]byte("short")))[0:31], | ||
}, { | ||
Name: "md5_long", | ||
Value: fmt.Sprintf("%x", md5.Sum([]byte("long"))) + "0", | ||
}, { | ||
Name: "md5_empty", | ||
Value: "", | ||
}} | ||
} | ||
|
||
func TestMD5Hex(t *testing.T) { | ||
test.MatchTestCaseGroupSlice{ | ||
getMD5ValidTestCases().WithMatched(true), | ||
getMD5InvalidTestCases().WithMatched(false), | ||
getSHA1ValidTestCases().WithMatched(false), | ||
getSHA256ValidTestCases().WithMatched(false), | ||
}.Run(t, base.Helper.MD5Hex()) | ||
} | ||
|
||
func getSHA1ValidTestCases() test.MatchTestCaseSlice { | ||
return test.MatchTestCaseSlice{{ | ||
Name: "sha1_ok_example", | ||
Value: "da39a3ee5e6b4b0d3255bfef95601890afd80709", | ||
}, { | ||
Name: "sha1_ok_upper", | ||
Value: "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709", | ||
}, { | ||
Name: "sha1_ok_3", | ||
Value: fmt.Sprintf("%x", sha1.Sum([]byte("3"))), | ||
}, { | ||
Name: "sha1_ok_4", | ||
Value: fmt.Sprintf("%x", sha1.Sum([]byte("4"))), | ||
}, { | ||
Name: "sha1_ok_5", | ||
Value: fmt.Sprintf("%x", sha1.Sum([]byte("5"))), | ||
}, { | ||
Name: "sha1_ok_6", | ||
Value: fmt.Sprintf("%x", sha1.Sum([]byte("6"))), | ||
}} | ||
} | ||
|
||
func getSHA1InvalidTestCases() test.MatchTestCaseSlice { | ||
return test.MatchTestCaseSlice{{ | ||
Name: "sha1_non_hex_p", | ||
Value: "pa39a3ee5e6b4b0d3255bfef95601890afd80709", | ||
}, { | ||
Name: "sha1_short", | ||
Value: fmt.Sprintf("%x", sha1.Sum([]byte("short")))[0:39], | ||
}, { | ||
Name: "sha1_long", | ||
Value: fmt.Sprintf("%x", sha1.Sum([]byte("long"))) + "0", | ||
}, { | ||
Name: "sha1_empty", | ||
Value: "", | ||
}} | ||
} | ||
|
||
func TestSHA1Hex(t *testing.T) { | ||
test.MatchTestCaseGroupSlice{ | ||
getSHA1ValidTestCases().WithMatched(true), | ||
getSHA1InvalidTestCases().WithMatched(false), | ||
getMD5ValidTestCases().WithMatched(false), | ||
getSHA256ValidTestCases().WithMatched(false), | ||
}.Run(t, base.Helper.SHA1Hex()) | ||
} | ||
|
||
func getSHA256ValidTestCases() test.MatchTestCaseSlice { | ||
return test.MatchTestCaseSlice{{ | ||
Name: "sha256_ok_example", | ||
Value: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", | ||
}, { | ||
Name: "sha256_ok_upper", | ||
Value: "E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855", | ||
}, { | ||
Name: "sha256_ok_3", | ||
Value: fmt.Sprintf("%x", sha256.Sum256([]byte("3"))), | ||
}, { | ||
Name: "sha256_ok_4", | ||
Value: fmt.Sprintf("%x", sha256.Sum256([]byte("4"))), | ||
}, { | ||
Name: "sha256_ok_5", | ||
Value: fmt.Sprintf("%x", sha256.Sum256([]byte("5"))), | ||
}, { | ||
Name: "sha256_ok_6", | ||
Value: fmt.Sprintf("%x", sha256.Sum256([]byte("6"))), | ||
}} | ||
} | ||
|
||
func getSHA256InvalidTestCases() test.MatchTestCaseSlice { | ||
return test.MatchTestCaseSlice{{ | ||
Name: "sha256_non_hex_p", | ||
Value: "p3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", | ||
}, { | ||
Name: "sha256_short", | ||
Value: fmt.Sprintf("%x", sha256.Sum256([]byte("short")))[0:63], | ||
}, { | ||
Name: "sha256_long", | ||
Value: fmt.Sprintf("%x", sha256.Sum256([]byte("long"))) + "0", | ||
}, { | ||
Name: "sha256_empty", | ||
Value: "", | ||
}} | ||
} | ||
|
||
func TestSHA256Hex(t *testing.T) { | ||
test.MatchTestCaseGroupSlice{ | ||
getSHA256ValidTestCases().WithMatched(true), | ||
getSHA256InvalidTestCases().WithMatched(false), | ||
getSHA1ValidTestCases().WithMatched(false), | ||
getMD5ValidTestCases().WithMatched(false), | ||
}.Run(t, base.Helper.SHA256Hex()) | ||
} |
Oops, something went wrong.