-
Notifications
You must be signed in to change notification settings - Fork 6
/
random.go
201 lines (157 loc) · 4.41 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// The random package provides cryptographically secure random integers
// and strings.
package random
import (
"crypto/rand"
"encoding/base64"
"encoding/binary"
"errors"
"math"
)
// Uint64Range returns a random 64-bit unsigned integer in the range [start, end].
// An error is returned if start is greater than end.
func Uint64Range(start, end uint64) (uint64, error) {
var val uint64
var err error
if start >= end {
return val, errors.New("Start value must be less than end value.")
}
// Get uniformly distributed numbers in the range 0 to size. Using the
// arc4random_uniform algorithm.
// https://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/lib/libc/crypt/arc4random_uniform.c
size := end - start // Get range size
min := (math.MaxUint64 - size) % size
for {
val, err = Uint64()
if err != nil {
return val, err
}
if val > min {
break
}
}
val = val % size
// End arc4random_uniform
// Add start to val to shift numbers to correct range.
return val + start, nil
}
// Chars returns a random string of length n, which consists of the given
// character set. If the charset is empty or n is less than or equal to zero
// then an empty string is returned.
func Chars(charset string, n uint64) (string, error) {
if n == 0 {
return "", errors.New("Requested string length cannot be 0.")
}
if len(charset) == 0 {
return "", errors.New("Charset cannot be empty.")
}
length := uint64(len(charset))
b := make([]byte, n)
for i := range b {
j, err := Uint64Range(0, length)
if err != nil {
return "", err
}
b[i] = charset[j]
}
return string(b), nil
}
// Alpha returns a string of length n, which consists of random upper case and
// lowercase characters. If n is less than or equal to zero then an empty
// string is returned
func Alpha(n uint64) (string, error) {
charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
return Chars(charset, n)
}
// AlphaNum returns a string of length n, which consists of random uppercase,
// lowercase, and numeric characters. If n is zero then an empty string is
// returned.
func AlphaNum(n uint64) (string, error) {
charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
return Chars(charset, n)
}
// Token returns a string suitable for cryptographic tokens such as session ids.
func Token() (string, error) {
var bytes [32]byte
_, err := rand.Read(bytes[:])
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(bytes[:]), nil
}
// Uint8 returns a random 8-bit unsigned integer. Return 0 and an error if
// unable to get random data.
func Uint8() (uint8, error) {
var bytes [1]byte
_, err := rand.Read(bytes[:])
if err != nil {
return uint8(0), err
}
return bytes[0], nil
}
// Int8 returns a random 8-bit signed integer. Return 0 and an error if
// unable to get random data.
func Int8() (int8, error) {
i, err := Uint8()
if err != nil {
return int8(0), err
}
return int8(i), nil
}
// Uint16 returns a random 16-bit unsigned integer. Return 0 and an error if
// unable to get random data.
func Uint16() (uint16, error) {
var bytes [2]byte
_, err := rand.Read(bytes[:])
if err != nil {
return uint16(0), err
}
return binary.LittleEndian.Uint16(bytes[:]), nil
}
// Int16 returns a random 16-bit signed integer. Return 0 and an error if
// unable to get random data.
func Int16() (int16, error) {
i, err := Uint16()
if err != nil {
return int16(0), err
}
return int16(i), nil
}
// Uint32 returns a random 32-bit unsigned integer. Return 0 and an error if
// unable to get random data.
func Uint32() (uint32, error) {
var bytes [4]byte
_, err := rand.Read(bytes[:])
if err != nil {
return uint32(0), err
}
return binary.LittleEndian.Uint32(bytes[:]), nil
}
// Int32 returns a random 32-bit signed integer. Return 0 and an error if
// unable to get random data.
func Int32() (int32, error) {
i, err := Uint32()
if err != nil {
return int32(0), err
}
return int32(i), nil
}
// Uint64 returns a random 64-bit unsigned integer. Return 0 and an error if
// unable to get random data.
func Uint64() (uint64, error) {
var bytes [8]byte
_, err := rand.Read(bytes[:])
if err != nil {
return uint64(0), err
}
return binary.LittleEndian.Uint64(bytes[:]), nil
}
// Int64 returns a random 64-bit signed integer. Return 0 and an error if
// unable to get random data.
func Int64() (int64, error) {
i, err := Uint64()
if err != nil {
return int64(0), err
}
return int64(i), nil
}