Golang-RSA2 签名及验签

const (
    // 私钥 PEMBEGIN 开头
    PEMBEGIN = "-----BEGIN RSA PRIVATE KEY-----\n"
    // 私钥 PEMEND 结尾
    PEMEND = "\n-----END RSA PRIVATE KEY-----"
    // 公钥 PEMBEGIN 开头
    PUBPEMBEGIN = "-----BEGIN PUBLIC KEY-----\n"
    // 公钥 PEMEND 结尾
    PUBPEMEND = "\n-----END PUBLIC KEY-----"
)
// Rsa2Sign RSA2私钥签名
func Rsa2Sign(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)
}

// ParsePrivateKey 私钥验证
func ParsePrivateKey(privateKey string) (*rsa.PrivateKey, error) {
    privateKey = FormatPrivateKey(privateKey)
    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
}

// FormatPrivateKey 组装私钥
func FormatPrivateKey(privateKey string) string {
    if !strings.HasPrefix(privateKey, PEMBEGIN) {
        privateKey = PEMBEGIN + privateKey
    }
    if !strings.HasSuffix(privateKey, PEMEND) {
        privateKey = privateKey + PEMEND
    }
    return privateKey
}

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

// ParsePublicKey 公钥验证
func ParsePublicKey(publicKey string) (*rsa.PublicKey, error) {
    publicKey = FormatPublicKey(publicKey)
    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
}

// FormatPublicKey 组装公钥
func FormatPublicKey(publicKey string) string {
    if !strings.HasPrefix(publicKey, PUBPEMBEGIN) {
        publicKey = PUBPEMBEGIN + publicKey
    }
    if !strings.HasSuffix(publicKey, PUBPEMEND) {
        publicKey = publicKey + PUBPEMEND
    }
    return publicKey
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值