mirror of
https://github.com/xinliangnote/go-gin-api.git
synced 2024-04-21 12:31:46 +00:00
32 lines
554 B
Go
32 lines
554 B
Go
package hash
|
|
|
|
import (
|
|
"github.com/speps/go-hashids"
|
|
)
|
|
|
|
func (h *hash) HashidsEncode(params []int) (string, error) {
|
|
hd := hashids.NewData()
|
|
hd.Salt = h.secret
|
|
hd.MinLength = h.length
|
|
|
|
hashStr, err := hashids.NewWithData(hd).Encode(params)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return hashStr, nil
|
|
}
|
|
|
|
func (h *hash) HashidsDecode(hash string) ([]int, error) {
|
|
hd := hashids.NewData()
|
|
hd.Salt = h.secret
|
|
hd.MinLength = h.length
|
|
|
|
ids, err := hashids.NewWithData(hd).DecodeWithError(hash)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return ids, nil
|
|
}
|