You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
683 B
28 lines
683 B
package wechatpayopen
|
|
|
|
import (
|
|
"crypto"
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"encoding/base64"
|
|
"fmt"
|
|
)
|
|
|
|
// 通过私钥对字符串以 SHA256WithRSA 算法生成签名信息
|
|
func (c *Client) signSHA256WithRSA(source string, privateKey *rsa.PrivateKey) (signature string, err error) {
|
|
if privateKey == nil {
|
|
return "", fmt.Errorf("private key should not be nil")
|
|
}
|
|
h := crypto.Hash.New(crypto.SHA256)
|
|
_, err = h.Write([]byte(source))
|
|
if err != nil {
|
|
return "", nil
|
|
}
|
|
hashed := h.Sum(nil)
|
|
signatureByte, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, hashed)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return base64.StdEncoding.EncodeToString(signatureByte), nil
|
|
}
|