-
Notifications
You must be signed in to change notification settings - Fork 1
/
uuid.go
41 lines (33 loc) · 965 Bytes
/
uuid.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
package uuid
import (
"crypto/rand"
"fmt"
"math/big"
)
const base62alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
// Creates a hexadecimal UUID.
func NewUuid() string {
b := make([]byte, 16)
rand.Read(b)
return fmt.Sprintf("%x", b)
}
// Creates a base-62 UUID.
func NewUuid62() string {
return ConvertUp(NewUuid(), base62alphabet)
}
// ConvertUp converts a hexadecimal UUID string to a base alphabet greater than
// 16. It is used here to compress a 32 character UUID down to 23 URL friendly
// characters.
func ConvertUp(oldNumber string, baseAlphabet string) string {
n := big.NewInt(0)
n.SetString(oldNumber, 16)
base := big.NewInt(int64(len(baseAlphabet)))
newNumber := make([]byte, 23) //converted size of max base-62 uuid
i := len(newNumber)
for n.Int64() != 0 {
i--
_, r := n.DivMod(n, base, big.NewInt(0))
newNumber[i] = baseAlphabet[r.Int64()]
}
return string(newNumber[i:])
}