From bf11728f7d7dc4d53f1df16905f02f25d9ee25ce Mon Sep 17 00:00:00 2001 From: Saeed Rasooli Date: Sun, 17 Mar 2024 19:50:44 +0330 Subject: [PATCH] add support for \d and \w --- README.md | 4 ++++ lib/charclass_names.go | 5 +++++ lib/generate_test.go | 27 +++++++++++++++++++++++++++ lib/lex.go | 4 ++++ 4 files changed, 40 insertions(+) diff --git a/README.md b/README.md index 2fc521c..d58dc6a 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,10 @@ Then `repassgen` binary file will be created in current directory. - [x] `[:ascii:]` ASCII characters - [x] [Unicode code points](https://www.regular-expressions.info/unicode.html), like `[\u00e0-\u00ef]{5}` - [x] Group references `\1`, `\2`, etc +- [x] `\d` Digits +- [x] `\w` Word characters: alphanumeric and underline, same as `[a-zA-Z0-9_]` + + # Aditional Features (not part of regexp) - [x] Combined multiple named/manual character classes, for example: diff --git a/lib/charclass_names.go b/lib/charclass_names.go index 04741d5..84a9193 100644 --- a/lib/charclass_names.go +++ b/lib/charclass_names.go @@ -1,5 +1,10 @@ package passgen +// \w == [a-zA-Z0-9_] +var wordChars = []rune( + `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_`, +) + var charClasses = map[string][]rune{ // POSIX character classes, https://www.regular-expressions.info/posixbrackets.html "alpha": []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), diff --git a/lib/generate_test.go b/lib/generate_test.go index 1e676ef..f4840d9 100644 --- a/lib/generate_test.go +++ b/lib/generate_test.go @@ -777,6 +777,33 @@ func TestGenerate(t *testing.T) { return true }, }) + test(&genCase{ + Pattern: `\d{4}`, + PassLen: [2]int{4, 4}, + Entropy: [2]float64{13.2, 13.3}, + Validate: func(p string) bool { + for _, c := range p { + if c < '0' || c > '9' { + return false + } + } + return true + }, + }) + const wordChars = `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_` + test(&genCase{ + Pattern: `\w{4}`, + PassLen: [2]int{4, 4}, + Entropy: [2]float64{23.9, 23.91}, + Validate: func(p string) bool { + for _, c := range p { + if !strings.ContainsRune(wordChars, c) { + return false + } + } + return true + }, + }) testErr(&genErrCase{ Pattern: `$base64(gh)`, Error: ` ^ value error: invalid hex number "gh"`, diff --git a/lib/lex.go b/lib/lex.go index fdf2f6e..e3c0781 100644 --- a/lib/lex.go +++ b/lib/lex.go @@ -92,6 +92,10 @@ func lexBackslash(s *State) (LexType, error) { return nil, s.errorUnknown("incomplete buffer: %s", string(s.buffer)) } return lexRootUnicodeWide, nil + case 'd': + return processRange(s, []rune("0123456789")) + case 'w': + return processRange(s, wordChars) } err := s.addOutputOne(backslashEscape(c)) if err != nil {