-
Notifications
You must be signed in to change notification settings - Fork 2
/
randstring.go
50 lines (40 loc) · 1.04 KB
/
randstring.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
48
49
50
package randstring
import (
"crypto/rand"
"encoding/hex"
"io"
"math/big"
)
/*
Use mixed-case alphanumeric characters, minus vowels so we don't
get naughty words. This leaves us with 10+21+21=52 possibilities
per character, or 5.7 bits (-log(1/52, 2)) of information.
Thus a random string of length 15 gives us 85 bits of information
*/
const chars = "0123456789bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ"
func randFromString(n int, charSet string) (string, error) {
max := big.NewInt(int64(len(charSet)))
bytes := make([]byte, n)
for i := range bytes {
j, err := rand.Int(rand.Reader, max)
if err != nil {
return "", err
}
bytes[i] = charSet[int(j.Int64())]
}
return string(bytes), nil
}
func AlphaNum(n int) (string, error) {
return randFromString(n, chars)
}
func Numeric(n int) (string, error) {
return randFromString(n, chars[0:10])
}
func Hex(n int) (string, error) {
bytes := make([]byte, n)
_, err := io.ReadFull(rand.Reader, bytes)
if err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
}