session.go 660 B

123456789101112131415161718192021222324252627282930313233
  1. package http
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "time"
  6. bm "go-common/library/net/http/blademaster"
  7. "github.com/Dai0522/go-hash/murmur3"
  8. )
  9. // SessionID .
  10. func SessionID(ctx *bm.Context) string {
  11. ts := time.Now().Unix()
  12. dev, _ := ctx.Get("device")
  13. buvid := dev.(*bm.Device).Buvid
  14. rnum := rand.Uint64()
  15. str := fmt.Sprintf("%s:%d:%d", buvid, rnum, ts)
  16. hc := murmur3.New().Murmur3_128([]byte(str))
  17. result := ""
  18. pattern := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"}
  19. for _, v := range hc {
  20. tmp := int(v & 15)
  21. result += pattern[tmp]
  22. tmp = int((v >> 4) & 15)
  23. result += pattern[tmp]
  24. }
  25. return result
  26. }