-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
46 lines (39 loc) · 933 Bytes
/
utils.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
package chirp
import (
crand "crypto/rand"
"encoding/hex"
"math/big"
"math/rand"
"strings"
)
func crandUint64n(n uint64) uint64 {
bigN := (&big.Int{}).SetUint64(n)
val, err := crand.Int(crand.Reader, bigN)
if err != nil {
// fall back to non-crypto rand
return uint64(rand.Int63n(int64(n)))
}
return val.Uint64()
}
var alphanumerics = strings.Split("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", "")
func randSenderID() string {
randData := make([]byte, 16)
_, err := crand.Read(randData)
if err != nil {
// fall back to non-crypto rand
_, err = rand.Read(randData)
if err != nil {
panic("unable to get any random data to create a sender id")
}
}
return hex.EncodeToString(randData)
}
func randAlphaNum(length int) string {
s := ""
numRunes := uint64(len(alphanumerics))
for i := 0; i < length; i++ {
idx := crandUint64n(numRunes)
s += alphanumerics[idx]
}
return s
}