正在显示
3 个修改的文件
包含
123 行增加
和
0 行删除
common/aes_encryption.go
0 → 100644
1 | +package common | |
2 | + | |
3 | +import ( | |
4 | + "bytes" | |
5 | + "crypto/aes" | |
6 | + "crypto/cipher" | |
7 | + "encoding/base64" | |
8 | +) | |
9 | + | |
10 | +func Aes_CBC_Decrypt(crypted []byte, key, iv []byte) ([]byte, error) { | |
11 | + block, err := aes.NewCipher(key) | |
12 | + if err != nil { | |
13 | + return nil, err | |
14 | + } | |
15 | + blockMode := cipher.NewCBCDecrypter(block, iv) | |
16 | + origData := make([]byte, len(crypted)) | |
17 | + blockMode.CryptBlocks(origData, crypted) | |
18 | + origData = unpadding(origData) | |
19 | + return origData, nil | |
20 | +} | |
21 | + | |
22 | +func unpadding(cipherText []byte) []byte { | |
23 | + end := cipherText[len(cipherText)-1] | |
24 | + cipherText = cipherText[:len(cipherText)-int(end)] | |
25 | + | |
26 | + return cipherText | |
27 | +} | |
28 | + | |
29 | +func Aes_CBC_Encrypt(data []byte, key, iv []byte) (string, error) { | |
30 | + block, err := aes.NewCipher(key) | |
31 | + if err != nil { | |
32 | + return "", err | |
33 | + } | |
34 | + origData := padding([]byte(data), block.BlockSize()) | |
35 | + println("block ", block.BlockSize()) | |
36 | + println("block2 ", len(iv)) | |
37 | + blockMode := cipher.NewCBCEncrypter(block, iv) | |
38 | + crypted := make([]byte, len(origData)) | |
39 | + blockMode.CryptBlocks(crypted, origData) | |
40 | + return base64.StdEncoding.EncodeToString(crypted), nil | |
41 | +} | |
42 | + | |
43 | +func padding(plainText []byte, blockSize int) []byte { | |
44 | + n := blockSize - len(plainText)%blockSize | |
45 | + temp := bytes.Repeat([]byte{byte(n)}, n) | |
46 | + plainText = append(plainText, temp...) | |
47 | + | |
48 | + return plainText | |
49 | +} | ... | ... |
common/rsa_encryption.go
0 → 100644
1 | +package common | |
2 | + | |
3 | +import ( | |
4 | + "crypto" | |
5 | + "crypto/md5" | |
6 | + "crypto/rand" | |
7 | + "crypto/rsa" | |
8 | + "crypto/x509" | |
9 | + "encoding/base64" | |
10 | + "encoding/pem" | |
11 | +) | |
12 | + | |
13 | +func RsaSignWithMd5Hex(data string, prvKey string) (string, error) { | |
14 | + block, _ := pem.Decode([]byte(prvKey)) | |
15 | + privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes) | |
16 | + if err != nil { | |
17 | + privateKey, err = x509.ParsePKCS1PrivateKey(block.Bytes) | |
18 | + if err != nil { | |
19 | + return "", err | |
20 | + } | |
21 | + } | |
22 | + md5 := md5.New() | |
23 | + md5.Write([]byte(data)) | |
24 | + hash := md5.Sum(nil) | |
25 | + signature, err := rsa.SignPKCS1v15(rand.Reader, privateKey.(*rsa.PrivateKey), crypto.MD5, hash[:]) | |
26 | + if err != nil { | |
27 | + return "", err | |
28 | + } | |
29 | + out := base64.StdEncoding.EncodeToString(signature) | |
30 | + return out, nil | |
31 | +} | ... | ... |
order_client.go
0 → 100644
1 | +package socialwork_sdk | |
2 | + | |
3 | +import ( | |
4 | + "encoding/json" | |
5 | + "log" | |
6 | + "socialwork-sdk/common" | |
7 | +) | |
8 | + | |
9 | +type OrderClient struct { | |
10 | + Key string `json:"key"` | |
11 | + IV string `json:"iv"` | |
12 | + PublicKey string `json:"public_key"` | |
13 | + CustomerPrivateKey string `json:"customer_private_key"` | |
14 | +} | |
15 | + | |
16 | +func NewOrderClient(key, iv, customerPrivateKey, publicKey string) *OrderClient { | |
17 | + return &OrderClient{ | |
18 | + Key: key, | |
19 | + IV: iv, | |
20 | + PublicKey: publicKey, | |
21 | + CustomerPrivateKey: customerPrivateKey, | |
22 | + } | |
23 | +} | |
24 | + | |
25 | +func (o *OrderClient) EncryptAndSign(res interface{}) (string, string, error) { | |
26 | + bs, _ := json.Marshal(res) | |
27 | + cipherText, err := common.Aes_CBC_Encrypt(bs, []byte(o.Key), []byte(o.IV)) | |
28 | + if err != nil { | |
29 | + log.Printf("Aes_CBC_Encrypt.failed.err=%v\n", err) | |
30 | + return "", "", err | |
31 | + } | |
32 | + | |
33 | + sign, err := common.RsaSignWithMd5Hex(cipherText, o.CustomerPrivateKey) | |
34 | + if err != nil { | |
35 | + log.Printf("RsaSignWithMd5Hex.failed.err=%v\n", err) | |
36 | + return "", "", err | |
37 | + } | |
38 | + return cipherText, sign, nil | |
39 | +} | |
40 | + | |
41 | +func (o *OrderClient) PrintVersion() string { | |
42 | + return "1.0" | |
43 | +} | ... | ... |
请
注册
或
登录
后发表评论