guid_string.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package guid
  7. import (
  8. "os"
  9. "strconv"
  10. "time"
  11. "github.com/gogf/gf/container/gtype"
  12. "github.com/gogf/gf/encoding/ghash"
  13. "github.com/gogf/gf/net/gipv4"
  14. "github.com/gogf/gf/util/grand"
  15. )
  16. const (
  17. sequenceMax = uint32(46655) // Sequence max("zzz").
  18. randomStrBase = "0123456789abcdefghijklmnopqrstuvwxyz" // Random chars string(36 bytes).
  19. )
  20. var (
  21. sequence gtype.Uint32 // Sequence for unique purpose of current process.
  22. macAddrStr = "0000000" // MAC addresses hash result in 7 bytes.
  23. processIdStr = "0000" // Process id in 4 bytes.
  24. )
  25. // init initializes several fixed local variable.
  26. func init() {
  27. // MAC addresses hash result in 7 bytes.
  28. macs, _ := gipv4.GetMacArray()
  29. if len(macs) > 0 {
  30. var macAddrBytes []byte
  31. for _, mac := range macs {
  32. macAddrBytes = append(macAddrBytes, []byte(mac)...)
  33. }
  34. b := []byte{'0', '0', '0', '0', '0', '0', '0'}
  35. s := strconv.FormatUint(uint64(ghash.DJBHash(macAddrBytes)), 36)
  36. copy(b, s)
  37. macAddrStr = string(b)
  38. }
  39. // Process id in 4 bytes.
  40. {
  41. b := []byte{'0', '0', '0', '0'}
  42. s := strconv.FormatInt(int64(os.Getpid()), 36)
  43. copy(b, s)
  44. processIdStr = string(b)
  45. }
  46. }
  47. // S creates and returns a global unique string in 32 bytes that meets most common
  48. // usages without strict UUID algorithm. It returns an unique string using default
  49. // unique algorithm if no `data` is given.
  50. //
  51. // The specified `data` can be no more than 2 parts. No matter how long each of the
  52. // `data` size is, each of them will be hashed into 7 bytes as part of the result.
  53. // If given `data` parts is less than 2, the leftover size of the result bytes will
  54. // be token by random string.
  55. //
  56. // The returned string is composed with:
  57. // 1. Default: MAC(7) + PID(4) + TimestampNano(12) + Sequence(3) + RandomString(6)
  58. // 2. CustomData: Data(7/14) + TimestampNano(12) + Sequence(3) + RandomString(3/10)
  59. //
  60. // Note that:
  61. // 1. The returned length is fixed to 32 bytes for performance purpose.
  62. // 2. The custom parameter `data` composed should have unique attribute in your
  63. // business situation.
  64. func S(data ...[]byte) string {
  65. var (
  66. b = make([]byte, 32)
  67. nanoStr = strconv.FormatInt(time.Now().UnixNano(), 36)
  68. )
  69. if len(data) == 0 {
  70. copy(b, macAddrStr)
  71. copy(b[7:], processIdStr)
  72. copy(b[11:], nanoStr)
  73. copy(b[23:], getSequence())
  74. copy(b[26:], getRandomStr(6))
  75. } else if len(data) <= 2 {
  76. n := 0
  77. for i, v := range data {
  78. // Ignore empty data item bytes.
  79. if len(v) > 0 {
  80. copy(b[i*7:], getDataHashStr(v))
  81. n += 7
  82. }
  83. }
  84. copy(b[n:], nanoStr)
  85. copy(b[n+12:], getSequence())
  86. copy(b[n+12+3:], getRandomStr(32-n-12-3))
  87. } else {
  88. panic("too many data parts, it should be no more than 2 parts")
  89. }
  90. return string(b)
  91. }
  92. // getSequence increases and returns the sequence string in 3 bytes.
  93. // The sequence is less than "zzz"(46655).
  94. func getSequence() []byte {
  95. b := []byte{'0', '0', '0'}
  96. s := strconv.FormatUint(uint64(sequence.Add(1)%sequenceMax), 36)
  97. copy(b, s)
  98. return b
  99. }
  100. // getRandomStr randomly picks and returns `n` count of chars from randomStrBase.
  101. func getRandomStr(n int) []byte {
  102. if n <= 0 {
  103. return []byte{}
  104. }
  105. var (
  106. b = make([]byte, n)
  107. numberBytes = grand.B(n)
  108. )
  109. for i := range b {
  110. b[i] = randomStrBase[numberBytes[i]%36]
  111. }
  112. return b
  113. }
  114. // getDataHashStr creates and returns hash bytes in 7 bytes with given data bytes.
  115. func getDataHashStr(data []byte) []byte {
  116. b := []byte{'0', '0', '0', '0', '0', '0', '0'}
  117. s := strconv.FormatUint(uint64(ghash.DJBHash(data)), 36)
  118. copy(b, s)
  119. return b
  120. }