mirror of
https://github.com/wneessen/apg-go.git
synced 2024-11-22 22:00:49 +01:00
Compare commits
No commits in common. "3b1ce9009aaaa9b0d5b10c38dfd11e0e0d262217" and "ddae28d1a975c3ba97dd2269d021510d57f4d918" have entirely different histories.
3b1ce9009a
...
ddae28d1a9
3 changed files with 55 additions and 78 deletions
|
@ -65,8 +65,26 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Old style character modes
|
// Old style character modes
|
||||||
configOldStyle(config, humanReadable, lowerCase, upperCase, numeric,
|
if humanReadable {
|
||||||
special, complexPass)
|
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeHumanReadable)
|
||||||
|
}
|
||||||
|
if lowerCase {
|
||||||
|
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeLowerCase)
|
||||||
|
}
|
||||||
|
if upperCase {
|
||||||
|
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeUpperCase)
|
||||||
|
}
|
||||||
|
if numeric {
|
||||||
|
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeNumeric)
|
||||||
|
}
|
||||||
|
if special {
|
||||||
|
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeSpecial)
|
||||||
|
}
|
||||||
|
if complexPass {
|
||||||
|
config.Mode = apg.MaskSetMode(config.Mode, apg.ModeLowerCase|apg.ModeNumeric|
|
||||||
|
apg.ModeSpecial|apg.ModeUpperCase)
|
||||||
|
config.Mode = apg.MaskClearMode(config.Mode, apg.ModeHumanReadable)
|
||||||
|
}
|
||||||
|
|
||||||
// New style character modes (has higher priority than the old style modes)
|
// New style character modes (has higher priority than the old style modes)
|
||||||
if modeString != "" {
|
if modeString != "" {
|
||||||
|
@ -75,21 +93,6 @@ func main() {
|
||||||
|
|
||||||
// For the "minimum amount of" modes we need to imply at the type
|
// For the "minimum amount of" modes we need to imply at the type
|
||||||
// of character mode is set
|
// of character mode is set
|
||||||
configMinRequirement(config)
|
|
||||||
|
|
||||||
// Check if algorithm is supported
|
|
||||||
config.Algorithm = apg.IntToAlgo(algorithm)
|
|
||||||
if config.Algorithm == apg.AlgoUnsupported {
|
|
||||||
_, _ = fmt.Fprintf(os.Stderr, "unsupported algorithm value: %d\n", algorithm)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate the password based on the given flags and print it to stdout
|
|
||||||
generate(config)
|
|
||||||
}
|
|
||||||
|
|
||||||
// configMinRequirement configures the "minimum amount" feature
|
|
||||||
func configMinRequirement(config *apg.Config) {
|
|
||||||
if config.MinLowerCase > 0 {
|
if config.MinLowerCase > 0 {
|
||||||
if float64(config.MinLength)/2 < float64(config.MinNumeric) {
|
if float64(config.MinLength)/2 < float64(config.MinNumeric) {
|
||||||
_, _ = os.Stderr.WriteString(MinimumAmountTooHigh)
|
_, _ = os.Stderr.WriteString(MinimumAmountTooHigh)
|
||||||
|
@ -114,35 +117,15 @@ func configMinRequirement(config *apg.Config) {
|
||||||
}
|
}
|
||||||
config.Mode = apg.MaskSetMode(config.Mode, apg.ModeUpperCase)
|
config.Mode = apg.MaskSetMode(config.Mode, apg.ModeUpperCase)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if algorithm is supported
|
||||||
|
config.Algorithm = apg.IntToAlgo(algorithm)
|
||||||
|
if config.Algorithm == apg.AlgoUnsupported {
|
||||||
|
_, _ = fmt.Fprintf(os.Stderr, "unsupported algorithm value: %d\n", algorithm)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// configOldStyle configures the old style character modes
|
// Generate the password based on the given flags
|
||||||
func configOldStyle(config *apg.Config, humanReadable, lowerCase, upperCase,
|
|
||||||
numeric, special, complexPass bool,
|
|
||||||
) {
|
|
||||||
if humanReadable {
|
|
||||||
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeHumanReadable)
|
|
||||||
}
|
|
||||||
if lowerCase {
|
|
||||||
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeLowerCase)
|
|
||||||
}
|
|
||||||
if upperCase {
|
|
||||||
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeUpperCase)
|
|
||||||
}
|
|
||||||
if numeric {
|
|
||||||
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeNumeric)
|
|
||||||
}
|
|
||||||
if special {
|
|
||||||
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeSpecial)
|
|
||||||
}
|
|
||||||
if complexPass {
|
|
||||||
config.Mode = apg.MaskSetMode(config.Mode, apg.ModeLowerCase|apg.ModeNumeric|
|
|
||||||
apg.ModeSpecial|apg.ModeUpperCase)
|
|
||||||
config.Mode = apg.MaskClearMode(config.Mode, apg.ModeHumanReadable)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func generate(config *apg.Config) {
|
|
||||||
generator := apg.New(config)
|
generator := apg.New(config)
|
||||||
for i := int64(0); i < config.NumberPass; i++ {
|
for i := int64(0); i < config.NumberPass; i++ {
|
||||||
password, err := generator.Generate()
|
password, err := generator.Generate()
|
||||||
|
|
45
random.go
45
random.go
|
@ -229,7 +229,13 @@ func (g *Generator) checkMinimumRequirements(password string) bool {
|
||||||
charRange = CharRangeAlphaLower
|
charRange = CharRangeAlphaLower
|
||||||
}
|
}
|
||||||
|
|
||||||
matchesMinimumAmount(charRange, password, g.config.MinLowerCase, &ok)
|
count := 0
|
||||||
|
for _, char := range charRange {
|
||||||
|
count += strings.Count(password, string(char))
|
||||||
|
}
|
||||||
|
if int64(count) < g.config.MinLowerCase {
|
||||||
|
ok = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if g.config.MinNumeric > 0 {
|
if g.config.MinNumeric > 0 {
|
||||||
var charRange string
|
var charRange string
|
||||||
|
@ -240,7 +246,13 @@ func (g *Generator) checkMinimumRequirements(password string) bool {
|
||||||
charRange = CharRangeNumeric
|
charRange = CharRangeNumeric
|
||||||
}
|
}
|
||||||
|
|
||||||
matchesMinimumAmount(charRange, password, g.config.MinNumeric, &ok)
|
count := 0
|
||||||
|
for _, char := range charRange {
|
||||||
|
count += strings.Count(password, string(char))
|
||||||
|
}
|
||||||
|
if int64(count) < g.config.MinNumeric {
|
||||||
|
ok = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if g.config.MinSpecial > 0 {
|
if g.config.MinSpecial > 0 {
|
||||||
var charRange string
|
var charRange string
|
||||||
|
@ -251,7 +263,13 @@ func (g *Generator) checkMinimumRequirements(password string) bool {
|
||||||
charRange = CharRangeSpecial
|
charRange = CharRangeSpecial
|
||||||
}
|
}
|
||||||
|
|
||||||
matchesMinimumAmount(charRange, password, g.config.MinSpecial, &ok)
|
count := 0
|
||||||
|
for _, char := range charRange {
|
||||||
|
count += strings.Count(password, string(char))
|
||||||
|
}
|
||||||
|
if int64(count) < g.config.MinSpecial {
|
||||||
|
ok = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if g.config.MinUpperCase > 0 {
|
if g.config.MinUpperCase > 0 {
|
||||||
var charRange string
|
var charRange string
|
||||||
|
@ -262,7 +280,13 @@ func (g *Generator) checkMinimumRequirements(password string) bool {
|
||||||
charRange = CharRangeAlphaUpper
|
charRange = CharRangeAlphaUpper
|
||||||
}
|
}
|
||||||
|
|
||||||
matchesMinimumAmount(charRange, password, g.config.MinUpperCase, &ok)
|
count := 0
|
||||||
|
for _, char := range charRange {
|
||||||
|
count += strings.Count(password, string(char))
|
||||||
|
}
|
||||||
|
if int64(count) < g.config.MinUpperCase {
|
||||||
|
ok = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
@ -335,16 +359,3 @@ func (g *Generator) generateRandom() (string, error) {
|
||||||
|
|
||||||
return password, nil
|
return password, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// matchesMinimumAmount checks if the number of occurrences of characters in
|
|
||||||
// charRange in the password is less than minAmount and updates the
|
|
||||||
// value of ok accordingly.
|
|
||||||
func matchesMinimumAmount(charRange, password string, minAmount int64, ok *bool) {
|
|
||||||
count := 0
|
|
||||||
for _, char := range charRange {
|
|
||||||
count += strings.Count(password, string(char))
|
|
||||||
}
|
|
||||||
if int64(count) < minAmount {
|
|
||||||
*ok = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -98,7 +98,7 @@ func TestGenerator_RandomBytes(t *testing.T) {
|
||||||
}
|
}
|
||||||
bl := len(rb)
|
bl := len(rb)
|
||||||
if int64(bl) != tc.l {
|
if int64(bl) != tc.l {
|
||||||
t.Errorf("length of provided bytes does not match requested length: got: %d, expected: %d",
|
t.Errorf("lenght of provided bytes does not match requested length: got: %d, expected: %d",
|
||||||
bl, tc.l)
|
bl, tc.l)
|
||||||
}
|
}
|
||||||
if bytes.Equal(rb, make([]byte, tc.l)) {
|
if bytes.Equal(rb, make([]byte, tc.l)) {
|
||||||
|
@ -220,23 +220,6 @@ func TestGetCharRangeFromConfig(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetCharRangeFromConfig_ExcludeChar(t *testing.T) {
|
|
||||||
defaultConf := NewConfig()
|
|
||||||
defaultGen := New(defaultConf)
|
|
||||||
defaultRange := defaultGen.GetCharRangeFromConfig()
|
|
||||||
defaultRange = strings.ReplaceAll(defaultRange, "a", "")
|
|
||||||
defaultRange = strings.ReplaceAll(defaultRange, "b", "")
|
|
||||||
|
|
||||||
config := NewConfig(WithExcludeChars("ab"))
|
|
||||||
generator := New(config)
|
|
||||||
excludeRange := generator.GetCharRangeFromConfig()
|
|
||||||
|
|
||||||
if excludeRange != defaultRange {
|
|
||||||
t.Errorf("GetCharRangeFromConfig(WithExcludeChars()) failed. Expected"+
|
|
||||||
"char range: %s, got: %s", defaultRange, excludeRange)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetPasswordLength(t *testing.T) {
|
func TestGetPasswordLength(t *testing.T) {
|
||||||
config := NewConfig()
|
config := NewConfig()
|
||||||
generator := New(config)
|
generator := New(config)
|
||||||
|
|
Loading…
Reference in a new issue