hash_hashids.go 677 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. hashID, err := hashids.NewWithData(hd)
  10. if err != nil {
  11. return "", err
  12. }
  13. hashStr, err := hashID.Encode(params)
  14. if err != nil {
  15. return "", err
  16. }
  17. return hashStr, nil
  18. }
  19. func (h *hash) HashidsDecode(hash string) ([]int, error) {
  20. hd := hashids.NewData()
  21. hd.Salt = h.secret
  22. hd.MinLength = h.length
  23. hashID, err := hashids.NewWithData(hd)
  24. if err != nil {
  25. return nil, err
  26. }
  27. ids, err := hashID.DecodeWithError(hash)
  28. if err != nil {
  29. return nil, err
  30. }
  31. return ids, nil
  32. }