utils.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package utils
  2. import (
  3. "bytes"
  4. "crypto"
  5. "crypto/hmac"
  6. "crypto/md5"
  7. "crypto/rand"
  8. "crypto/rsa"
  9. "crypto/sha1"
  10. "crypto/x509"
  11. "encoding/base64"
  12. "encoding/hex"
  13. "fmt"
  14. "hash"
  15. "io"
  16. mathrand "math/rand"
  17. "net/url"
  18. "os"
  19. "runtime"
  20. "strconv"
  21. "sync/atomic"
  22. "time"
  23. )
  24. type uuid [16]byte
  25. const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  26. var hookRead = func(fn func(p []byte) (n int, err error)) func(p []byte) (n int, err error) {
  27. return fn
  28. }
  29. var hookRSA = func(fn func(rand io.Reader, priv *rsa.PrivateKey, hash crypto.Hash, hashed []byte) ([]byte, error)) func(rand io.Reader, priv *rsa.PrivateKey, hash crypto.Hash, hashed []byte) ([]byte, error) {
  30. return fn
  31. }
  32. // GetUUID returns a uuid
  33. func GetUUID() (uuidHex string) {
  34. uuid := newUUID()
  35. uuidHex = hex.EncodeToString(uuid[:])
  36. return
  37. }
  38. // RandStringBytes returns a rand string
  39. func RandStringBytes(n int) string {
  40. b := make([]byte, n)
  41. for i := range b {
  42. b[i] = letterBytes[mathrand.Intn(len(letterBytes))]
  43. }
  44. return string(b)
  45. }
  46. // ShaHmac1 return a string which has been hashed
  47. func ShaHmac1(source, secret string) string {
  48. key := []byte(secret)
  49. hmac := hmac.New(sha1.New, key)
  50. hmac.Write([]byte(source))
  51. signedBytes := hmac.Sum(nil)
  52. signedString := base64.StdEncoding.EncodeToString(signedBytes)
  53. return signedString
  54. }
  55. // Sha256WithRsa return a string which has been hashed with Rsa
  56. func Sha256WithRsa(source, secret string) string {
  57. decodeString, err := base64.StdEncoding.DecodeString(secret)
  58. if err != nil {
  59. panic(err)
  60. }
  61. private, err := x509.ParsePKCS8PrivateKey(decodeString)
  62. if err != nil {
  63. panic(err)
  64. }
  65. h := crypto.Hash.New(crypto.SHA256)
  66. h.Write([]byte(source))
  67. hashed := h.Sum(nil)
  68. signature, err := hookRSA(rsa.SignPKCS1v15)(rand.Reader, private.(*rsa.PrivateKey),
  69. crypto.SHA256, hashed)
  70. if err != nil {
  71. panic(err)
  72. }
  73. return base64.StdEncoding.EncodeToString(signature)
  74. }
  75. // GetMD5Base64 returns a string which has been base64
  76. func GetMD5Base64(bytes []byte) (base64Value string) {
  77. md5Ctx := md5.New()
  78. md5Ctx.Write(bytes)
  79. md5Value := md5Ctx.Sum(nil)
  80. base64Value = base64.StdEncoding.EncodeToString(md5Value)
  81. return
  82. }
  83. // GetTimeInFormatISO8601 returns a time string
  84. func GetTimeInFormatISO8601() (timeStr string) {
  85. gmt := time.FixedZone("GMT", 0)
  86. return time.Now().In(gmt).Format("2006-01-02T15:04:05Z")
  87. }
  88. // GetURLFormedMap returns a url encoded string
  89. func GetURLFormedMap(source map[string]string) (urlEncoded string) {
  90. urlEncoder := url.Values{}
  91. for key, value := range source {
  92. urlEncoder.Add(key, value)
  93. }
  94. urlEncoded = urlEncoder.Encode()
  95. return
  96. }
  97. func newUUID() uuid {
  98. ns := uuid{}
  99. safeRandom(ns[:])
  100. u := newFromHash(md5.New(), ns, RandStringBytes(16))
  101. u[6] = (u[6] & 0x0f) | (byte(2) << 4)
  102. u[8] = (u[8]&(0xff>>2) | (0x02 << 6))
  103. return u
  104. }
  105. func newFromHash(h hash.Hash, ns uuid, name string) uuid {
  106. u := uuid{}
  107. h.Write(ns[:])
  108. h.Write([]byte(name))
  109. copy(u[:], h.Sum(nil))
  110. return u
  111. }
  112. func safeRandom(dest []byte) {
  113. if _, err := hookRead(rand.Read)(dest); err != nil {
  114. panic(err)
  115. }
  116. }
  117. func (u uuid) String() string {
  118. buf := make([]byte, 36)
  119. hex.Encode(buf[0:8], u[0:4])
  120. buf[8] = '-'
  121. hex.Encode(buf[9:13], u[4:6])
  122. buf[13] = '-'
  123. hex.Encode(buf[14:18], u[6:8])
  124. buf[18] = '-'
  125. hex.Encode(buf[19:23], u[8:10])
  126. buf[23] = '-'
  127. hex.Encode(buf[24:], u[10:])
  128. return string(buf)
  129. }
  130. var processStartTime int64 = time.Now().UnixNano() / 1e6
  131. var seqId int64 = 0
  132. func getGID() uint64 {
  133. // https://blog.sgmansfield.com/2015/12/goroutine-ids/
  134. b := make([]byte, 64)
  135. b = b[:runtime.Stack(b, false)]
  136. b = bytes.TrimPrefix(b, []byte("goroutine "))
  137. b = b[:bytes.IndexByte(b, ' ')]
  138. n, _ := strconv.ParseUint(string(b), 10, 64)
  139. return n
  140. }
  141. func GetNonce() (uuidHex string) {
  142. routineId := getGID()
  143. currentTime := time.Now().UnixNano() / 1e6
  144. seq := atomic.AddInt64(&seqId, 1)
  145. randNum := mathrand.Int63()
  146. msg := fmt.Sprintf("%d-%d-%d-%d-%d", processStartTime, routineId, currentTime, seq, randNum)
  147. h := md5.New()
  148. h.Write([]byte(msg))
  149. return hex.EncodeToString(h.Sum(nil))
  150. }
  151. // Get first non-empty value
  152. func GetDefaultString(values ...string) string {
  153. for _, v := range values {
  154. if v != "" {
  155. return v
  156. }
  157. }
  158. return ""
  159. }
  160. // set back the memoried enviroment variables
  161. type Rollback func()
  162. func Memory(keys ...string) Rollback {
  163. // remenber enviroment variables
  164. m := make(map[string]string)
  165. for _, key := range keys {
  166. m[key] = os.Getenv(key)
  167. }
  168. return func() {
  169. for _, key := range keys {
  170. os.Setenv(key, m[key])
  171. }
  172. }
  173. }