utils.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package utils
  2. import (
  3. "crypto"
  4. "crypto/hmac"
  5. "crypto/md5"
  6. "crypto/rand"
  7. "crypto/rsa"
  8. "crypto/sha1"
  9. "crypto/x509"
  10. "encoding/base64"
  11. "encoding/hex"
  12. "hash"
  13. "io"
  14. rand2 "math/rand"
  15. "net/url"
  16. "time"
  17. )
  18. type uuid [16]byte
  19. const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  20. var hookRead = func(fn func(p []byte) (n int, err error)) func(p []byte) (n int, err error) {
  21. return fn
  22. }
  23. 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) {
  24. return fn
  25. }
  26. // GetUUID returns a uuid
  27. // Deprecated: it was used for internal
  28. func GetUUID() (uuidHex string) {
  29. uuid := newUUID()
  30. uuidHex = hex.EncodeToString(uuid[:])
  31. return
  32. }
  33. // RandStringBytes returns a rand string
  34. func RandStringBytes(n int) string {
  35. b := make([]byte, n)
  36. for i := range b {
  37. b[i] = letterBytes[rand2.Intn(len(letterBytes))]
  38. }
  39. return string(b)
  40. }
  41. // ShaHmac1 return a string which has been hashed
  42. // Deprecated: it was used for internal
  43. func ShaHmac1(source, secret string) string {
  44. key := []byte(secret)
  45. hmac := hmac.New(sha1.New, key)
  46. hmac.Write([]byte(source))
  47. signedBytes := hmac.Sum(nil)
  48. signedString := base64.StdEncoding.EncodeToString(signedBytes)
  49. return signedString
  50. }
  51. // Sha256WithRsa return a string which has been hashed with Rsa
  52. // Deprecated: it was used for internal
  53. func Sha256WithRsa(source, secret string) string {
  54. decodeString, err := base64.StdEncoding.DecodeString(secret)
  55. if err != nil {
  56. panic(err)
  57. }
  58. private, err := x509.ParsePKCS8PrivateKey(decodeString)
  59. if err != nil {
  60. panic(err)
  61. }
  62. h := crypto.Hash.New(crypto.SHA256)
  63. h.Write([]byte(source))
  64. hashed := h.Sum(nil)
  65. signature, err := hookRSA(rsa.SignPKCS1v15)(rand.Reader, private.(*rsa.PrivateKey),
  66. crypto.SHA256, hashed)
  67. if err != nil {
  68. panic(err)
  69. }
  70. return base64.StdEncoding.EncodeToString(signature)
  71. }
  72. // GetMD5Base64 returns a string which has been base64
  73. // Deprecated: it was used for internal
  74. func GetMD5Base64(bytes []byte) (base64Value string) {
  75. md5Ctx := md5.New()
  76. md5Ctx.Write(bytes)
  77. md5Value := md5Ctx.Sum(nil)
  78. base64Value = base64.StdEncoding.EncodeToString(md5Value)
  79. return
  80. }
  81. // GetTimeInFormatISO8601 returns a time string
  82. // Deprecated: it was used for internal
  83. func GetTimeInFormatISO8601() (timeStr string) {
  84. gmt := time.FixedZone("GMT", 0)
  85. return time.Now().In(gmt).Format("2006-01-02T15:04:05Z")
  86. }
  87. // GetURLFormedMap returns a url encoded string
  88. // Deprecated: it was used for internal
  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. }