-
Notifications
You must be signed in to change notification settings - Fork 0
/
hasher.go
64 lines (49 loc) · 903 Bytes
/
hasher.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
package hasher
import (
"github.com/speps/go-hashids/v2"
)
var (
MinLength = 30
Salt = "this is my salt"
)
type Hasher struct {
salt string
minLength int
}
func NewHasher(
salt string,
minLength int,
) Hasher {
return Hasher{
salt,
minLength,
}
}
func (h *Hasher) Hash(vals []int) (string, error) {
hd, e := hashids.NewWithData(&hashids.HashIDData{
Salt: h.salt,
Alphabet: hashids.DefaultAlphabet,
MinLength: h.minLength,
})
if e != nil {
return "", e
}
encodedString, e := hd.Encode(vals)
if e != nil {
return "", e
}
return encodedString, nil
}
func (h *Hasher) DecodeHash(hash string) ([]int, error) {
vals := []int{}
hd, e := hashids.NewWithData(&hashids.HashIDData{
Salt: h.salt,
MinLength: h.minLength,
Alphabet: hashids.DefaultAlphabet,
})
if e != nil {
return vals, e
}
vals = hd.Decode(hash)
return vals, nil
}