私钥签名公钥验证
package main
import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"fmt"
)
func main() {
pri := "MIICXAIBAAKBgQDO/DNlf8XfyvCh2CMFXhV9cwZkEDhSqul/NiOrGLTWpK+gqYU1iMO6BQ2alz8VfAhrzxvM/KVY++8R5p6ICHBloiYckIDlVnXi3gG10QVCrznxSoW8pB9Rxrx0ThWDUUJ5c/2I3XQvryQkg5usqbgauta8QJ/utUGSvGMjbHy0cQIDAQABAoGAcd6BD/epDiMBGqhjK9LEk80x2N6rfZ8U8a0BFmFzKxpSnFpmNDtXIgEhqMhIJjOkFltL+guLk1aWAQ0UbdXmrqUdNpEn0/3CEiLiFEjNjfbeBs0DMxg8KZ/VcJStj4E53orv6bCWCTG7fnDXFpBNQhg5Du+sH9ci33VkT06GlAECQQDXWiYPomWxfh66Ac6Q6x+n6i984n6hTU/WG2T2PvY3OhjX6MKglTvN2B5bks9oTyRdk4oEJfLVfTU0YIxSE9DhAkEA9g3CDojjJzfesIfInN0Yh/UvmPAjs2K669JZL+PQRDbCXPRvNgOJKWsmt3tBxiIRjG4Emn5GqVDO6NkMLw8FkQJAbuhmobapWPfzKyizEb+F1hytyEt9m+UNMN9657cVZuvsz4V4dutkmWase8zoNHhN6kOJ2QkSPxD0zbzLh5ndAQJBAMyC0vDRxjwqq+V+drgq7ODUYiN0Nbg8L7EivQ6ZA9ouyY4JfQYgGjla3mtS/3B3kK5wI1Nl8cV5bQEMppvWmIECQFvqSVxiFPYfntZSaRbZv7D5bs936W7O3YXZcV/QOGBqQHUEyWlOfvxHK2CZZ569RF1E87xh8d2W2JTPcGPnLW8="
pub := "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDO/DNlf8XfyvCh2CMFXhV9cwZkEDhSqul/NiOrGLTWpK+gqYU1iMO6BQ2alz8VfAhrzxvM/KVY++8R5p6ICHBloiYckIDlVnXi3gG10QVCrznxSoW8pB9Rxrx0ThWDUUJ5c/2I3XQvryQkg5usqbgauta8QJ/utUGSvGMjbHy0cQIDAQAB"
prik,_ := LoadPrivateKeyBase64(pri)
pubk,_ := LoadPublicKeyBase64(pub)
a, _ := RsaSignWithSha1Hex("0x0de7cb19f736c2e123f5885ec5a105cad5719410cafd74bb2d749f1b73554bf3", prik)
fmt.Println(a)
err := RsaVerySignWithSha1Base64("0x6efecf48429a3955d1d2bebc5116f784d867ccd56275bc18928b4d3f67e01b0a", a, pubk)
if err != nil {
fmt.Println(err)
}
}
// Load private key from base64
func LoadPrivateKeyBase64(base64key string) (*rsa.PrivateKey, error) {
keybytes, err := base64.StdEncoding.DecodeString(base64key)
if err != nil {
return nil, fmt.Errorf("base64 decode failed, error=%s\n", err.Error())
}
privatekey, err := x509.ParsePKCS1PrivateKey(keybytes)
if err != nil {
return nil, errors.New("parse private key error!")
}
return privatekey, nil
}
func LoadPublicKeyBase64(base64key string) (*rsa.PublicKey, error) {
keybytes, err := base64.StdEncoding.DecodeString(base64key)
if err != nil {
return nil, fmt.Errorf("base64 decode failed, error=%s\n", err.Error())
}
pubkeyinterface, err := x509.ParsePKIXPublicKey(keybytes)
if err != nil {
return nil, err
}
publickey := pubkeyinterface.(*rsa.PublicKey)
return publickey, nil
}
//私钥签名
func RsaSignWithSha1Hex(data string, prvKey *rsa.PrivateKey) (string, error) {
h := sha256.New()
h.Write([]byte([]byte(data)))
hash := h.Sum(nil)
signature, err := rsa.SignPKCS1v15(rand.Reader, prvKey, crypto.SHA256, hash[:])
if err != nil {
fmt.Printf("Error from signing: %s\n", err)
return "", err
}
out := hex.EncodeToString(signature)
fmt.Println(signature)
return out, nil
}
func RsaVerySignWithSha1Base64(originalData, signData string, pubKey *rsa.PublicKey) error{
sign, err := hex.DecodeString(signData)
fmt.Println(sign)
if err != nil {
return err
}
hash := sha256.New()
hashed_int, err1 := hash.Write([]byte(originalData))
if err1 != nil {
fmt.Println(err1)
return err1
}
fmt.Println("hash int: ", hashed_int)
fmt.Println(hash.Sum(nil))
return rsa.VerifyPKCS1v15(pubKey, crypto.SHA256, hash.Sum(nil), sign)
}