rsa_encryption.go
692 Bytes
package common
import (
"crypto"
"crypto/md5"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
)
func RsaSignWithMd5Hex(data string, prvKey string) (string, error) {
block, _ := pem.Decode([]byte(prvKey))
privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
privateKey, err = x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return "", err
}
}
md5 := md5.New()
md5.Write([]byte(data))
hash := md5.Sum(nil)
signature, err := rsa.SignPKCS1v15(rand.Reader, privateKey.(*rsa.PrivateKey), crypto.MD5, hash[:])
if err != nil {
return "", err
}
out := base64.StdEncoding.EncodeToString(signature)
return out, nil
}