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