-
Notifications
You must be signed in to change notification settings - Fork 6
/
hashid.go
73 lines (55 loc) · 1.38 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package hide
import (
"errors"
"github.com/speps/go-hashids"
)
// HashID implements the hide.Hash interface and uses github.com/speps/go-hashids to encode and decode hashes.
type HashID struct {
Salt string
MinLength int
}
// NewHashID creates a new HashID with given salt and minimum hash length.
func NewHashID(salt string, minlen int) *HashID {
return &HashID{salt, minlen}
}
// Encode implements the hide.Hash interface.
func (hasher *HashID) Encode(id ID) ([]byte, error) {
hash, err := hasher.newHash()
if err != nil {
return nil, err
}
result, err := hash.EncodeInt64([]int64{int64(id)})
if err != nil {
return nil, err
}
return []byte(result), nil
}
// Decode implements the hide.Hash interface.
func (hasher *HashID) Decode(data []byte) (ID, error) {
if len(data) == 0 {
return 0, nil
}
hash, err := hasher.newHash()
if err != nil {
return 0, err
}
result, err := hash.DecodeInt64WithError(string(data))
if err != nil {
return 0, err
}
if len(result) != 1 {
return 0, errors.New("input value too long")
}
return ID(result[0]), nil
}
// Creates a new hashids.HashID object to encode/decode IDs.
func (hasher *HashID) newHash() (*hashids.HashID, error) {
config := hashids.NewData()
config.Salt = hasher.Salt
config.MinLength = hasher.MinLength
hash, err := hashids.NewWithData(config)
if err != nil {
return nil, err
}
return hash, nil
}