-
Notifications
You must be signed in to change notification settings - Fork 1
/
nanoid.go
108 lines (97 loc) · 2.55 KB
/
nanoid.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
package main
import (
"crypto/rand"
"errors"
"math"
)
// defaultAlphabet is the alphabet used for ID characters by default.
var defaultAlphabet = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
const (
defaultSize = 21
)
// getMask generates bit mask used to obtain bits from the random bytes that are used to get index of random character
// from the alphabet. Example: if the alphabet has 6 = (110)_2 characters it is sufficient to use mask 7 = (111)_2
func getMask(alphabetSize int) int {
for i := 1; i <= 8; i++ {
mask := (2 << uint(i)) - 1
if mask >= alphabetSize-1 {
return mask
}
}
return 0
}
// Generate is a low-level function to change alphabet and ID size.
func Generate(alphabet string, size int) (string, error) {
chars := []rune(alphabet)
if len(alphabet) == 0 || len(alphabet) > 255 {
return "", errors.New("alphabet must not be empty and contain no more than 255 chars")
}
if size <= 0 {
return "", errors.New("size must be positive integer")
}
mask := getMask(len(chars))
// estimate how many random bytes we will need for the ID, we might actually need more but this is tradeoff
// between average case and worst case
ceilArg := 1.6 * float64(mask*size) / float64(len(alphabet))
step := int(math.Ceil(ceilArg))
id := make([]rune, size)
bytes := make([]byte, step)
for j := 0; ; {
_, err := rand.Read(bytes)
if err != nil {
return "", err
}
for i := 0; i < step; i++ {
currByte := bytes[i] & byte(mask)
if currByte < byte(len(chars)) {
id[j] = chars[currByte]
j++
if j == size {
return string(id[:size]), nil
}
}
}
}
}
// MustGenerate is the same as Generate but panics on error.
func MustGenerate(alphabet string, size int) string {
id, err := Generate(alphabet, size)
if err != nil {
panic(err)
}
return id
}
// New generates secure URL-friendly unique ID.
// Accepts optional parameter - length of the ID to be generated (21 by default).
func NanoId(l ...int) (string, error) {
var size int
switch {
case len(l) == 0:
size = defaultSize
case len(l) == 1:
size = l[0]
if size < 0 {
return "", errors.New("negative id length")
}
default:
return "", errors.New("unexpected parameter")
}
bytes := make([]byte, size)
_, err := rand.Read(bytes)
if err != nil {
return "", err
}
id := make([]rune, size)
for i := 0; i < size; i++ {
id[i] = defaultAlphabet[bytes[i]&61]
}
return string(id[:size]), nil
}
// Must is the same as New but panics on error.
func Must(l ...int) string {
id, err := NanoId(l...)
if err != nil {
panic(err)
}
return id
}