1
This commit is contained in:
@@ -13,7 +13,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"github.com/tjfoc/gmsm/sm4"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"math/big"
|
||||
@@ -135,35 +134,35 @@ type UploadImgResp struct {
|
||||
}
|
||||
|
||||
// signParamRSA 支付签名
|
||||
func (a *API) signParamRSA(params map[string]interface{}, RSAPrivate string) (sig string, err error) {
|
||||
block, _ := pem.Decode([]byte(RSAPrivate))
|
||||
private, err := x509.ParsePKCS8PrivateKey(block.Bytes)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 签名参数
|
||||
body, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
//bodyData := base64.StdEncoding.EncodeToString(body)
|
||||
nonceStr := GenerateSecureRandomString(12)
|
||||
timeStamp := utils.Int64ToStr(time.Now().Unix())
|
||||
context := fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n", a.appID, a.serialNo, timeStamp, nonceStr, string(body))
|
||||
|
||||
// 进行rsa加密签名
|
||||
hashed := sha256.Sum256([]byte(context))
|
||||
signedData, err := rsa.SignPKCS1v15(rand.Reader, private.(*rsa.PrivateKey), crypto.SHA256, hashed[:])
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
signData := base64.StdEncoding.EncodeToString(signedData)
|
||||
authorization := fmt.Sprintf(`LKLAPI-SHA256withRSA appid="%s",serial_no="%s",timestamp="%s",nonce_str="%s",signature="%s"`, a.appID, a.serialNo, timeStamp, nonceStr, signData)
|
||||
return authorization, nil
|
||||
}
|
||||
//func (a *API) signParamRSA(params map[string]interface{}, RSAPrivate string) (sig string, err error) {
|
||||
// block, _ := pem.Decode([]byte(RSAPrivate))
|
||||
// private, err := x509.ParsePKCS8PrivateKey(block.Bytes)
|
||||
// if err != nil {
|
||||
// return "", err
|
||||
// }
|
||||
//
|
||||
// // 签名参数
|
||||
// body, err := json.Marshal(params)
|
||||
// if err != nil {
|
||||
// return "", err
|
||||
// }
|
||||
//
|
||||
// //bodyData := base64.StdEncoding.EncodeToString(body)
|
||||
// nonceStr := GenerateSecureRandomString(12)
|
||||
// timeStamp := utils.Int64ToStr(time.Now().Unix())
|
||||
// context := fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n", a.appID, a.serialNo, timeStamp, nonceStr, string(body))
|
||||
//
|
||||
// // 进行rsa加密签名
|
||||
// hashed := sha256.Sum256([]byte(context))
|
||||
// signedData, err := rsa.SignPKCS1v15(rand.Reader, private.(*rsa.PrivateKey), crypto.SHA256, hashed[:])
|
||||
// if err != nil {
|
||||
// return "", err
|
||||
// }
|
||||
//
|
||||
// signData := base64.StdEncoding.EncodeToString(signedData)
|
||||
// authorization := fmt.Sprintf(`LKLAPI-SHA256withRSA appid="%s",serial_no="%s",timestamp="%s",nonce_str="%s",signature="%s"`, a.appID, a.serialNo, timeStamp, nonceStr, signData)
|
||||
// return authorization, nil
|
||||
//}
|
||||
|
||||
// signParamRSA 支付签名
|
||||
func (a *API) signParamPrivateKey(params map[string]interface{}, RSAPrivate string) (sig string, err error) {
|
||||
@@ -220,34 +219,6 @@ func GetOrderNumber(length int) string {
|
||||
return utils.Time2TimeStrByFormat(time.Now(), TimeFormat) + string(bytes)
|
||||
}
|
||||
|
||||
func SM4ECBEncrypt(plaintext, key []byte) ([]byte, error) {
|
||||
if len(key) != sm4.BlockSize {
|
||||
return nil, fmt.Errorf("SM4 密钥长度必须为 %d 字节", sm4.BlockSize)
|
||||
}
|
||||
// 补位处理 (PKCS5Padding)
|
||||
paddedText := PKCS5Padding(plaintext, sm4.BlockSize)
|
||||
|
||||
// ECB 模式加密
|
||||
ciphertext := make([]byte, len(paddedText))
|
||||
for i := 0; i < len(paddedText); i += sm4.BlockSize {
|
||||
block := paddedText[i : i+sm4.BlockSize]
|
||||
encryptedBlock, err := sm4.Sm4Ecb(key, block, true) // true 表示加密
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
copy(ciphertext[i:], encryptedBlock)
|
||||
}
|
||||
|
||||
return ciphertext, nil
|
||||
}
|
||||
|
||||
// PKCS5Padding 填充(与 PKCS7Padding 对于 128位分组等价)
|
||||
func PKCS5Padding(data []byte, blockSize int) []byte {
|
||||
padding := blockSize - len(data)%blockSize
|
||||
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
|
||||
return append(data, padtext...)
|
||||
}
|
||||
|
||||
type CallBackResult struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
|
||||
Reference in New Issue
Block a user