- dingding callback

This commit is contained in:
gazebo
2019-03-09 08:10:04 +08:00
parent 9bd3b3f086
commit 803240fc9f
6 changed files with 160 additions and 26 deletions

43
utils/utils_crypt.go Normal file
View File

@@ -0,0 +1,43 @@
package utils
import (
"bytes"
"crypto/aes"
"crypto/cipher"
)
func AESCBCEncpryt(data, aesKey, iv []byte) (encryptedData []byte, err error) {
c, err := aes.NewCipher(aesKey)
if err != nil {
return nil, err
}
cfbdec := cipher.NewCBCEncrypter(c, iv[:c.BlockSize()])
data = PKCSPadding(data, c.BlockSize())
encryptedData = make([]byte, len(data))
cfbdec.CryptBlocks(encryptedData, data)
return encryptedData, nil
}
func AESCBCDecpryt(encryptedData, aesKey, iv []byte) (decryptedData []byte, err error) {
c, err := aes.NewCipher(aesKey)
if err != nil {
return nil, err
}
cfbdec := cipher.NewCBCDecrypter(c, iv[:c.BlockSize()])
decryptedData = make([]byte, len(encryptedData))
cfbdec.CryptBlocks(decryptedData, encryptedData)
decryptedData = PKCSUnPadding(decryptedData)
return decryptedData, nil
}
func PKCSUnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
func PKCSPadding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}

21
utils/utils_crypt_test.go Normal file
View File

@@ -0,0 +1,21 @@
package utils
import (
"testing"
)
func TestCrypt(t *testing.T) {
aesKey := []byte("123456789012345678901234567890ab")
msg := "hellasfsafsdsads"
encryptedMsg, err := AESCBCEncpryt([]byte(msg), aesKey, aesKey[:16])
if err != nil {
t.Fatal(err)
}
decryptedMsg, err := AESCBCDecpryt(encryptedMsg, aesKey, aesKey[:16])
if err != nil {
t.Fatal(err)
}
if msg != string(decryptedMsg) {
t.Fatal("result is wrong")
}
}