-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils-random.go
47 lines (34 loc) · 999 Bytes
/
utils-random.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
42
43
44
45
46
47
package main
import (
"bytes"
"encoding/binary"
"fmt"
"math/rand"
"time"
crypto_rand "crypto/rand"
)
func randomToken(length uint) string {
randGen := rand.New(rand.NewSource(randomSeed()))
bytes := make([]byte, length)
for i := range bytes {
bytes[i] = sessionRandLetters[randGen.Intn(len(sessionRandLetters))]
}
return string(bytes)
}
func randomSeed() int64 {
var randomBytes [8]byte
_, err := crypto_rand.Read(randomBytes[:])
if err == nil {
// Hopefully this is always the code path in production
// since session IDs could be guessed by nano times
var seed int64
err = binary.Read(bytes.NewBuffer(randomBytes[:]), binary.LittleEndian, &seed)
if err == nil {
return seed
}
logError(nil, nil, fmt.Errorf("reading session ID random generator seed with binary.Read: %w", err))
return time.Now().UnixNano()
}
logError(nil, nil, fmt.Errorf("reading session ID random generator seed bytes with crypto/rand: %w", err))
return time.Now().UnixNano()
}