Step 1 generate the RSA private key file ‘rsa_priviate. Key’
openssl genrsa -out rsa_private.key 1024
Copy the code
Step 2, use the private key file to generate the RSA public key file ‘rsa_public.key
openssl rsa -in rsa_private.key -pubout -out rsa_public.key
Copy the code
Step 3, place the private key filersa_private.key
Generate againPkcs8 coding
Private key filepkcs8_rsa_private.key
openssl pkcs8 -topk8 -nocrypt -in rsa_private.key -outform PEM -out pkcs8_rsa_private.key
Copy the code
Step 4, add the aboversa_public.key
andpkcs8_private.key
Two files embedded in the GO code are used as follows:
package main
import (
"crypto"
"crypto/md5"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"net/url"
"time"
)
var privateKey = []byte(`
-----BEGIN PRIVATE KEY-----
MIICeQIBADANBgkqhkiG9w0BAQEFAASCAmMwggJfAgEAAoGBAOLeZUZQeju3BLzx
Szk0D3UN6mjFxSDrqyZS/9nAoihQhn/+MKcAdis4wj2/0IdmQHS2iOtB0/5j9NGQ
9RqezvPsVirNw35OV4QypWpIp0D0HvXOBAGinbWiqowuTVaU9EYAvQVlgF2xQFWE
FCXFcsBWA37dAjqi2eESD3OrSqFRAgMBAAECgYEAz2StQl05kLNbonoW4UFFTCWf
f44kCldGi29esqpV0vLiMcmmolRasmJdt2NNdymzjL59Zks0SB8KZDXWyzIYTs2U
Gwc3KsMJMul8DaVllR67PKcvc9rbniJX1MQg6VmXg59eYdlGgh52bAR2oO5rK5Ol
sYdQbXPIHvS73xRtjH0CQQD4La/8RXsEX9PvasrzWIJ0hAZy/rgRyaFvxsTtCGTq
K/Yt5njVgMdXJqvKnLbXN27va24KJhhNcNCikrihrlsfAkEA6gTHzb51WgaRPfBs
5aPixUmw0X1AdoE0OaH5PaFNj2oZhLo/fvxscUQjjXgK7Muo/D31ydBrJQ38eP7S
sz3ljwJBANJg0FUWhQChRyQmMCJ2vieNH3ZtzJVvicUaByczvW/wkkA4pee+wbx0
EJ6xmCD6YbTdKau6vVEwN/wISjDsf1sCQQCpc6K2MUZKVN0Qy6oesof/MXfuiVKH
PNFzNf0DCmzd/wbRXAekTOW9MojH5HeMtX/8fmUeqGMdJr59Yo9xKM2zAkEAhzpc
Am5Q0eoDb9C49pXmZNYHRSb6fklP3qHmzNrzBvQLaqzhBtnBB96ewWP4xWWctEmp
Pc/RYVStP4kitZabIA==
-----END PRIVATE KEY-----
`)
var publicKey = []byte(`
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDi3mVGUHo7twS88Us5NA91Depo
xcUg66smUv/ZwKIoUIZ//jCnAHYrOMI9v9CHZkB0tojrQdP+Y/TRkPUans7z7FYq
zcN+TleEMqVqSKdA9B71zgQBop21oqqMLk1WlPRGAL0FZYBdsUBVhBQlxXLAVgN+
3QI6otnhEg9zq0qhUQIDAQAB
-----END PUBLIC KEY-----
`)
type ZtData struct {
SendTime string `json:"sendTime"`
Sign string `json:"sign"`
Msg string `json:"msg"`
Data map[string]string `json:"data"`
}
func GenPrivateKey(privateKey []byte) (*rsa.PrivateKey, error) {
var err error
var block *pem.Block
block, _ = pem.Decode(privateKey)
if block == nil {
return nil, errors.New("Private key error")
}
prkI, err := x509.ParsePKCS8PrivateKey(block.Bytes)
x509.ParsePKCS1PrivateKey(block.Bytes)
iferr ! =nil {
return nil, errors.New("Error parsing private key")
}
pri := prkI.(*rsa.PrivateKey)
return pri, nil
}
func SignPKCS1v15(src, privateKey []byte, hash crypto.Hash) ([]byte, error) {
h := hash.New()
h.Write(src)
var hashed = h.Sum(nil)
pri, err := GenPrivateKey(privateKey)
iferr ! =nil {
return nil, err
}
ret, err := rsa.SignPKCS1v15(rand.Reader, pri, hash, hashed)
iferr ! =nil {
return nil, errors.New("Signature error")}return ret, err
}
func signMd5(data []byte) (s string, err error) {
sig, err := SignPKCS1v15(data, privateKey, crypto.MD5)
iferr ! =nil {
return "", err
}
s = base64.StdEncoding.EncodeToString(sig)
return s, nil
}
func VerifyPKCS1v15(src, sig, key []byte, hash crypto.Hash) error {
h := md5.New()
h.Write(src)
var hashed = h.Sum(nil)
var err error
var block *pem.Block
block, _ = pem.Decode(key)
if block == nil {
return errors.New("public key error")}var pubInterface interface{}
pubInterface, err = x509.ParsePKIXPublicKey(block.Bytes)
iferr ! =nil {
return err
}
var pub = pubInterface.(*rsa.PublicKey)
return rsa.VerifyPKCS1v15(pub, hash, hashed, sig)
}
func checkSign(src, sig []byte) {
err := VerifyPKCS1v15(src, sig, publicKey, crypto.MD5)
}
func main(a) {
data := "abcdef"
ret, _ := signMd5([]byte(data))
ifret ! ="" {
ret = url.QueryEscape(ret)
}
fmt.Println(ret)
sign, err := base64.StdEncoding.DecodeString(ret)
fmt.Println(string(sign), err)
checkSign([]byte(data), sign)
}
Copy the code