hash_hashids.go 554 B

12345678910111213141516171819202122232425262728293031
  1. package hash
  2. import (
  3. "github.com/speps/go-hashids"
  4. )
  5. func (h *hash) HashidsEncode(params []int) (string, error) {
  6. hd := hashids.NewData()
  7. hd.Salt = h.secret
  8. hd.MinLength = h.length
  9. hashStr, err := hashids.NewWithData(hd).Encode(params)
  10. if err != nil {
  11. return "", err
  12. }
  13. return hashStr, nil
  14. }
  15. func (h *hash) HashidsDecode(hash string) ([]int, error) {
  16. hd := hashids.NewData()
  17. hd.Salt = h.secret
  18. hd.MinLength = h.length
  19. ids, err := hashids.NewWithData(hd).DecodeWithError(hash)
  20. if err != nil {
  21. return nil, err
  22. }
  23. return ids, nil
  24. }