utils.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. func GetUUID() (uuidHex string) {
  28. uuid := newUUID()
  29. uuidHex = hex.EncodeToString(uuid[:])
  30. return
  31. }
  32. // RandStringBytes returns a rand string
  33. func RandStringBytes(n int) string {
  34. b := make([]byte, n)
  35. for i := range b {
  36. b[i] = letterBytes[rand2.Intn(len(letterBytes))]
  37. }
  38. return string(b)
  39. }
  40. // ShaHmac1 return a string which has been hashed
  41. func ShaHmac1(source, secret string) string {
  42. key := []byte(secret)
  43. hmac := hmac.New(sha1.New, key)
  44. hmac.Write([]byte(source))
  45. signedBytes := hmac.Sum(nil)
  46. signedString := base64.StdEncoding.EncodeToString(signedBytes)
  47. return signedString
  48. }
  49. // Sha256WithRsa return a string which has been hashed with Rsa
  50. func Sha256WithRsa(source, secret string) string {
  51. decodeString, err := base64.StdEncoding.DecodeString(secret)
  52. if err != nil {
  53. panic(err)
  54. }
  55. private, err := x509.ParsePKCS8PrivateKey(decodeString)
  56. if err != nil {
  57. panic(err)
  58. }
  59. h := crypto.Hash.New(crypto.SHA256)
  60. h.Write([]byte(source))
  61. hashed := h.Sum(nil)
  62. signature, err := hookRSA(rsa.SignPKCS1v15)(rand.Reader, private.(*rsa.PrivateKey),
  63. crypto.SHA256, hashed)
  64. if err != nil {
  65. panic(err)
  66. }
  67. return base64.StdEncoding.EncodeToString(signature)
  68. }
  69. // GetMD5Base64 returns a string which has been base64
  70. func GetMD5Base64(bytes []byte) (base64Value string) {
  71. md5Ctx := md5.New()
  72. md5Ctx.Write(bytes)
  73. md5Value := md5Ctx.Sum(nil)
  74. base64Value = base64.StdEncoding.EncodeToString(md5Value)
  75. return
  76. }
  77. // GetTimeInFormatISO8601 returns a time string
  78. func GetTimeInFormatISO8601() (timeStr string) {
  79. gmt := time.FixedZone("GMT", 0)
  80. return time.Now().In(gmt).Format("2006-01-02T15:04:05Z")
  81. }
  82. // GetURLFormedMap returns a url encoded string
  83. func GetURLFormedMap(source map[string]string) (urlEncoded string) {
  84. urlEncoder := url.Values{}
  85. for key, value := range source {
  86. urlEncoder.Add(key, value)
  87. }
  88. urlEncoded = urlEncoder.Encode()
  89. return
  90. }
  91. func newUUID() uuid {
  92. ns := uuid{}
  93. safeRandom(ns[:])
  94. u := newFromHash(md5.New(), ns, RandStringBytes(16))
  95. u[6] = (u[6] & 0x0f) | (byte(2) << 4)
  96. u[8] = (u[8]&(0xff>>2) | (0x02 << 6))
  97. return u
  98. }
  99. func newFromHash(h hash.Hash, ns uuid, name string) uuid {
  100. u := uuid{}
  101. h.Write(ns[:])
  102. h.Write([]byte(name))
  103. copy(u[:], h.Sum(nil))
  104. return u
  105. }
  106. func safeRandom(dest []byte) {
  107. if _, err := hookRead(rand.Read)(dest); err != nil {
  108. panic(err)
  109. }
  110. }
  111. func (u uuid) String() string {
  112. buf := make([]byte, 36)
  113. hex.Encode(buf[0:8], u[0:4])
  114. buf[8] = '-'
  115. hex.Encode(buf[9:13], u[4:6])
  116. buf[13] = '-'
  117. hex.Encode(buf[14:18], u[6:8])
  118. buf[18] = '-'
  119. hex.Encode(buf[19:23], u[8:10])
  120. buf[23] = '-'
  121. hex.Encode(buf[24:], u[10:])
  122. return string(buf)
  123. }