|
|
@@ -2,10 +2,12 @@ package asign
|
|
|
|
|
|
import (
|
|
|
"crypto"
|
|
|
+ "crypto/md5"
|
|
|
"crypto/rand"
|
|
|
"crypto/rsa"
|
|
|
"crypto/sha1"
|
|
|
"crypto/x509"
|
|
|
+ "encoding/hex"
|
|
|
"encoding/pem"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
@@ -26,7 +28,7 @@ import (
|
|
|
// 4、签名算法:
|
|
|
// 4.1、将上述3所属的bizData(json字符串),按照阿拉伯字母排序(如:{"ba":1,"ac":2}--->{"ac":2,"ba":1}),
|
|
|
// 4.2、将4.1排序后的字符串,将【bizData+md5(bizData)+ appId + timestatmp】拼接后利用RSA非对称加密算法(SHA1withRSA),计算出最后的签名sign,对其base64编码,放入head的key(sign)中。
|
|
|
-func GetSignature(bizData map[string]interface{}, appId string, privateKeyPEM string) (signatureBase64 string, timestamp string, err error) {
|
|
|
+func GetSignature(bizData string, appId string, privateKeyPEM string) (signatureBase64 string, timestamp string, err error) {
|
|
|
timestamp = strconv.FormatInt(time.Now().UnixMilli(), 10)
|
|
|
// Parse the privateKeyPEM into an RSA private key
|
|
|
privateKeyBlock, _ := pem.Decode([]byte(privateKeyPEM))
|
|
|
@@ -39,8 +41,13 @@ func GetSignature(bizData map[string]interface{}, appId string, privateKeyPEM st
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+ // md5(bizData)
|
|
|
+ m := md5.New()
|
|
|
+ m.Write([]byte(bizData))
|
|
|
+ bdMd5Hx := hex.EncodeToString(m.Sum(nil))
|
|
|
+
|
|
|
// Message to be signed
|
|
|
- message := ""
|
|
|
+ message := bizData + bdMd5Hx + appId + timestamp
|
|
|
|
|
|
// Sign the message using SHA1withRSA
|
|
|
signature, err := signMessage(message, privateKey)
|
|
|
@@ -60,8 +67,8 @@ func signMessage(message string, privateKey *rsa.PrivateKey) ([]byte, error) {
|
|
|
}
|
|
|
|
|
|
// verifySignature verifies the given signature for the message using SHA1withRSA
|
|
|
-func verifySignature(message string, signature []byte, publicKey *rsa.PublicKey) bool {
|
|
|
- hashed := sha1.Sum([]byte(message))
|
|
|
- err := rsa.VerifyPKCS1v15(publicKey, crypto.SHA1, hashed[:], signature)
|
|
|
- return err == nil
|
|
|
-}
|
|
|
+// func verifySignature(message string, signature []byte, publicKey *rsa.PublicKey) bool {
|
|
|
+// hashed := sha1.Sum([]byte(message))
|
|
|
+// err := rsa.VerifyPKCS1v15(publicKey, crypto.SHA1, hashed[:], signature)
|
|
|
+// return err == nil
|
|
|
+// }
|