1
This commit is contained in:
88
utils/aeshelper/aes256.go
Normal file
88
utils/aeshelper/aes256.go
Normal file
@ -0,0 +1,88 @@
|
||||
package aeshelper
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/md5"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"io"
|
||||
)
|
||||
|
||||
// Encrypt text with the passphrase
|
||||
func Encrypt(text string, passphrase string) string {
|
||||
salt := make([]byte, 8)
|
||||
if _, err := io.ReadFull(rand.Reader, salt); err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
key, iv := DeriveKeyAndIv(passphrase, string(salt))
|
||||
|
||||
block, err := aes.NewCipher([]byte(key))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
pad := PKCS7Padding([]byte(text), block.BlockSize())
|
||||
ecb := cipher.NewCBCEncrypter(block, []byte(iv))
|
||||
encrypted := make([]byte, len(pad))
|
||||
ecb.CryptBlocks(encrypted, pad)
|
||||
|
||||
return base64.StdEncoding.EncodeToString([]byte("Salted__" + string(salt) + string(encrypted)))
|
||||
}
|
||||
|
||||
// Decrypt encrypted text with the passphrase
|
||||
func Decrypt(encrypted string, passphrase string) string {
|
||||
ct, _ := base64.StdEncoding.DecodeString(encrypted)
|
||||
if len(ct) < 16 || string(ct[:8]) != "Salted__" {
|
||||
return ""
|
||||
}
|
||||
|
||||
salt := ct[8:16]
|
||||
ct = ct[16:]
|
||||
key, iv := DeriveKeyAndIv(passphrase, string(salt))
|
||||
|
||||
block, err := aes.NewCipher([]byte(key))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
cbc := cipher.NewCBCDecrypter(block, []byte(iv))
|
||||
dst := make([]byte, len(ct))
|
||||
cbc.CryptBlocks(dst, ct)
|
||||
|
||||
return string(PKCS7Trimming(dst))
|
||||
}
|
||||
|
||||
// PKCS7Padding PKCS7Padding
|
||||
func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
|
||||
padding := blockSize - len(ciphertext)%blockSize
|
||||
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
|
||||
return append(ciphertext, padtext...)
|
||||
}
|
||||
|
||||
// PKCS7Trimming PKCS7Trimming
|
||||
func PKCS7Trimming(encrypt []byte) []byte {
|
||||
padding := encrypt[len(encrypt)-1]
|
||||
return encrypt[:len(encrypt)-int(padding)]
|
||||
}
|
||||
|
||||
// DeriveKeyAndIv DeriveKeyAndIv
|
||||
func DeriveKeyAndIv(passphrase string, salt string) (string, string) {
|
||||
salted := ""
|
||||
dI := ""
|
||||
|
||||
for len(salted) < 48 {
|
||||
md := md5.New()
|
||||
md.Write([]byte(dI + passphrase + salt))
|
||||
dM := md.Sum(nil)
|
||||
dI = string(dM[:16])
|
||||
salted = salted + dI
|
||||
}
|
||||
|
||||
key := salted[0:32]
|
||||
iv := salted[32:48]
|
||||
|
||||
return key, iv
|
||||
}
|
||||
108
utils/aeshelper/aeshelper.go
Normal file
108
utils/aeshelper/aeshelper.go
Normal file
@ -0,0 +1,108 @@
|
||||
package aeshelper
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
|
||||
"github.com/forgoer/openssl"
|
||||
)
|
||||
|
||||
const (
|
||||
sKey = "ptQJqRKyICCTeo6w" // "dde4b1f8a9e6b814"
|
||||
ivParameter = "O3vZvOJSxQDP9hKT" // "dde4b1f8a9e6b814"
|
||||
|
||||
)
|
||||
|
||||
// PswEncrypt 加密
|
||||
func PswEncrypt(src string) (string, error) {
|
||||
key := []byte(sKey)
|
||||
iv := []byte(ivParameter)
|
||||
result, err := Aes128Encrypt([]byte(src), key, iv)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base64.RawStdEncoding.EncodeToString(result), nil
|
||||
}
|
||||
|
||||
// PswDecrypt 解密
|
||||
func PswDecrypt(src string) (string, error) {
|
||||
key := []byte(sKey)
|
||||
iv := []byte(ivParameter)
|
||||
var result []byte
|
||||
var err error
|
||||
result, err = base64.StdEncoding.DecodeString(src)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
origData, err := Aes128Decrypt(result, key, iv)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(origData), nil
|
||||
}
|
||||
|
||||
func Aes128Encrypt(origData, key []byte, IV []byte) ([]byte, error) {
|
||||
if key == nil || len(key) != 16 {
|
||||
return nil, nil
|
||||
}
|
||||
if IV != nil && len(IV) != 16 {
|
||||
return nil, nil
|
||||
}
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
blockSize := block.BlockSize()
|
||||
origData = PKCS5Padding(origData, blockSize)
|
||||
blockMode := cipher.NewCBCEncrypter(block, IV[:blockSize])
|
||||
crypted := make([]byte, len(origData))
|
||||
// 根据CryptBlocks方法的说明,如下方式初始化crypted也可以
|
||||
blockMode.CryptBlocks(crypted, origData)
|
||||
return crypted, nil
|
||||
}
|
||||
func Aes128Decrypt(crypted, key []byte, IV []byte) ([]byte, error) {
|
||||
if key == nil || len(key) != 16 {
|
||||
return nil, nil
|
||||
}
|
||||
if IV != nil && len(IV) != 16 {
|
||||
return nil, nil
|
||||
}
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
blockSize := block.BlockSize()
|
||||
blockMode := cipher.NewCBCDecrypter(block, IV[:blockSize])
|
||||
origData := make([]byte, len(crypted))
|
||||
blockMode.CryptBlocks(origData, crypted)
|
||||
origData = PKCS5UnPadding(origData)
|
||||
return origData, nil
|
||||
}
|
||||
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
|
||||
padding := blockSize - len(ciphertext)%blockSize
|
||||
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
|
||||
return append(ciphertext, padtext...)
|
||||
}
|
||||
func PKCS5UnPadding(origData []byte) []byte {
|
||||
length := len(origData)
|
||||
// 去掉最后一个字节 unpadding 次
|
||||
unpadding := int(origData[length-1])
|
||||
return origData[:(length - unpadding)]
|
||||
}
|
||||
|
||||
// 密码加密
|
||||
func AesEcbEncrypt(origData string) string {
|
||||
//加密
|
||||
dst, _ := openssl.AesECBEncrypt([]byte(origData), []byte(sKey), openssl.PKCS7_PADDING)
|
||||
return hex.EncodeToString(dst)
|
||||
}
|
||||
|
||||
// 密码解密
|
||||
func AesEcbDecrypt(origData string) string {
|
||||
value, _ := hex.DecodeString(origData)
|
||||
dst, _ := openssl.AesECBDecrypt(value, []byte(sKey), openssl.PKCS7_PADDING)
|
||||
return string(dst)
|
||||
}
|
||||
65
utils/aeshelper/aeshelper_test.go
Normal file
65
utils/aeshelper/aeshelper_test.go
Normal file
@ -0,0 +1,65 @@
|
||||
package aeshelper
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 测试加密解密
|
||||
func Test_EnDe(t *testing.T) {
|
||||
a := `asg`
|
||||
b := Encrypt(a, `code_verify_success`)
|
||||
c := Decrypt(b, `code_verify_success`)
|
||||
fmt.Println(`原始为`, a)
|
||||
fmt.Println(`加密后`, b)
|
||||
fmt.Println(`解密后`, c)
|
||||
}
|
||||
func TestAesEcbEncrypt(t *testing.T) {
|
||||
//aes := AesEcbEncrypt("123456")
|
||||
aes := AesEcbEncrypt(fmt.Sprintf("%v_%v", time.Now().Unix(), 1332355333))
|
||||
dst := AesEcbDecrypt(aes)
|
||||
fmt.Println(aes)
|
||||
fmt.Println(dst)
|
||||
}
|
||||
|
||||
// TODO:需要加密的接口
|
||||
/**
|
||||
1.合约下单接口 /api/futures/trade/order
|
||||
2.合约撤单 /api/futures/trade/cancelorder
|
||||
3.调整保证金 /api/futures/trade/adjustmargin
|
||||
4.变换逐全仓模式 /api/futures/trade/marginType
|
||||
5.更改持仓模式(方向) /api/futures/trade/positionSide/dual
|
||||
6.资产划转 /api/futures/transfer
|
||||
*/
|
||||
func TestAesEcbEncryptOrder(t *testing.T) {
|
||||
data := addFutOrderReq{
|
||||
OrderType: 1,
|
||||
BuyType: 3,
|
||||
TriggerDecide: 3,
|
||||
IsReduce: 2,
|
||||
Coin: "asdf",
|
||||
Price: "333.23",
|
||||
Num: "23.20",
|
||||
TriggerPrice: "1.023",
|
||||
PositionSide: "long",
|
||||
}
|
||||
b, _ := json.Marshal(data)
|
||||
aes := AesEcbEncrypt(string(b))
|
||||
dst := AesEcbDecrypt(aes)
|
||||
fmt.Println(aes)
|
||||
fmt.Println(dst)
|
||||
}
|
||||
|
||||
type addFutOrderReq struct {
|
||||
OrderType int `json:"order_type"` // 订单类型:1限价,2限价止盈止损,3市价,4市价止盈止损,5强平委托(就是限价委托)
|
||||
BuyType int `json:"buy_type"` // 买卖类型:1买,2卖
|
||||
TriggerDecide int `json:"trigger_decide"` // 触发条件 1按最新成交价格算,2按标记价格算
|
||||
IsReduce int `json:"is_reduce"` // 1是只减仓位(点击仓位列表中的平仓按钮),0正常
|
||||
Coin string `json:"coin"` // 交易币
|
||||
Price string `json:"price"` // 下单价格(限价+止盈止损时,该字段必填)
|
||||
Num string `json:"num"` // 下单数量(市价时该字段必填)
|
||||
TriggerPrice string `json:"trigger_price"` // 触发价格
|
||||
PositionSide string `json:"position_side"` // 持仓方向,单向持仓模式下可填both;在双向持仓模式下必填,且仅可选择 long 或 short
|
||||
}
|
||||
94
utils/aeshelper/aesoldhelper/aesoldhelper.go
Normal file
94
utils/aeshelper/aesoldhelper/aesoldhelper.go
Normal file
@ -0,0 +1,94 @@
|
||||
package aesoldhelper
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func addBase64Padding(value string) string {
|
||||
m := len(value) % 4
|
||||
if m != 0 {
|
||||
value += strings.Repeat("=", 4-m)
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
func removeBase64Padding(value string) string {
|
||||
return strings.Replace(value, "=", "", -1)
|
||||
}
|
||||
|
||||
// Pad Pad
|
||||
func Pad(src []byte) []byte {
|
||||
padding := aes.BlockSize - len(src)%aes.BlockSize
|
||||
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
|
||||
return append(src, padtext...)
|
||||
}
|
||||
|
||||
// Unpad Unpad
|
||||
func Unpad(src []byte) ([]byte, error) {
|
||||
length := len(src)
|
||||
unpadding := int(src[length-1])
|
||||
|
||||
if unpadding > length {
|
||||
return nil, errors.New("unpad error. This could happen when incorrect encryption key is used")
|
||||
}
|
||||
|
||||
return src[:(length - unpadding)], nil
|
||||
}
|
||||
|
||||
// AesEncrypt AesEncrypt
|
||||
func AesEncrypt(key []byte, text string) (string, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
msg := Pad([]byte(text))
|
||||
ciphertext := make([]byte, aes.BlockSize+len(msg))
|
||||
iv := ciphertext[:aes.BlockSize]
|
||||
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
cfb := cipher.NewCFBEncrypter(block, iv)
|
||||
cfb.XORKeyStream(ciphertext[aes.BlockSize:], []byte(msg))
|
||||
finalMsg := removeBase64Padding(base64.URLEncoding.EncodeToString(ciphertext))
|
||||
return finalMsg, nil
|
||||
}
|
||||
|
||||
// AesDecrypt AesDecrypt
|
||||
func AesDecrypt(key []byte, text string) (string, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
decodedMsg, err := base64.URLEncoding.DecodeString(addBase64Padding(text))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if (len(decodedMsg) % aes.BlockSize) != 0 {
|
||||
return "", errors.New("blocksize must be multipe of decoded message length")
|
||||
}
|
||||
|
||||
iv := decodedMsg[:aes.BlockSize]
|
||||
msg := decodedMsg[aes.BlockSize:]
|
||||
|
||||
cfb := cipher.NewCFBDecrypter(block, iv)
|
||||
cfb.XORKeyStream(msg, msg)
|
||||
|
||||
unpadMsg, err := Unpad(msg)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(unpadMsg), nil
|
||||
}
|
||||
19
utils/aeshelper/apikey.go
Normal file
19
utils/aeshelper/apikey.go
Normal file
@ -0,0 +1,19 @@
|
||||
package aeshelper
|
||||
|
||||
var (
|
||||
apiaeskey = "9jxFTkydwCJsmIA1TUrv"
|
||||
)
|
||||
|
||||
// EncryptApi 加密apikey
|
||||
func EncryptApi(apikey, secretkey string) (apikeyen, secrekeyen string) {
|
||||
apikeyen = Encrypt(apikey, apiaeskey)
|
||||
secrekeyen = Encrypt(secretkey, apiaeskey)
|
||||
return apikeyen, secrekeyen
|
||||
}
|
||||
|
||||
// DecryptApi 解密apikey
|
||||
func DecryptApi(apikeyen, secrekeyen string) (apikey, secrekey string) {
|
||||
apikey = Decrypt(apikeyen, apiaeskey)
|
||||
secrekey = Decrypt(secrekeyen, apiaeskey)
|
||||
return apikey, secrekey
|
||||
}
|
||||
Reference in New Issue
Block a user