-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid.go
141 lines (118 loc) · 2.49 KB
/
id.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
package valid
import (
"crypto/rand"
"encoding/base32"
"encoding/binary"
"io"
"time"
)
var wordSafeBase32 = base32.NewEncoding("23456789CFGHJMPQRVWXcfghjmpqrvwx").WithPadding(base32.NoPadding)
var referenceTime = time.Date(2021, 9, 4, 0, 0, 0, 0, time.UTC)
func SetReferenceTime(t time.Time) {
referenceTime = t
}
type Source interface {
WithChecksum() Source
WithTimestamp() Source
Generate() string
Timestamp(id string) time.Time
IsValid(id string) bool
}
type baseSource struct {
randomByteLength int
useChecksum bool
useTimestamp bool
}
func (s baseSource) WithChecksum() Source {
s.useChecksum = true
return s
}
func (s baseSource) WithTimestamp() Source {
s.useTimestamp = true
return s
}
func NewSource(randomByteLength int) Source {
if randomByteLength < 1 {
panic("random byte length < 1")
}
return baseSource{
randomByteLength: randomByteLength,
useChecksum: false,
useTimestamp: false,
}
}
func (s baseSource) Generate() string {
b := make([]byte, s.byteLength())
cursor := 0
// Timestamp
if s.useTimestamp {
// 40bit timestamp ~ Millisecond
ts := (time.Now().UnixNano() - referenceTime.UnixNano()) >> 20 & ((1 << 40) - 1)
b[0] = byte(ts >> 32)
b[1] = byte(ts >> 24)
b[2] = byte(ts >> 16)
b[3] = byte(ts >> 8)
b[4] = byte(ts)
cursor = 5
}
// Random value
_, err := io.ReadFull(rand.Reader, b[cursor:cursor+s.randomByteLength])
if err != nil {
panic("failed to read random value")
}
cursor += s.randomByteLength
// Checksum
if s.useChecksum {
for _, bb := range b[:cursor] {
b[cursor] ^= bb
}
}
return wordSafeBase32.EncodeToString(b)
}
func (s baseSource) Timestamp(id string) time.Time {
if !s.useTimestamp {
return time.Time{}
}
if !s.IsValid(id) {
return time.Time{}
}
b, err := wordSafeBase32.DecodeString(id[:8])
if err != nil {
return time.Time{}
}
ts := make([]byte, 8)
ts[3] = b[0]
ts[4] = b[1]
ts[5] = b[2]
ts[6] = b[3]
ts[7] = b[4]
val := binary.BigEndian.Uint64(ts) << 20
return time.Unix(0, referenceTime.UnixNano()+int64(val))
}
func (s baseSource) IsValid(id string) bool {
if !s.useChecksum {
return false // No UseChecksum
}
b, err := wordSafeBase32.DecodeString(id)
if err != nil {
return false
}
if len(b) != s.byteLength() {
return false
}
var checksum byte
for _, bb := range b {
checksum ^= bb
}
return checksum == byte(0)
}
func (s baseSource) byteLength() int {
l := s.randomByteLength
if s.useTimestamp {
l += 5
}
if s.useChecksum {
l++
}
return l
}