hash.go 838 B

1234567891011121314151617181920212223242526272829303132333435
  1. /**
  2. #*****************************************************************************
  3. # @file hash.go
  4. # @author MakerYang(https://www.makeryang.com)
  5. # @statement 免费课程配套开源项目,任何形式收费均为盗版
  6. #*****************************************************************************
  7. */
  8. package Utils
  9. import (
  10. "Game/framework/config"
  11. "github.com/speps/go-hashids"
  12. )
  13. func EncodeId(len int, id ...int) string {
  14. hd := hashids.NewData()
  15. hd.Salt = Config.Get.Hash.Salt
  16. hd.MinLength = len
  17. h := hashids.NewWithData(hd)
  18. e, _ := h.Encode(id)
  19. return e
  20. }
  21. func DecodeId(len int, encodedId string) ([]int, error) {
  22. hd := hashids.NewData()
  23. hd.Salt = Config.Get.Hash.Salt
  24. hd.MinLength = len
  25. h := hashids.NewWithData(hd)
  26. d, err := h.DecodeWithError(encodedId)
  27. if err != nil {
  28. return nil, err
  29. }
  30. return d, nil
  31. }