-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashid.go
57 lines (45 loc) · 1.01 KB
/
hashid.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
package hashid
import (
"github.com/sohaha/zlsgo/zarray"
"errors"
"github.com/speps/go-hashids/v2"
)
type HashID struct {
hash *hashids.HashID
}
var h = zarray.NewHashMap[string, *HashID]()
func Set(name, salt string, minLength ...int) error {
if h.Has(name) {
return errors.New("repeat the initialization of the hashid")
}
l := 6
if len(minLength) > 0 {
l = minLength[0]
}
h.Set(name, New(salt, l))
return nil
}
func Get(name string) (*HashID, bool) {
hashid, ok := h.Get(name)
if !ok {
return nil, false
}
return hashid, true
}
func New(salt string, MinLength int) *HashID {
hd := hashids.NewData()
hd.Salt = salt
hd.MinLength = MinLength
hash, _ := hashids.NewWithData(hd)
return &HashID{hash: hash}
}
func EncryptID(hashI *HashID, id int64) (string, error) {
return hashI.hash.EncodeInt64([]int64{id})
}
func DecryptID(hashI *HashID, hid string) (int64, error) {
ids, err := hashI.hash.DecodeInt64WithError(hid)
if len(ids) == 0 || err != nil {
return 0, err
}
return ids[0], nil
}