-
Notifications
You must be signed in to change notification settings - Fork 60
/
util.go
56 lines (48 loc) · 1.25 KB
/
util.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
package douyingo
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"errors"
"net/http"
"github.com/zhangshuai/douyin-go/auth"
)
type transport struct {
http.RoundTripper
credentials *auth.Credentials
}
func newTransport(credentials *auth.Credentials, tr http.RoundTripper) *transport {
if tr == nil {
tr = http.DefaultTransport
}
return &transport{tr, credentials}
}
// Base64Encode Base64编码
func Base64Encode(str []byte) string {
return base64.StdEncoding.EncodeToString(str)
}
// Base64Decode Base64解码
func Base64Decode(encodeString string) ([]byte, error) {
return base64.StdEncoding.DecodeString(encodeString)
}
// PKCS5UnPadding PKCS5填充
func PKCS5UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
// AesDecrypt AES解密
func AesDecrypt(crypted, key, iv []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
if len(iv) != block.BlockSize() {
var keySizeError aes.KeySizeError
return nil, errors.New(keySizeError.Error())
}
encrypter := cipher.NewCBCDecrypter(block, iv)
origData := make([]byte, len(crypted))
encrypter.CryptBlocks(origData, crypted)
return PKCS5UnPadding(origData), nil
}