-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
270 lines (248 loc) · 7.99 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* Copyright Bytedance Author(https://houseme.github.io/bytedance/). All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* You can obtain one at https://github.com/houseme/bytedance.
*
*/
// Package helper universal tool package
package helper
import (
"bytes"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"sort"
"strings"
"time"
"unicode"
"github.com/houseme/bytedance/config"
)
// ParsePrivateKey parses private key bytes to rsa privateKey
func ParsePrivateKey(privateKeyDecoded []byte, keyType config.Secret) (*rsa.PrivateKey, error) {
switch keyType {
case config.PKCS1:
return x509.ParsePKCS1PrivateKey(privateKeyDecoded)
case config.PKCS8:
if keyParsed, err := x509.ParsePKCS8PrivateKey(privateKeyDecoded); err != nil {
return nil, err
} else {
return keyParsed.(*rsa.PrivateKey), nil
}
default:
return &rsa.PrivateKey{}, errors.New("secretInfo PrivateKeyDataType unsupported")
}
}
// GenSign 生成签名
func GenSign(method, url, timestamp, nonce, body string, key []byte, keyType config.Secret) (sign string, err error) {
var privateKey *rsa.PrivateKey
if privateKey, err = ParsePrivateKey(key, keyType); err != nil {
return "", err
}
// method 内容必须大写,如 GET、POST,URI 不包含域名,必须以'/'开头
targetStr := method + "\n" + url + "\n" + timestamp + "\n" + nonce + "\n" + body + "\n"
h := sha256.New()
n, err := h.Write([]byte(targetStr))
if err != nil {
return "", err
}
if n != len(targetStr) {
return "", fmt.Errorf("sha256 write bytes not match")
}
digestBytes := h.Sum(nil)
signBytes, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, digestBytes)
if err != nil {
return "", err
}
sign = base64.StdEncoding.EncodeToString(signBytes)
return
}
// CheckSign 校验签名
func CheckSign(timestamp, nonce, body, signature, pubKeyStr string) (bool, error) {
pubKey, err := PemToRSAPublicKey(pubKeyStr)
if err != nil {
return false, err
}
hashed := sha256.Sum256([]byte(timestamp + "\n" + nonce + "\n" + body + "\n"))
signBytes, err := base64.StdEncoding.DecodeString(signature)
if err != nil {
return false, err
}
err = rsa.VerifyPKCS1v15(pubKey, crypto.SHA256, hashed[:], signBytes)
return err == nil, nil
}
// PemToRSAPublicKey pem to rsa public key
func PemToRSAPublicKey(pemKeyStr string) (*rsa.PublicKey, error) {
block, _ := pem.Decode([]byte(pemKeyStr))
if block == nil || len(block.Bytes) == 0 {
return nil, fmt.Errorf("empty block in pem string")
}
key, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
switch key := key.(type) {
case *rsa.PublicKey:
return key, nil
default:
return nil, fmt.Errorf("not rsa public key")
}
}
// Template 对字符串中的和 map 的 key 相同的字符串进行模板替换 仅支持 形如:{name}
func Template(source string, data map[string]interface{}) string {
sourceCopy := &source
for k, val := range data {
valStr := ""
switch v := val.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
valStr = fmt.Sprintf("%d", v)
case bool:
valStr = fmt.Sprintf("%v", v)
default:
valStr = fmt.Sprintf("%s", v)
}
*sourceCopy = strings.Replace(*sourceCopy, strings.Join([]string{"{", k, "}"}, ""), valStr, 1)
}
return *sourceCopy
}
// GetCurrTS return current timestamps
func GetCurrTS() int64 {
return time.Now().Unix()
}
// SliceChunk 用于将字符串切片分块
func SliceChunk(src []string, chunkSize int) (chunks [][]string) {
total := len(src)
chunks = make([][]string, 0)
if chunkSize < 1 {
chunkSize = 1
}
if total == 0 {
return
}
chunkNum := total / chunkSize
if total%chunkSize != 0 {
chunkNum++
}
chunks = make([][]string, chunkNum)
for i := 0; i < chunkNum; i++ {
for j := 0; j < chunkSize; j++ {
offset := i*chunkSize + j
if offset >= total {
return
}
if chunks[i] == nil {
actualChunkSize := chunkSize
if i == chunkNum-1 && total%chunkSize != 0 {
actualChunkSize = total % chunkSize
}
chunks[i] = make([]string, actualChunkSize)
}
chunks[i][j] = src[offset]
}
}
return
}
// Query 将 Map 序列化为 Query 参数
func Query(params map[string]interface{}) string {
finalString := make([]string, 0)
for key, value := range params {
valueString := ""
switch v := value.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
valueString = fmt.Sprintf("%d", v)
case bool:
valueString = fmt.Sprintf("%v", v)
default:
valueString = fmt.Sprintf("%s", v)
}
finalString = append(finalString, strings.Join([]string{key, valueString}, "="))
}
return strings.Join(finalString, "&")
}
// OrderParam order params
func OrderParam(p map[string]string, bizKey string) (returnStr string) {
keys := make([]string, 0, len(p))
for k := range p {
if k == "sign" {
continue
}
keys = append(keys, k)
}
sort.Strings(keys)
var buf bytes.Buffer
for _, k := range keys {
if p[k] == "" {
continue
}
if buf.Len() > 0 {
buf.WriteByte('&')
}
buf.WriteString(k)
buf.WriteByte('=')
buf.WriteString(p[k])
}
buf.WriteString(bizKey)
returnStr = buf.String()
return
}
// RSADecrypt 数据解密
func RSADecrypt(privateKey string, ciphertext []byte) ([]byte, error) {
block, _ := pem.Decode([]byte(privateKey))
if block == nil {
return nil, errors.New("PrivateKey format error")
}
privy, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
oldErr := err
key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("ParsePKCS1PrivateKey error: %s, ParsePKCS8PrivateKey error: %s", oldErr.Error(), err.Error())
}
switch t := key.(type) {
case *rsa.PrivateKey:
privy = key.(*rsa.PrivateKey)
default:
return nil, fmt.Errorf("ParsePKCS1PrivateKey error: %s, ParsePKCS8PrivateKey error: Not supported privatekey format, should be *rsa.PrivateKey, got %T", oldErr.Error(), t)
}
}
return rsa.DecryptPKCS1v15(rand.Reader, privy, ciphertext)
}
// RSADecryptBase64 Base64 解码后再次进行 RSA 解密
func RSADecryptBase64(privateKey string, cryptoText string) ([]byte, error) {
encryptedData, err := base64.StdEncoding.DecodeString(cryptoText)
if err != nil {
return nil, err
}
return RSADecrypt(privateKey, encryptedData)
}
// UcFirst 首字母大些
func UcFirst(str string) string {
for i, v := range str {
return string(unicode.ToUpper(v)) + str[i+1:]
}
return ""
}
// LcFirst 首字母小写
func LcFirst(str string) string {
for i, v := range str {
return string(unicode.ToLower(v)) + str[i+1:]
}
return ""
}