Skip to content

Commit

Permalink
feat: parameterize rsa sign/verify hash algorithm (#23)
Browse files Browse the repository at this point in the history
* feat: parameterize rsa sign/verify hash algorithm

* doc: update README

Co-authored-by: jet <jet@shundaojia.com>
  • Loading branch information
fosmjo and jet authored Oct 20, 2022
1 parent f9f06f8 commit 1af64c4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ openssl.RSAGeneratePublicKey(priKey []byte, out io.Writer)
openssl.RSAEncrypt(src, pubKey []byte) ([]byte, error)
openssl.RSADecrypt(src, priKey []byte) ([]byte, error)

openssl.RSASign(src []byte, priKey []byte) ([]byte, error)
openssl.RSAVerify(src, sign, pubKey []byte) error
openssl.RSASign(src []byte, priKey []byte, hash crypto.Hash) ([]byte, error)
openssl.RSAVerify(src, sign, pubKey []byte, hash crypto.Hash) error
```

## License
Expand Down
35 changes: 17 additions & 18 deletions rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/pem"
"errors"
Expand All @@ -28,7 +27,7 @@ func RSAGenerateKey(bits int, out io.Writer) error {
// RSAGeneratePublicKey generate RSA public key
func RSAGeneratePublicKey(priKey []byte, out io.Writer) error {
block, _ := pem.Decode(priKey)
if block == nil{
if block == nil {
return errors.New("key is invalid format")
}

Expand All @@ -51,7 +50,7 @@ func RSAGeneratePublicKey(priKey []byte, out io.Writer) error {
// RSAEncrypt RSA encrypt
func RSAEncrypt(src, pubKey []byte) ([]byte, error) {
block, _ := pem.Decode(pubKey)
if block == nil{
if block == nil {
return nil, errors.New("key is invalid format")
}

Expand All @@ -77,7 +76,7 @@ func RSAEncrypt(src, pubKey []byte) ([]byte, error) {
// RSADecrypt RSA decrypt
func RSADecrypt(src, priKey []byte) ([]byte, error) {
block, _ := pem.Decode(priKey)
if block == nil{
if block == nil {
return nil, errors.New("key is invalid format")
}

Expand All @@ -95,10 +94,10 @@ func RSADecrypt(src, priKey []byte) ([]byte, error) {
return dst, nil
}

// RSASign RSA sign, use crypto.SHA256
func RSASign(src []byte, priKey []byte) ([]byte, error) {
// RSASign RSA sign
func RSASign(src []byte, priKey []byte, hash crypto.Hash) ([]byte, error) {
block, _ := pem.Decode(priKey)
if block == nil{
if block == nil {
return nil, errors.New("key is invalid format")
}

Expand All @@ -108,25 +107,25 @@ func RSASign(src []byte, priKey []byte) ([]byte, error) {
return nil, err
}

hash := sha256.New()
_, err = hash.Write(src)
h := hash.New()
_, err = h.Write(src)
if err != nil {
return nil, err
}

bytes := hash.Sum(nil)
sign, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, bytes)
bytes := h.Sum(nil)
sign, err := rsa.SignPKCS1v15(rand.Reader, privateKey, hash, bytes)
if err != nil {
return nil, err
}

return sign, nil
}

// RSAVerify RSA Verify
func RSAVerify(src, sign, pubKey []byte) error {
// RSAVerify RSA verify
func RSAVerify(src, sign, pubKey []byte, hash crypto.Hash) error {
block, _ := pem.Decode(pubKey)
if block == nil{
if block == nil {
return errors.New("key is invalid format")
}

Expand All @@ -141,13 +140,13 @@ func RSAVerify(src, sign, pubKey []byte) error {
return errors.New("the kind of key is not a rsa.PublicKey")
}

hash := sha256.New()
_, err = hash.Write(src)
h := hash.New()
_, err = h.Write(src)
if err != nil {
return err
}

bytes := hash.Sum(nil)
bytes := h.Sum(nil)

return rsa.VerifyPKCS1v15(publicKey, crypto.SHA256, bytes, sign)
return rsa.VerifyPKCS1v15(publicKey, hash, bytes, sign)
}
10 changes: 6 additions & 4 deletions rsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package openssl

import (
"bytes"
"crypto"
"encoding/base64"
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestRSAEncrypt(t *testing.T) {
Expand Down Expand Up @@ -43,10 +45,10 @@ func TestRSASign(t *testing.T) {
t.Logf("public key: %s\n", pubBuf.Bytes())

src := []byte("123456")
sign, err := RSASign(src, priBuf.Bytes())
sign, err := RSASign(src, priBuf.Bytes(), crypto.SHA256)
assert.NoError(t, err)
t.Logf("sign out: %s\n", base64.RawStdEncoding.EncodeToString(sign))

err = RSAVerify(src, sign, pubBuf.Bytes())
err = RSAVerify(src, sign, pubBuf.Bytes(), crypto.SHA256)
assert.NoError(t, err)
}
}

0 comments on commit 1af64c4

Please sign in to comment.