Skip to content

Commit f138978

Browse files
committed
Merge branch 'master' of github.com:forgoer/openssl
2 parents 3e4b280 + 1af64c4 commit f138978

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ openssl.RSAGeneratePublicKey(priKey []byte, out io.Writer)
9595
openssl.RSAEncrypt(src, pubKey []byte) ([]byte, error)
9696
openssl.RSADecrypt(src, priKey []byte) ([]byte, error)
9797

98-
openssl.RSASign(src []byte, priKey []byte) ([]byte, error)
99-
openssl.RSAVerify(src, sign, pubKey []byte) error
98+
openssl.RSASign(src []byte, priKey []byte, hash crypto.Hash) ([]byte, error)
99+
openssl.RSAVerify(src, sign, pubKey []byte, hash crypto.Hash) error
100100
```
101101

102102
### HMAC-SHA

rsa.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"crypto"
55
"crypto/rand"
66
"crypto/rsa"
7-
"crypto/sha256"
87
"crypto/x509"
98
"encoding/pem"
109
"errors"
@@ -28,7 +27,7 @@ func RSAGenerateKey(bits int, out io.Writer) error {
2827
// RSAGeneratePublicKey generate RSA public key
2928
func RSAGeneratePublicKey(priKey []byte, out io.Writer) error {
3029
block, _ := pem.Decode(priKey)
31-
if block == nil{
30+
if block == nil {
3231
return errors.New("key is invalid format")
3332
}
3433

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

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

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

98-
// RSASign RSA sign, use crypto.SHA256
99-
func RSASign(src []byte, priKey []byte) ([]byte, error) {
97+
// RSASign RSA sign
98+
func RSASign(src []byte, priKey []byte, hash crypto.Hash) ([]byte, error) {
10099
block, _ := pem.Decode(priKey)
101-
if block == nil{
100+
if block == nil {
102101
return nil, errors.New("key is invalid format")
103102
}
104103

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

111-
hash := sha256.New()
112-
_, err = hash.Write(src)
110+
h := hash.New()
111+
_, err = h.Write(src)
113112
if err != nil {
114113
return nil, err
115114
}
116115

117-
bytes := hash.Sum(nil)
118-
sign, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, bytes)
116+
bytes := h.Sum(nil)
117+
sign, err := rsa.SignPKCS1v15(rand.Reader, privateKey, hash, bytes)
119118
if err != nil {
120119
return nil, err
121120
}
122121

123122
return sign, nil
124123
}
125124

126-
// RSAVerify RSA Verify
127-
func RSAVerify(src, sign, pubKey []byte) error {
125+
// RSAVerify RSA verify
126+
func RSAVerify(src, sign, pubKey []byte, hash crypto.Hash) error {
128127
block, _ := pem.Decode(pubKey)
129-
if block == nil{
128+
if block == nil {
130129
return errors.New("key is invalid format")
131130
}
132131

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

144-
hash := sha256.New()
145-
_, err = hash.Write(src)
143+
h := hash.New()
144+
_, err = h.Write(src)
146145
if err != nil {
147146
return err
148147
}
149148

150-
bytes := hash.Sum(nil)
149+
bytes := h.Sum(nil)
151150

152-
return rsa.VerifyPKCS1v15(publicKey, crypto.SHA256, bytes, sign)
151+
return rsa.VerifyPKCS1v15(publicKey, hash, bytes, sign)
153152
}

rsa_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package openssl
22

33
import (
44
"bytes"
5+
"crypto"
56
"encoding/base64"
6-
"github.com/stretchr/testify/assert"
77
"testing"
8+
9+
"github.com/stretchr/testify/assert"
810
)
911

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

4547
src := []byte("123456")
46-
sign, err := RSASign(src, priBuf.Bytes())
48+
sign, err := RSASign(src, priBuf.Bytes(), crypto.SHA256)
4749
assert.NoError(t, err)
4850
t.Logf("sign out: %s\n", base64.RawStdEncoding.EncodeToString(sign))
4951

50-
err = RSAVerify(src, sign, pubBuf.Bytes())
52+
err = RSAVerify(src, sign, pubBuf.Bytes(), crypto.SHA256)
5153
assert.NoError(t, err)
52-
}
54+
}

0 commit comments

Comments
 (0)