-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
init password Generator/Sequencer with rules
- Loading branch information
Showing
9 changed files
with
302 additions
and
72 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
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,60 @@ | ||
package password | ||
|
||
// Rule controls how the Generator/Sequencer generates passwords. | ||
type Rule func(any) | ||
|
||
var ( | ||
defaultRules = []Rule{ | ||
WithCharset(AlphaNumeric), | ||
WithLength(8), | ||
} | ||
) | ||
|
||
// WithCharset sets the Charset the Generator/Sequencer can use. | ||
func WithCharset(c Charset) Rule { | ||
return func(a any) { | ||
switch v := a.(type) { | ||
case *generator: | ||
v.charset = []rune(c) | ||
case *sequencer: | ||
v.charset = []rune(c) | ||
} | ||
} | ||
} | ||
|
||
// WithLength sets the length of the generated password. | ||
func WithLength(l int) Rule { | ||
return func(a any) { | ||
switch v := a.(type) { | ||
case *generator: | ||
v.numChars = l | ||
case *sequencer: | ||
v.numChars = l | ||
} | ||
} | ||
} | ||
|
||
// WithNumSymbols controls the min/max number of symbols that can appear in the | ||
// password. | ||
// | ||
// Note: This works only on a Generator and is ineffective with a Sequencer. | ||
func WithNumSymbols(min, max int) Rule { | ||
// sanitize min and max | ||
if min < 0 { | ||
min = 0 | ||
} | ||
if max < 0 { | ||
max = 0 | ||
} | ||
if min > max { | ||
min = max | ||
} | ||
|
||
return func(a any) { | ||
switch v := a.(type) { | ||
case *generator: | ||
v.minSymbols = min | ||
v.maxSymbols = max | ||
} | ||
} | ||
} |
Oops, something went wrong.