From bf5521059fcca839cdb60f51e7b34f75af099c1f Mon Sep 17 00:00:00 2001 From: hunter007 Date: Wed, 20 Sep 2023 15:06:06 +0800 Subject: [PATCH] Fix Signed-off-by: hunter007 --- .golangci.yml | 3 +++ generator.go | 52 +++++++++++++++++++++++++-------------------------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 0a5ccdd..915769b 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -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' diff --git a/generator.go b/generator.go index 6344713..1d04bf3 100644 --- a/generator.go +++ b/generator.go @@ -34,6 +34,10 @@ type Config struct { random RandomFunc } +func NewGenerator(c Config) Generator { + return newGenerator(c) +} + type generator struct { chars []rune upperLetters []rune @@ -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 { @@ -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) @@ -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)) +}