golang实现RSA2的签名与验签函数

2022-11-12 11:40:15 浏览数 (3)

使用非对称加密算法,实现签名与验签

代码语言:javascript复制
package tools

import (
    "crypto"
    "crypto/rand"
    "crypto/rsa"
    "crypto/sha256"
    "crypto/x509"
    "encoding/base64"
    "encoding/pem"
    "errors"
)

// RSA2私钥签名
func Rsa2PriSign(signContent string, privateKey string, hash crypto.Hash) string {
    shaNew := hash.New()
    shaNew.Write([]byte(signContent))
    hashed := shaNew.Sum(nil)
    priKey, err := ParsePrivateKey(privateKey)
    if err != nil {
        return ""
    }

    signature, err := rsa.SignPKCS1v15(rand.Reader, priKey, hash, hashed)
    if err != nil {
        return ""
    }
    return base64.StdEncoding.EncodeToString(signature)
}

// 解析私钥
func ParsePrivateKey(privateKey string) (*rsa.PrivateKey, error) {
    block, _ := pem.Decode([]byte(privateKey))
    if block == nil {
        return nil, errors.New("私钥信息错误!")
    }
    priKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
    if err != nil {
        return nil, err
    }
    return priKey, nil
}

// RSA2公钥验证签名
func Rsa2PubCheckSign(signContent, sign, publicKey string, hash crypto.Hash) bool {
    hashed := sha256.Sum256([]byte(signContent))
    pubKey, err := ParsePublicKey(publicKey)
    if err != nil {
        return false
    }
    sig, _ := base64.StdEncoding.DecodeString(sign)
    err = rsa.VerifyPKCS1v15(pubKey, hash, hashed[:], sig)
    if err != nil {
        return false
    }
    return true
}

// 解析公钥
func ParsePublicKey(publicKey string) (*rsa.PublicKey, error) {
    block, _ := pem.Decode([]byte(publicKey))
    if block == nil {
        return nil, errors.New("公钥信息错误!")
    }
    pubKey, err := x509.ParsePKIXPublicKey(block.Bytes)
    if err != nil {
        return nil, err
    }
    return pubKey.(*rsa.PublicKey), nil
}

测试用例

代码语言:javascript复制
package tools

import (
    "crypto"
    "log"
    "testing"
)

const (
    // 私钥
    PRIVATE_KEY = `
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAvSa9KsScy ZuFTiPLE7ZwMacmxmA6A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cv RNqu9D4BwcXBaNBMUkD6M
/P unUkI8Ukdy70yHfKPT1N5FfHGN8thqZv VQ8HQkSS6MY7vcHK06o6H2xpUMvA
5zDuI eHtytTFd/snsPtbQKBgCFM8TklydqMtTXv9ZG767PtUlJTjzIUVM5kYLP3
tXSzVZwSr8e/m19dmgz4uwDUN9eiHKwWOiilOfAxGBtd lHIgDiBOWDmDdFgnkjW
qY0 WTjAmp7WhufIM9Ah35IX3v1c1m5fZ1HZRTQBTw4k2A9zI/UpbIbv68 7h/ks
qvCDAoGAF0jwXqwZu0CJB ojr6dawYXNalEKH7bMPyItdHK4PqWcftHjYXQthnQC
uOeS4tEa3nfahGhp68U9N18L3h3gkkHqIGJvPzAEIjaIeghkQ73RinA/94ZlUBm4
jVGL8OEtfrm3O6ukVFTaiBMXPwAN4e3qfkXuyRepN0DjUthymes=
-----END RSA PRIVATE KEY-----
`
    // 私钥
    PUBLIC_KEY = `
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvSa9KsScy ZuFTiPLE7Z
wMacmxmA6A8fu0jiPluliLwutgROTTZp3g9HmW1CuYt ZoQH9t0JzHHNFu9yCetJ
dt5TrpQx9bBovTsh8h/ZQOPsyo0pnKqi5G3x1Azvbzs6yTU8voz6xVT90/DVJcd7
1osTsn 7ajEZccfa35tl5DBdF95thWeyMz5p6ayFMUEU10HBUYmYTUzi4dvXcZUZ
vLOIOPMHoCTXcX98EmGqUvsLWpx8mscaA5llZMg ZXnT5OH HW9ncfz7DqXiON3y
o1BrNPdmN Y4SI9dWrADUzGJO6u58OwncCphUsYqnlG2jd4n0JhjpA2WGD4c6zqJ
JQIDAQAB
-----END PUBLIC KEY-----
`
)

func TestRsa2Rsa2PriSign(t *testing.T) {
    //原内容
    str := "F78691A5-2963-42D2-A0C7-3A4F31CF5EB6"
    //生成签名
    sig := Rsa2PriSign(str, PRIVATE_KEY, crypto.SHA256)
    log.Println(sig)
    //验证原内容与签名是否一致
    res := Rsa2PubCheckSign(str, sig, PUBLIC_KEY, crypto.SHA256)
    log.Println(res)
}

 验证成功!

0 人点赞