mirror of
https://github.com/wneessen/apg-go.git
synced 2024-11-25 07:10:50 +01:00
Refactor character counting logic in password validation
The repetitive code used for password character counting in different scenarios has been consolidated into a single function, matchesMinimumAmount(). This extraction simplifies the main function and makes the code more maintainable, while maintaining the functionality of checking whether the password meets certain character count criteria.
This commit is contained in:
parent
cc45ec1119
commit
4ee6b6651b
1 changed files with 17 additions and 28 deletions
45
random.go
45
random.go
|
@ -229,13 +229,7 @@ func (g *Generator) checkMinimumRequirements(password string) bool {
|
||||||
charRange = CharRangeAlphaLower
|
charRange = CharRangeAlphaLower
|
||||||
}
|
}
|
||||||
|
|
||||||
count := 0
|
matchesMinimumAmount(charRange, password, g.config.MinLowerCase, &ok)
|
||||||
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
|
||||||
|
@ -246,13 +240,7 @@ func (g *Generator) checkMinimumRequirements(password string) bool {
|
||||||
charRange = CharRangeNumeric
|
charRange = CharRangeNumeric
|
||||||
}
|
}
|
||||||
|
|
||||||
count := 0
|
matchesMinimumAmount(charRange, password, g.config.MinNumeric, &ok)
|
||||||
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
|
||||||
|
@ -263,13 +251,7 @@ func (g *Generator) checkMinimumRequirements(password string) bool {
|
||||||
charRange = CharRangeSpecial
|
charRange = CharRangeSpecial
|
||||||
}
|
}
|
||||||
|
|
||||||
count := 0
|
matchesMinimumAmount(charRange, password, g.config.MinSpecial, &ok)
|
||||||
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
|
||||||
|
@ -280,13 +262,7 @@ func (g *Generator) checkMinimumRequirements(password string) bool {
|
||||||
charRange = CharRangeAlphaUpper
|
charRange = CharRangeAlphaUpper
|
||||||
}
|
}
|
||||||
|
|
||||||
count := 0
|
matchesMinimumAmount(charRange, password, g.config.MinUpperCase, &ok)
|
||||||
for _, char := range charRange {
|
|
||||||
count += strings.Count(password, string(char))
|
|
||||||
}
|
|
||||||
if int64(count) < g.config.MinUpperCase {
|
|
||||||
ok = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
@ -359,3 +335,16 @@ 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue