hashids.go 528 B

123456789101112131415161718192021222324252627
  1. package Utils
  2. import (
  3. "Game/framework/config"
  4. "github.com/speps/go-hashids"
  5. )
  6. func EncodeId(len int, id ...int) string {
  7. hd := hashids.NewData()
  8. hd.Salt = Config.Get.Hash.Salt
  9. hd.MinLength = len
  10. h := hashids.NewWithData(hd)
  11. e, _ := h.Encode(id)
  12. return e
  13. }
  14. func DecodeId(len int, encodedId string) ([]int, error) {
  15. hd := hashids.NewData()
  16. hd.Salt = Config.Get.Hash.Salt
  17. hd.MinLength = len
  18. h := hashids.NewWithData(hd)
  19. d, err := h.DecodeWithError(encodedId)
  20. if err != nil {
  21. return nil, err
  22. }
  23. return d, nil
  24. }