From b315397ac6d5f71312d0cc54a8f151d8bb4b58d4 Mon Sep 17 00:00:00 2001 From: starainrt Date: Fri, 13 Aug 2021 11:06:07 +0800 Subject: [PATCH] bug fix --- crypto.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/crypto.go b/crypto.go index a598ce3..92e60b0 100644 --- a/crypto.go +++ b/crypto.go @@ -947,11 +947,11 @@ func RSADecrypt(data, private []byte, password string) ([]byte, error) { } // RSASign RSA私钥签名加密 -func RSASign(hashdata, private []byte, password string, hashtype crypto.Hash) ([]byte, error) { +func RSASign(msg, priKey []byte, password string, hashType crypto.Hash) ([]byte, error) { var prikey *rsa.PrivateKey var err error var bytes []byte - blk, _ := pem.Decode(private) + blk, _ := pem.Decode(priKey) if blk == nil { return []byte{}, errors.New("private key error!") } @@ -972,12 +972,17 @@ func RSASign(hashdata, private []byte, password string, hashtype crypto.Hash) ([ } prikey = tmp.(*rsa.PrivateKey) } - return rsa.SignPKCS1v15(rand.Reader, prikey, hashtype, hashdata) + hashMethod := hashType.New() + _, err = hashMethod.Write(msg) + if err != nil { + return nil, err + } + return rsa.SignPKCS1v15(rand.Reader, prikey, hashType, hashMethod.Sum(nil)) } // RSAVerify RSA公钥签名验证 -func RSAVerify(data, hashdata, public []byte, hashtype crypto.Hash) error { - blk, _ := pem.Decode(public) +func RSAVerify(data, msg, pubKey []byte, hashType crypto.Hash) error { + blk, _ := pem.Decode(pubKey) if blk == nil { return errors.New("public key error!") } @@ -985,7 +990,12 @@ func RSAVerify(data, hashdata, public []byte, hashtype crypto.Hash) error { if err != nil { return err } - return rsa.VerifyPKCS1v15(pubkey.(*rsa.PublicKey), hashtype, hashdata, data) + hashMethod := hashType.New() + _, err = hashMethod.Write(msg) + if err != nil { + return err + } + return rsa.VerifyPKCS1v15(pubkey.(*rsa.PublicKey), hashType, hashMethod.Sum(nil), data) } // VicqueEncodeV1 Best!