/
uidgen.go
92 lines (79 loc) · 2.55 KB
/
uidgen.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
package types
import (
"encoding/base64"
"encoding/binary"
sf "github.com/tinode/snowflake"
"golang.org/x/crypto/xtea"
)
// UidGenerator holds snowflake and encryption paramenets.
// RethinkDB generates UUIDs as primary keys. Using snowflake-generated uint64 instead.
type UidGenerator struct {
seq *sf.SnowFlake
cipher *xtea.Cipher
}
// Init initialises the Uid generator
func (ug *UidGenerator) Init(workerID uint, key []byte) error {
var err error
if ug.seq == nil {
ug.seq, err = sf.NewSnowFlake(uint32(workerID))
}
if ug.cipher == nil {
ug.cipher, err = xtea.NewCipher(key)
}
return err
}
// Get generates a unique weakly-encryped random-looking ID.
// The Uid is a unit64 with the highest bit possibly set which makes it
// incompatible with go's pre-1.9 sql package.
func (ug *UidGenerator) Get() Uid {
buf, err := getIDBuffer(ug)
if err != nil {
return ZeroUid
}
return Uid(binary.LittleEndian.Uint64(buf))
}
// GetStr generates the same unique ID as Get then returns it as
// base64-encoded string. Slightly more efficient than calling Get()
// then base64-encoding the result.
func (ug *UidGenerator) GetStr() string {
buf, err := getIDBuffer(ug)
if err != nil {
return ""
}
return base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(buf)
}
// getIdBuffer returns a byte array holding the Uid bytes
func getIDBuffer(ug *UidGenerator) ([]byte, error) {
var id uint64
var err error
if id, err = ug.seq.Next(); err != nil {
return nil, err
}
src := make([]byte, 8)
dst := make([]byte, 8)
binary.LittleEndian.PutUint64(src, id)
ug.cipher.Encrypt(dst, src)
return dst, nil
}
// DecodeUid takes an encrypted Uid and decrypts it into a non-negative int64.
// This is needed for go/sql compatibility where uint64 with high bit
// set is unsupported and possibly for other uses such as MySQL's recommendation
// for sequential primary keys.
func (ug *UidGenerator) DecodeUid(uid Uid) int64 {
src := make([]byte, 8)
dst := make([]byte, 8)
binary.LittleEndian.PutUint64(src, uint64(uid))
ug.cipher.Decrypt(dst, src)
return int64(binary.LittleEndian.Uint64(dst))
}
// EncodeInt64 takes a positive int64 and encrypts it into a Uid.
// This is needed for go/sql compatibility where uint64 with high bit
// set is unsupported and possibly for other uses such as MySQL's recommendation
// for sequential primary keys.
func (ug *UidGenerator) EncodeInt64(val int64) Uid {
src := make([]byte, 8)
dst := make([]byte, 8)
binary.LittleEndian.PutUint64(src, uint64(val))
ug.cipher.Encrypt(dst, src)
return Uid(binary.LittleEndian.Uint64(dst))
}