-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquantumid.go
More file actions
108 lines (88 loc) · 2.42 KB
/
quantumid.go
File metadata and controls
108 lines (88 loc) · 2.42 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
package quantumid
import (
"crypto/rand"
"time"
)
const (
base64Alphabet = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
base58Alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
)
func base64(raw []byte) string {
items := make([]byte, 22)
items[0] = base64Alphabet[raw[0]<<2>>2]
items[1] = base64Alphabet[raw[1]<<4>>2+raw[0]>>6]
items[2] = base64Alphabet[raw[2]<<6>>2+raw[1]>>4]
items[3] = base64Alphabet[raw[2]>>2]
items[4] = base64Alphabet[raw[3]<<2>>2]
items[5] = base64Alphabet[raw[4]<<4>>2+raw[3]>>6]
items[6] = base64Alphabet[raw[5]<<6>>2+raw[4]>>4]
items[7] = base64Alphabet[raw[5]>>2]
items[8] = base64Alphabet[raw[6]<<2>>2]
items[9] = base64Alphabet[raw[7]<<4>>2+raw[6]>>6]
items[10] = base64Alphabet[raw[8]<<6>>2+raw[7]>>4]
items[11] = base64Alphabet[raw[8]>>2]
items[12] = base64Alphabet[raw[9]<<2>>2]
items[13] = base64Alphabet[raw[10]<<4>>2+raw[9]>>6]
items[14] = base64Alphabet[raw[11]<<6>>2+raw[10]>>4]
items[15] = base64Alphabet[raw[11]>>2]
items[16] = base64Alphabet[raw[12]<<2>>2]
items[17] = base64Alphabet[raw[13]<<4>>2+raw[12]>>6]
items[18] = base64Alphabet[raw[14]<<6>>2+raw[13]>>4]
items[19] = base64Alphabet[raw[14]>>2]
items[20] = base64Alphabet[raw[15]<<2>>2]
items[21] = base64Alphabet[raw[15]>>6]
return string(items)
}
func base58(raw []byte) string {
num := make([]byte, len(raw))
copy(num, raw)
var result []byte
for len(num) > 0 {
rem := 0
for i := 0; i < len(num); i++ {
temp := rem*256 + int(num[i])
num[i] = byte(temp / 58)
rem = temp % 58
}
result = append([]byte{base58Alphabet[rem]}, result...)
for len(num) > 0 && num[0] == 0 {
num = num[1:]
}
}
for _, b := range raw {
if b != 0 {
break
}
result = append([]byte{base58Alphabet[0]}, result...)
}
// 确保固定长度22位,前面用base58Alphabet[0]补全
for len(result) < 22 {
result = append([]byte{base58Alphabet[0]}, result...)
}
return string(result)
}
// Generate function
// Deprecated: Use Base64() instead.
func Generate() string {
return Base64()
}
func Base64() string {
raw := make([]byte, 16)
s := time.Now().UnixNano()
for i := 0; i < 8; i++ {
raw[7-i] = byte(s % 256)
s >>= 8
}
_, _ = rand.Read(raw[8:16])
return base64(raw)
}
func Base58() string {
raw := make([]byte, 16)
s := time.Now().UnixNano()
for i := 0; i < 8; i++ {
raw[7-i] = byte(s % 256)
s >>= 8
}
_, _ = rand.Read(raw[8:16])
return base58(raw)
}