Skip to content

Commit c51bce1

Browse files
authored
Use crypto/rand instead of math/rand (#148)
1 parent 1f95008 commit c51bce1

1 file changed

Lines changed: 8 additions & 11 deletions

File tree

internal/oauth2/crypto.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
package oauth2
22

33
import (
4-
"math/rand"
5-
"time"
4+
"crypto/rand"
65
)
76

8-
var r *rand.Rand
9-
10-
func init() {
11-
r = rand.New(rand.NewSource(time.Now().UnixNano()))
12-
}
13-
14-
var letter = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
7+
var letters = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
158

169
func RandomString(n int) string {
17-
b := make([]rune, n)
10+
b := make([]byte, n)
11+
12+
if _, err := rand.Read(b); err != nil {
13+
panic(err)
14+
}
1815

1916
for i := range b {
20-
b[i] = letter[r.Intn(len(letter))]
17+
b[i] = letters[b[i]%byte(len(letters))]
2118
}
2219

2320
return string(b)

0 commit comments

Comments
 (0)