Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
Signed-off-by: hunter007 <[email protected]>
  • Loading branch information
hunter007 committed Sep 20, 2023
1 parent 86aee8b commit bf55210
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ linters-settings:
- NOTE
- OPTIMIZE # marks code that should be optimized before merging
- HACK # marks hack-arounds that should be removed before merging
gosec:
excludes:
- G404
godot:
exclude:
- 'nosec'
Expand Down
52 changes: 25 additions & 27 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ type Config struct {
random RandomFunc
}

func NewGenerator(c Config) Generator {
return newGenerator(c)
}

type generator struct {
chars []rune
upperLetters []rune
Expand All @@ -43,51 +47,35 @@ type generator struct {
random RandomFunc
}

type RandomFunc func() int

// CryptoRandom implemented by crypto/rand
func CryptoRandom() int {
n, _ := rand.Int(rand.Reader, big.NewInt(10000))
return int(n.Int64())
}

// MathRandom implemented by math/rand
func MathRandom() int {
r := mrand.New(mrand.NewSource(time.Now().UnixNano()))
return int(r.Int63n(10000))
}

func (g *generator) Generate(length, minDigitLength, minSymbolLength, minUpperLetter uint) (string, error) {
if err := check(length, minDigitLength, minSymbolLength, minUpperLetter); err != nil {
return "", err
}

pasword := make([]rune, 0, length)
password := make([]rune, 0, length)
for i, d := 0, len(g.digits); i < int(minDigitLength); i++ {
pasword = append(pasword, g.digits[g.random()%d])
password = append(password, g.digits[g.random()%d])
}

for i, s := 0, len(g.symbols); i < int(minSymbolLength); i++ {
pasword = append(pasword, g.symbols[g.random()%s])
password = append(password, g.symbols[g.random()%s])
}

for i, u := 0, len(g.upperLetters); i < int(minUpperLetter); i++ {
pasword = append(pasword, g.upperLetters[g.random()%u])
password = append(password, g.upperLetters[g.random()%u])
}

remain := int(length) - int(minDigitLength) - int(minSymbolLength) - int(minUpperLetter)
for i, t := 0, len(g.chars); i < remain; i++ {
pasword = append(pasword, g.chars[g.random()%t])
password = append(password, g.chars[g.random()%t])
}

for i := 0; i < int(length/2); i++ {
j := g.random() % int(length)
t := pasword[j]
pasword[j] = pasword[i]
pasword[i] = t
password[j], password[i] = password[i], password[j]
}

return string(pasword), nil
return string(password), nil
}

func (g *generator) MustGenerate(length, minDigitLength, minSymbolLength, minUpperLetter uint) string {
Expand All @@ -98,10 +86,6 @@ func (g *generator) MustGenerate(length, minDigitLength, minSymbolLength, minUpp
return s
}

func NewGenerator(c Config) Generator {
return newGenerator(c)
}

func newGenerator(c Config) Generator {
lLen := utf8.RuneCountInString(c.LowerLetters)
uLen := utf8.RuneCountInString(c.UpperLetters)
Expand Down Expand Up @@ -180,3 +164,17 @@ func check(length, minDigitLength, minSymbolLength, minUpperLetter uint) error {
}
return nil
}

type RandomFunc func() int

// CryptoRandom implemented by crypto/rand
func CryptoRandom() int {
n, _ := rand.Int(rand.Reader, big.NewInt(10000))
return int(n.Int64())
}

// MathRandom implemented by math/rand
func MathRandom() int {
r := mrand.New(mrand.NewSource(time.Now().UnixNano()))
return int(r.Int63n(10000))
}

0 comments on commit bf55210

Please sign in to comment.