id.go
997 Bytes
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
42
43
44
45
46
47
48
49
50
51
52
53
package util
import (
"crypto/rand"
"encoding/binary"
"fmt"
mrand "math/rand"
)
func RandomSeed() (seed int64, err error) {
err = binary.Read(rand.Reader, binary.LittleEndian, &seed)
return
}
// creates a random identifier of the specified length
func RandId(idlen int) string {
b := make([]byte, idlen)
var randVal uint32
for i := 0; i < idlen; i++ {
byteIdx := i % 4
if byteIdx == 0 {
randVal = mrand.Uint32()
}
b[i] = byte((randVal >> (8 * uint(byteIdx))) & 0xFF)
}
return fmt.Sprintf("%x", b)
}
// like RandId, but uses a crypto/rand for secure random identifiers
func SecureRandId(idlen int) (id string, err error) {
b := make([]byte, idlen)
n, err := rand.Read(b)
if n != idlen {
err = fmt.Errorf("Only generated %d random bytes, %d requested", n, idlen)
return
}
if err != nil {
return
}
id = fmt.Sprintf("%x", b)
return
}
func SecureRandIdOrPanic(idlen int) string {
id, err := SecureRandId(idlen)
if err != nil {
panic(err)
}
return id
}