-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrand.go
More file actions
221 lines (188 loc) · 5.59 KB
/
rand.go
File metadata and controls
221 lines (188 loc) · 5.59 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package goutil
import (
"bytes"
"crypto/rand"
"crypto/sha1"
"crypto/sha512"
"encoding/base64"
"math"
"strconv"
"time"
"github.com/tkdeng/regex"
)
// RandBytes generates random bytes using crypto/rand
//
// @exclude[0] allows you can to pass an optional []byte to ensure that set of chars
// will not be included in the output string
//
// @exclude[1] provides a replacement string to put in place of the unwanted chars
//
// @exclude[2:] is currently ignored
func RandBytes(size uint, exclude ...[]byte) []byte {
b := make([]byte, size)
rand.Read(b)
b = []byte(base64.URLEncoding.EncodeToString(b))
if len(exclude) >= 2 {
if exclude[0] == nil || len(exclude[0]) == 0 {
b = regex.Comp(`[^\w_-]`).RepLit(b, exclude[1])
} else {
b = regex.Comp(`[`+regex.Escape(string(exclude[0]))+`]`).RepLit(b, exclude[1])
}
} else if len(exclude) >= 1 {
if exclude[0] == nil || len(exclude[0]) == 0 {
b = regex.Comp(`[^\w_-]`).RepLit(b, []byte{})
} else {
b = regex.Comp(`[`+regex.Escape(string(exclude[0]))+`]`).RepLit(b, []byte{})
}
}
for uint(len(b)) < size {
a := make([]byte, size)
rand.Read(a)
a = []byte(base64.URLEncoding.EncodeToString(a))
if len(exclude) >= 2 {
if exclude[0] == nil || len(exclude[0]) == 0 {
a = regex.Comp(`[^\w_-]`).RepLit(a, exclude[1])
} else {
a = regex.Comp(`[`+regex.Escape(string(exclude[0]))+`]`).RepLit(a, exclude[1])
}
} else if len(exclude) >= 1 {
if exclude[0] == nil || len(exclude[0]) == 0 {
a = regex.Comp(`[^\w_-]`).RepLit(a, []byte{})
} else {
a = regex.Comp(`[`+regex.Escape(string(exclude[0]))+`]`).RepLit(a, []byte{})
}
}
b = append(b, a...)
}
return b[:size]
}
// URandBytes tries to generate a unique random bytes
//
// This method uses the current microsecond and crypto random bytes to generate unique keys.
// This method also only returns alphanumeric characters [A-Za-z0-9]
//
// @unique (optional): add a list pointer, to keep track of what keys were already used.
// This method will automattically append new keys to the list.
// If the same key is generated twice, the function will try again (using recursion).
func URandBytes(size uint, unique ...*[][]byte) []byte {
if size < 8 {
size = 8
}
var b []byte
if size < 12 {
b = []byte(strconv.FormatUint(uint64(time.Now().UnixMicro())/100000000000, 36))
} else {
b = []byte(strconv.FormatUint(uint64(time.Now().UnixMicro())/100000000, 36))
}
for uint(len(b)) < size {
a := make([]byte, size)
rand.Read(a)
a = bytes.TrimRight([]byte(base64.URLEncoding.EncodeToString(a)), "=")
b = append(b, a...)
}
b = bytes.ReplaceAll(b, []byte{'-'}, []byte{})
b = bytes.ReplaceAll(b, []byte{'_'}, []byte{})
b = b[:size]
if len(unique) != 0 {
if Contains(*unique[0], b) {
time.Sleep(1 * time.Microsecond)
return URandBytes(size, unique[0])
}
*unique[0] = append(*unique[0], b)
}
return b
}
var uuidGenLastTime int64
// GenUUID generates a Unique Identifier using a custom build method
//
// Notice: This feature is currently in beta
//
// @size: (minimum: 8) the bit size for the last part of the uuid
// (note: other parts may vary)
//
// @timezone: optionally add a timezone string to the uuid
// (note: you could also pass random info into here for a more complex algorithm)
//
// This method uses the following data:
// - A hash of the current year and day of year
// - A hash of the current timezone
// - A hash of the current unix time (in seconds)
// - A hash of the current unix time in nanoseconds and a random number
//
// The returned value is url encoded and will look something like this: xxxx-xxxx-xxxx-xxxxxxxx
func GenUUID(size uint, timezone ...string) string {
for time.Now().UnixNano() <= uuidGenLastTime {
time.Sleep(3 * time.Nanosecond)
}
uuidGenLastTime = time.Now().UnixNano()
if size < 8 {
size = 8
}
uuid := [][]byte{{}, {}, {}, {}}
// year
{
s := int(math.Min(float64(size/4), 8))
if s < 4 {
s = 4
}
sm := s / 2
if s%2 != 0 {
sm++
}
b := sha1.Sum([]byte(strconv.Itoa(time.Now().Year())))
uuid[0] = []byte(base64.URLEncoding.EncodeToString(b[:]))[:sm]
b = sha1.Sum([]byte(strconv.Itoa(time.Now().YearDay())))
uuid[0] = append(uuid[0], []byte(base64.URLEncoding.EncodeToString(b[:]))[:sm]...)
uuid[0] = uuid[0][:s]
}
// time zone
{
s := int(math.Min(float64(size/8), 8))
if s < 4 {
s = 4
}
if len(timezone) != 0 {
sm := s / len(timezone)
if s%2 != 0 {
sm++
}
for _, zone := range timezone {
b := sha1.Sum([]byte(zone))
uuid[1] = append(uuid[1], []byte(base64.URLEncoding.EncodeToString(b[:]))[:sm]...)
}
uuid[1] = uuid[1][:s]
} else {
z, _ := time.Now().Zone()
b := sha1.Sum([]byte(z))
uuid[1] = []byte(base64.URLEncoding.EncodeToString(b[:]))[:s]
}
}
// unix time
{
s := int(math.Min(float64(size/2), 16))
if s < 4 {
s = 4
}
b := sha1.Sum([]byte(strconv.Itoa(int(time.Now().Unix()))))
uuid[2] = []byte(base64.URLEncoding.EncodeToString(b[:]))[:s]
}
// random
{
s := uint(math.Min(float64(size/4), 64))
if s < 4 {
s = 4
}
b := sha512.Sum512([]byte(strconv.Itoa(int(time.Now().UnixNano()))))
uuid[3] = []byte(base64.URLEncoding.EncodeToString(b[:]))[:s]
// uuid[3] = append(uuid[3], []byte(base64.URLEncoding.EncodeToString(RandBytes(size)))[:size-s]...)
uuid[3] = append(uuid[3], []byte(base64.URLEncoding.EncodeToString(URandBytes(size)))[:size-s]...)
}
if len(uuid[1]) == 0 {
uuid = append(uuid[:1], uuid[2:]...)
}
for i := range uuid {
uuid[i] = bytes.ReplaceAll(uuid[i], []byte{'-'}, []byte{'0'})
uuid[i] = bytes.ReplaceAll(uuid[i], []byte{'_'}, []byte{'1'})
}
return string(bytes.Join(uuid, []byte{'-'}))
}