@@ -22,7 +22,7 @@ package common
2222import (
2323 "crypto/sha256"
2424 "encoding/binary"
25- mathrand "math/rand"
25+ "math/rand"
2626 "strings"
2727 "time"
2828 "unicode"
@@ -33,24 +33,24 @@ import (
3333)
3434
3535const (
36- // DefaultSymbols is the list of default symbols to generate password.
37- DefaultSymbols = "!@#&*"
36+ // defaultSymbols is the list of default symbols to generate password.
37+ defaultSymbols = "!@#&*"
3838)
3939
40- type PasswordReader struct {
41- rand * mathrand .Rand
40+ type passwordReader struct {
41+ rand * rand .Rand
4242}
4343
44- func (r * PasswordReader ) Read (data []byte ) (int , error ) {
44+ func (r * passwordReader ) Read (data []byte ) (int , error ) {
4545 return r .rand .Read (data )
4646}
4747
48- func (r * PasswordReader ) Seed (seed int64 ) {
48+ func (r * passwordReader ) Seed (seed int64 ) {
4949 r .rand .Seed (seed )
5050}
5151
5252func GeneratePasswordByConfig (config appsv1.PasswordConfig ) (string , error ) {
53- passwd , err := GeneratePassword ((int )(config .Length ), (int )(config .NumDigits ), (int )(config .NumSymbols ), config .Seed , config .SymbolCharacters )
53+ passwd , err := generatePassword ((int )(config .Length ), (int )(config .NumDigits ), (int )(config .NumSymbols ), config .Seed , config .SymbolCharacters )
5454 if err != nil {
5555 return "" , err
5656 }
@@ -60,38 +60,38 @@ func GeneratePasswordByConfig(config appsv1.PasswordConfig) (string, error) {
6060 case appsv1 .LowerCases :
6161 passwd = strings .ToLower (passwd )
6262 case appsv1 .MixedCases :
63- passwd , err = EnsureMixedCase (passwd , config .Seed )
63+ passwd , err = ensureMixedCase (passwd , config .Seed )
6464 }
6565 return passwd , err
6666}
6767
68- // GeneratePassword generates a password with the given requirements and seed in lowercase.
69- func GeneratePassword (length , numDigits , numSymbols int , seed string , symbols string ) (string , error ) {
70- rand , err := newRngFromSeed (seed )
68+ // generatePassword generates a password with the given requirements and seed in lowercase.
69+ func generatePassword (length , numDigits , numSymbols int , seed string , symbols string ) (string , error ) {
70+ // #nosec G404
71+ rand , err := newRandFromSeed (seed )
7172 if err != nil {
7273 return "" , err
7374 }
74- passwordReader := & PasswordReader {rand : rand }
7575 if symbols == "" {
76- symbols = DefaultSymbols
76+ symbols = defaultSymbols
7777 }
7878 gen , err := password .NewGenerator (& password.GeneratorInput {
7979 LowerLetters : password .LowerLetters ,
8080 UpperLetters : password .UpperLetters ,
8181 Symbols : symbols ,
8282 Digits : password .Digits ,
83- Reader : passwordReader ,
83+ Reader : & passwordReader { rand : rand } ,
8484 })
8585 if err != nil {
8686 return "" , err
8787 }
8888 return gen .Generate (length , numDigits , numSymbols , true , true )
8989}
9090
91- // EnsureMixedCase randomizes the letter casing in the given string, ensuring
91+ // ensureMixedCase randomizes the letter casing in the given string, ensuring
9292// that the result contains at least one uppercase and one lowercase letter.
9393// If the give string only has one letter, it is returned unmodified.
94- func EnsureMixedCase (in , seed string ) (string , error ) {
94+ func ensureMixedCase (in , seed string ) (string , error ) {
9595 runes := []rune (in )
9696 letterIndices := make ([]int , 0 , len (runes ))
9797 for i , r := range runes {
@@ -107,11 +107,11 @@ func EnsureMixedCase(in, seed string) (string, error) {
107107
108108 // Get a random number x in [1, 2^L - 2], whose binary list will be used to determine the letter casing.
109109 // avoid the all-0 and all-1 patterns, which cause all-lowercase and all-uppercase password.
110- rng , err := newRngFromSeed (seed )
110+ rand , err := newRandFromSeed (seed )
111111 if err != nil {
112112 return in , err
113113 }
114- x := uint64 (rng .Int63n (int64 ((1 << L )- 2 ))) + 1
114+ x := uint64 (rand .Int63n (int64 ((1 << L )- 2 ))) + 1
115115
116116 for i := 0 ; i < L ; i ++ {
117117 bit := (x >> i ) & 1
@@ -125,11 +125,9 @@ func EnsureMixedCase(in, seed string) (string, error) {
125125 return string (runes ), nil
126126}
127127
128- // newRngFromSeed initializes a *mathrand.Rand from the given seed. If seed is empty,
129- // it uses the current time, making the output non-deterministic.
130- func newRngFromSeed (seed string ) (* mathrand.Rand , error ) {
128+ func newRandFromSeed (seed string ) (* rand.Rand , error ) {
131129 if seed == "" {
132- return mathrand .New (mathrand .NewSource (time .Now ().UnixNano ())), nil
130+ return rand .New (rand .NewSource (time .Now ().UnixNano ())), nil
133131 }
134132 // Convert seed string to a 64-bit integer via SHA-256
135133 h := sha256 .New ()
@@ -138,5 +136,5 @@ func newRngFromSeed(seed string) (*mathrand.Rand, error) {
138136 }
139137 sum := h .Sum (nil )
140138 uSeed := binary .BigEndian .Uint64 (sum )
141- return mathrand .New (mathrand .NewSource (int64 (uSeed ))), nil
139+ return rand .New (rand .NewSource (int64 (uSeed ))), nil
142140}
0 commit comments