/
hashids.go
139 lines (105 loc) · 2.41 KB
/
hashids.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
package bloxid
import (
"errors"
"fmt"
hashids "github.com/speps/go-hashids/v2"
)
const (
IDSchemeHashID = "hashid"
hashIDAllowedChar = "0123456789abcdef"
//the prefix needs to be uppercase so there are no collisions with chars in hashIDAllowedChar
hashIDPrefix = "HIDZ"
)
var (
ErrInvalidSalt = errors.New("invalid salt")
ErrInvalidID = errors.New("invalid id")
maxHashIDLen = DefaultUniqueIDDecodedCharSize - len(hashIDPrefix)
hashIDPrefixBytes = []byte(hashIDPrefix)
)
func WithHashIDInt64(id int64) func(o *V0Options) {
return func(o *V0Options) {
o.schemer = &schemerHashIDInt64{
HashIDInt64: id,
scheme: IDSchemeHashID,
}
}
}
type schemerHashIDInt64 struct {
HashIDInt64 int64
scheme string
}
var _ Schemer = (*schemerRandomEncodedID)(nil)
func (sch *schemerHashIDInt64) FromEntityID(opts *V0Options) (scheme string, decoded string, encoded string, err error) {
var hashed string
if sch.HashIDInt64 < 0 {
err = ErrInvalidID
return
}
decoded = fmt.Sprintf("%v", sch.HashIDInt64)
hashed, err = getHashID(sch.HashIDInt64, opts.hashidSalt)
if err != nil {
return
}
encoded = encodeLowerAlphaNumeric(hashIDPrefix, hashed)
scheme = IDSchemeHashID
return
}
func newHashID(salt string) (*hashids.HashID, error) {
hid := hashids.HashIDData{
Alphabet: hashIDAllowedChar,
MinLength: maxHashIDLen,
Salt: salt,
}
return hashids.NewWithData(&hid)
}
func validateGetHashID(id int64, salt string) error {
if len(salt) < 1 {
return ErrInvalidSalt
}
if id < 0 {
return ErrInvalidID
}
return nil
}
func getHashID(id int64, salt string) (string, error) {
if err := validateGetHashID(id, salt); err != nil {
return "", err
}
h, err := newHashID(salt)
if err != nil {
return "", err
}
eID, err := h.EncodeInt64([]int64{id})
if err != nil {
return "", err
}
return eID, err
}
func validateGetint64FromHashID(id, salt string) error {
if len(salt) < 1 {
return ErrInvalidSalt
}
if len(id) != maxHashIDLen {
return ErrInvalidID
}
return nil
}
func getInt64FromHashID(id, salt string) (int64, error) {
if err := validateGetint64FromHashID(id, salt); err != nil {
return -1, err
}
h, err := newHashID(salt)
if err != nil {
return -1, err
}
dID, err := h.DecodeInt64WithError(id)
if err != nil {
return -1, err
}
return dID[0], err
}
func WithHashIDSalt(salt string) func(o *V0Options) {
return func(o *V0Options) {
o.hashidSalt = salt
}
}