-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuxid.go
294 lines (250 loc) · 10.1 KB
/
uxid.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package uxid
import (
cryptorand "crypto/rand"
"io"
"time"
)
const CrockfordEncoding string = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
func Generate(prefix string, size string) (id_str string, err error) {
time_string := EncodeTime(time.Now().UTC())
rand_string := EncodeRand(size)
if "" == prefix {
return time_string + rand_string, nil
}
return prefix + "_" + time_string + rand_string, nil
}
func EncodeTime(t time.Time) string {
ms := Timestamp(t)
// Create the time bytes to store timestamp
tbytes := make([]byte, 6)
tbytes[0] = byte(ms >> 40)
tbytes[1] = byte(ms >> 32)
tbytes[2] = byte(ms >> 24)
tbytes[3] = byte(ms >> 16)
tbytes[4] = byte(ms >> 8)
tbytes[5] = byte(ms)
// Now encode the bytes into a time string
tstring := make([]byte, 10)
tstring[0] = CrockfordEncoding[(tbytes[0]&224)>>5]
tstring[1] = CrockfordEncoding[tbytes[0]&31]
tstring[2] = CrockfordEncoding[(tbytes[1]&248)>>3]
tstring[3] = CrockfordEncoding[((tbytes[1]&7)<<2)|((tbytes[2]&192)>>6)]
tstring[4] = CrockfordEncoding[(tbytes[2]&62)>>1]
tstring[5] = CrockfordEncoding[((tbytes[2]&1)<<4)|((tbytes[3]&240)>>4)]
tstring[6] = CrockfordEncoding[((tbytes[3]&15)<<1)|((tbytes[4]&128)>>7)]
tstring[7] = CrockfordEncoding[(tbytes[4]&124)>>2]
tstring[8] = CrockfordEncoding[((tbytes[4]&3)<<3)|((tbytes[5]&224)>>5)]
tstring[9] = CrockfordEncoding[tbytes[5]&31]
return string(tstring[:])
}
func EncodeRand(size string) string {
switch size {
case "0", "xs", "xsmall":
return ""
case "1":
return EncodeRand1()
case "2", "s", "small":
return EncodeRand2()
case "3":
return EncodeRand3()
case "4":
return EncodeRand4()
case "5", "m", "medium":
return EncodeRand5()
case "6":
return EncodeRand6()
case "7", "l", "large":
return EncodeRand7()
case "8":
return EncodeRand8()
case "9":
return EncodeRand9()
}
return EncodeRand10()
}
func EncodeRand1() string {
entropy := cryptorand.Reader
// Populate the entropy bytes
ebytes := make([]byte, 1)
_, _ = io.ReadFull(entropy, ebytes[:])
// Convert the bytes to a string using CrockfordEncoding
estring := make([]byte, 2)
estring[0] = CrockfordEncoding[(ebytes[0]&248)>>3]
estring[1] = CrockfordEncoding[((ebytes[0] & 7) << 2)]
return string(estring[:2])
}
func EncodeRand2() string {
entropy := cryptorand.Reader
// Populate the entropy bytes
ebytes := make([]byte, 2)
_, _ = io.ReadFull(entropy, ebytes[:])
// Convert the bytes to a string using CrockfordEncoding
estring := make([]byte, 4)
estring[0] = CrockfordEncoding[(ebytes[0]&248)>>3]
estring[1] = CrockfordEncoding[((ebytes[0]&7)<<2)|((ebytes[1]&192)>>6)]
estring[2] = CrockfordEncoding[(ebytes[1]&62)>>1]
estring[3] = CrockfordEncoding[((ebytes[1] & 1) << 4)]
return string(estring[:])
}
func EncodeRand3() string {
entropy := cryptorand.Reader
// Populate the entropy bytes
ebytes := make([]byte, 3)
_, _ = io.ReadFull(entropy, ebytes[:])
// Convert the bytes to a string using CrockfordEncoding
estring := make([]byte, 5)
estring[0] = CrockfordEncoding[(ebytes[0]&248)>>3]
estring[1] = CrockfordEncoding[((ebytes[0]&7)<<2)|((ebytes[1]&192)>>6)]
estring[2] = CrockfordEncoding[(ebytes[1]&62)>>1]
estring[3] = CrockfordEncoding[((ebytes[1]&1)<<4)|((ebytes[2]&240)>>4)]
estring[4] = CrockfordEncoding[((ebytes[2] & 15) << 1)]
return string(estring[:])
}
func EncodeRand4() string {
entropy := cryptorand.Reader
// Populate the entropy bytes
ebytes := make([]byte, 4)
_, _ = io.ReadFull(entropy, ebytes[:])
// Convert the bytes to a string using CrockfordEncoding
estring := make([]byte, 7)
estring[0] = CrockfordEncoding[(ebytes[0]&248)>>3]
estring[1] = CrockfordEncoding[((ebytes[0]&7)<<2)|((ebytes[1]&192)>>6)]
estring[2] = CrockfordEncoding[(ebytes[1]&62)>>1]
estring[3] = CrockfordEncoding[((ebytes[1]&1)<<4)|((ebytes[2]&240)>>4)]
estring[4] = CrockfordEncoding[((ebytes[2]&15)<<1)|((ebytes[3]&128)>>7)]
estring[5] = CrockfordEncoding[(ebytes[3]&124)>>2]
estring[6] = CrockfordEncoding[((ebytes[3] & 3) << 3)]
return string(estring[:])
}
func EncodeRand5() string {
entropy := cryptorand.Reader
// Populate the entropy bytes
ebytes := make([]byte, 5)
_, _ = io.ReadFull(entropy, ebytes[:])
// Convert the bytes to a string using CrockfordEncoding
estring := make([]byte, 8)
estring[0] = CrockfordEncoding[(ebytes[0]&248)>>3]
estring[1] = CrockfordEncoding[((ebytes[0]&7)<<2)|((ebytes[1]&192)>>6)]
estring[2] = CrockfordEncoding[(ebytes[1]&62)>>1]
estring[3] = CrockfordEncoding[((ebytes[1]&1)<<4)|((ebytes[2]&240)>>4)]
estring[4] = CrockfordEncoding[((ebytes[2]&15)<<1)|((ebytes[3]&128)>>7)]
estring[5] = CrockfordEncoding[(ebytes[3]&124)>>2]
estring[6] = CrockfordEncoding[((ebytes[3]&3)<<3)|((ebytes[4]&224)>>5)]
estring[7] = CrockfordEncoding[ebytes[4]&31]
return string(estring[:])
}
func EncodeRand6() string {
entropy := cryptorand.Reader
// Populate the entropy bytes
ebytes := make([]byte, 6)
_, _ = io.ReadFull(entropy, ebytes[:])
// Convert the bytes to a string using CrockfordEncoding
estring := make([]byte, 10)
estring[0] = CrockfordEncoding[(ebytes[0]&248)>>3]
estring[1] = CrockfordEncoding[((ebytes[0]&7)<<2)|((ebytes[1]&192)>>6)]
estring[2] = CrockfordEncoding[(ebytes[1]&62)>>1]
estring[3] = CrockfordEncoding[((ebytes[1]&1)<<4)|((ebytes[2]&240)>>4)]
estring[4] = CrockfordEncoding[((ebytes[2]&15)<<1)|((ebytes[3]&128)>>7)]
estring[5] = CrockfordEncoding[(ebytes[3]&124)>>2]
estring[6] = CrockfordEncoding[((ebytes[3]&3)<<3)|((ebytes[4]&224)>>5)]
estring[7] = CrockfordEncoding[ebytes[4]&31]
estring[8] = CrockfordEncoding[(ebytes[5]&248)>>3]
estring[9] = CrockfordEncoding[((ebytes[5] & 7) << 2)]
return string(estring[:])
}
func EncodeRand7() string {
entropy := cryptorand.Reader
// Populate the entropy bytes
ebytes := make([]byte, 7)
_, _ = io.ReadFull(entropy, ebytes[:])
// Convert the bytes to a string using CrockfordEncoding
estring := make([]byte, 12)
estring[0] = CrockfordEncoding[(ebytes[0]&248)>>3]
estring[1] = CrockfordEncoding[((ebytes[0]&7)<<2)|((ebytes[1]&192)>>6)]
estring[2] = CrockfordEncoding[(ebytes[1]&62)>>1]
estring[3] = CrockfordEncoding[((ebytes[1]&1)<<4)|((ebytes[2]&240)>>4)]
estring[4] = CrockfordEncoding[((ebytes[2]&15)<<1)|((ebytes[3]&128)>>7)]
estring[5] = CrockfordEncoding[(ebytes[3]&124)>>2]
estring[6] = CrockfordEncoding[((ebytes[3]&3)<<3)|((ebytes[4]&224)>>5)]
estring[7] = CrockfordEncoding[ebytes[4]&31]
estring[8] = CrockfordEncoding[(ebytes[5]&248)>>3]
estring[9] = CrockfordEncoding[((ebytes[5]&7)<<2)|((ebytes[6]&192)>>6)]
estring[10] = CrockfordEncoding[(ebytes[6]&62)>>1]
estring[11] = CrockfordEncoding[((ebytes[6] & 1) << 4)]
return string(estring[:])
}
func EncodeRand8() string {
entropy := cryptorand.Reader
// Populate the entropy bytes
ebytes := make([]byte, 8)
_, _ = io.ReadFull(entropy, ebytes[:])
// Convert the bytes to a string using CrockfordEncoding
estring := make([]byte, 13)
estring[0] = CrockfordEncoding[(ebytes[0]&248)>>3]
estring[1] = CrockfordEncoding[((ebytes[0]&7)<<2)|((ebytes[1]&192)>>6)]
estring[2] = CrockfordEncoding[(ebytes[1]&62)>>1]
estring[3] = CrockfordEncoding[((ebytes[1]&1)<<4)|((ebytes[2]&240)>>4)]
estring[4] = CrockfordEncoding[((ebytes[2]&15)<<1)|((ebytes[3]&128)>>7)]
estring[5] = CrockfordEncoding[(ebytes[3]&124)>>2]
estring[6] = CrockfordEncoding[((ebytes[3]&3)<<3)|((ebytes[4]&224)>>5)]
estring[7] = CrockfordEncoding[ebytes[4]&31]
estring[8] = CrockfordEncoding[(ebytes[5]&248)>>3]
estring[9] = CrockfordEncoding[((ebytes[5]&7)<<2)|((ebytes[6]&192)>>6)]
estring[10] = CrockfordEncoding[(ebytes[6]&62)>>1]
estring[11] = CrockfordEncoding[((ebytes[6]&1)<<4)|((ebytes[7]&240)>>4)]
estring[12] = CrockfordEncoding[((ebytes[7] & 15) << 1)]
return string(estring[:])
}
func EncodeRand9() string {
entropy := cryptorand.Reader
// Populate the entropy bytes
ebytes := make([]byte, 9)
_, _ = io.ReadFull(entropy, ebytes[:])
// Convert the bytes to a string using CrockfordEncoding
estring := make([]byte, 15)
estring[0] = CrockfordEncoding[(ebytes[0]&248)>>3]
estring[1] = CrockfordEncoding[((ebytes[0]&7)<<2)|((ebytes[1]&192)>>6)]
estring[2] = CrockfordEncoding[(ebytes[1]&62)>>1]
estring[3] = CrockfordEncoding[((ebytes[1]&1)<<4)|((ebytes[2]&240)>>4)]
estring[4] = CrockfordEncoding[((ebytes[2]&15)<<1)|((ebytes[3]&128)>>7)]
estring[5] = CrockfordEncoding[(ebytes[3]&124)>>2]
estring[6] = CrockfordEncoding[((ebytes[3]&3)<<3)|((ebytes[4]&224)>>5)]
estring[7] = CrockfordEncoding[ebytes[4]&31]
estring[8] = CrockfordEncoding[(ebytes[5]&248)>>3]
estring[9] = CrockfordEncoding[((ebytes[5]&7)<<2)|((ebytes[6]&192)>>6)]
estring[10] = CrockfordEncoding[(ebytes[6]&62)>>1]
estring[11] = CrockfordEncoding[((ebytes[6]&1)<<4)|((ebytes[7]&240)>>4)]
estring[12] = CrockfordEncoding[((ebytes[7]&15)<<1)|((ebytes[8]&128)>>7)]
estring[13] = CrockfordEncoding[(ebytes[8]&124)>>2]
estring[14] = CrockfordEncoding[((ebytes[8] & 3) << 3)]
return string(estring[:])
}
func EncodeRand10() string {
entropy := cryptorand.Reader
// Populate the entropy bytes
ebytes := make([]byte, 10)
_, _ = io.ReadFull(entropy, ebytes[:])
// Convert the bytes to a string using CrockfordEncoding
estring := make([]byte, 16)
estring[0] = CrockfordEncoding[(ebytes[0]&248)>>3]
estring[1] = CrockfordEncoding[((ebytes[0]&7)<<2)|((ebytes[1]&192)>>6)]
estring[2] = CrockfordEncoding[(ebytes[1]&62)>>1]
estring[3] = CrockfordEncoding[((ebytes[1]&1)<<4)|((ebytes[2]&240)>>4)]
estring[4] = CrockfordEncoding[((ebytes[2]&15)<<1)|((ebytes[3]&128)>>7)]
estring[5] = CrockfordEncoding[(ebytes[3]&124)>>2]
estring[6] = CrockfordEncoding[((ebytes[3]&3)<<3)|((ebytes[4]&224)>>5)]
estring[7] = CrockfordEncoding[ebytes[4]&31]
estring[8] = CrockfordEncoding[(ebytes[5]&248)>>3]
estring[9] = CrockfordEncoding[((ebytes[5]&7)<<2)|((ebytes[6]&192)>>6)]
estring[10] = CrockfordEncoding[(ebytes[6]&62)>>1]
estring[11] = CrockfordEncoding[((ebytes[6]&1)<<4)|((ebytes[7]&240)>>4)]
estring[12] = CrockfordEncoding[((ebytes[7]&15)<<1)|((ebytes[8]&128)>>7)]
estring[13] = CrockfordEncoding[(ebytes[8]&124)>>2]
estring[14] = CrockfordEncoding[((ebytes[8]&3)<<3)|((ebytes[9]&224)>>5)]
estring[15] = CrockfordEncoding[ebytes[9]&31]
return string(estring[:])
}
func Timestamp(t time.Time) uint64 {
return uint64(t.Unix())*1000 +
uint64(t.Nanosecond()/int(time.Millisecond))
}