-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathkeys.go
41 lines (32 loc) · 1.03 KB
/
keys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package signature
import (
"math/rand"
"time"
)
// RandomKeyCharacters is a []byte of the characters to choose from when generating
// random keys.
var RandomKeyCharacters []byte = []byte("abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
// randomised gets whether the rand.Seed has been set or not.
var randomised bool
// RandomKey generates a random key at the given length.
//
// The first time this is called, the rand.Seed will be set
// to the current time.
func RandomKey(length int) string {
// randomise the seed
if !randomised {
rand.Seed(time.Now().UTC().UnixNano())
randomised = true
}
// Credit: http://stackoverflow.com/questions/12321133/golang-random-number-generator-how-to-seed-properly
bytes := make([]byte, length)
for i := 0; i < length; i++ {
randInt := randInt(0, len(RandomKeyCharacters))
bytes[i] = RandomKeyCharacters[randInt]
}
return string(bytes)
}
// randInt generates a random integer between min and max.
func randInt(min int, max int) int {
return min + rand.Intn(max-min)
}