Skip to content

Commit 53ad0f5

Browse files
committed
完善
1 parent 0878c12 commit 53ad0f5

File tree

11 files changed

+216
-216
lines changed

11 files changed

+216
-216
lines changed

FUTURE.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
* [ ] 增加 cmd 包,提供二进制 cli 使用
66
* [ ] 支持 SM3 国标算法
77
* [ ] 支持 SM4 国标算法
8+
* [ ] 支持更多的散列算法
89

910
### v0.5.x
1011

1112
* [ ] 支持 ECDSA 算法
1213
* [ ] 支持 EdDSA 算法
13-
* [ ] 支持更多的散列算法
1414
* [x] 重构代码,优化使用方式
15-
* [ ] 继续完善单元测试,提升覆盖率到 90% 以上
15+
* [ ] 提升覆盖率到 90% 以上
1616

1717
### v0.4.x
1818

README.en.md

Lines changed: 73 additions & 85 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 73 additions & 85 deletions
Large diffs are not rendered by default.

_examples/rsa.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ func main() {
4848
// Use the private key to sign data.
4949
digest := hash.SHA256(data)
5050

51-
sign, err := privateKey.SignPSS(digest, 0, rsa.WithHex())
51+
sign, err := privateKey.SignPSS(digest, rsa.WithHex())
5252
if err != nil {
5353
panic(err)
5454
}
5555

5656
fmt.Printf("sign: %s\n", sign)
5757

5858
// Use the public key to verify the sign.
59-
err = publicKey.VerifyPSS(digest, sign, 0, rsa.WithHex())
59+
err = publicKey.VerifyPSS(digest, sign, rsa.WithHex())
6060
if err != nil {
6161
panic(err)
6262
}

_examples/rsa_test.go

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func BenchmarkRSA_EncryptOAEP(b *testing.B) {
5353

5454
// go test -v -bench=^BenchmarkRSA_DecryptPKCS1v15$ -benchtime=1s rsa_test.go
5555
func BenchmarkRSA_DecryptPKCS1v15(b *testing.B) {
56-
privateKey, err := rsa.LoadPublicKey("rsa.key")
56+
privateKey, err := rsa.LoadPrivateKey("rsa.key")
5757
if err != nil {
5858
b.Fatal(err)
5959
}
@@ -81,7 +81,7 @@ func BenchmarkRSA_DecryptPKCS1v15(b *testing.B) {
8181

8282
// go test -v -bench=^BenchmarkRSA_DecryptPKCS1v15SessionKey$ -benchtime=1s rsa_test.go
8383
func BenchmarkRSA_DecryptPKCS1v15SessionKey(b *testing.B) {
84-
privateKey, err := rsa.LoadPublicKey("rsa.key")
84+
privateKey, err := rsa.LoadPrivateKey("rsa.key")
8585
if err != nil {
8686
b.Fatal(err)
8787
}
@@ -95,14 +95,14 @@ func BenchmarkRSA_DecryptPKCS1v15SessionKey(b *testing.B) {
9595

9696
encrypt, err := publicKey.EncryptPKCS1v15(sessionKey)
9797
if err != nil {
98-
t.Fatal(err)
98+
b.Fatal(err)
9999
}
100100

101101
b.ReportAllocs()
102102
b.ResetTimer()
103103

104104
for i := 0; i < b.N; i++ {
105-
err := privateKey.DecryptPKCS1v15SessionKey(encrypt, sessionKey, nil)
105+
err := privateKey.DecryptPKCS1v15SessionKey(encrypt, sessionKey)
106106
if err != nil {
107107
b.Fatal(err)
108108
}
@@ -111,7 +111,7 @@ func BenchmarkRSA_DecryptPKCS1v15SessionKey(b *testing.B) {
111111

112112
// go test -v -bench=^BenchmarkRSA_DecryptOAEP$ -benchtime=1s rsa_test.go
113113
func BenchmarkRSA_DecryptOAEP(b *testing.B) {
114-
privateKey, err := rsa.LoadPublicKey("rsa.key")
114+
privateKey, err := rsa.LoadPrivateKey("rsa.key")
115115
if err != nil {
116116
b.Fatal(err)
117117
}
@@ -137,50 +137,49 @@ func BenchmarkRSA_DecryptOAEP(b *testing.B) {
137137
}
138138
}
139139

140-
// go test -v -bench=^BenchmarkRSA_SignPSS$ -benchtime=1s rsa_test.go
141-
func BenchmarkRSA_SignPSS(b *testing.B) {
142-
privateKey, err := rsa.LoadPublicKey("rsa.key")
140+
// go test -v -bench=^BenchmarkRSA_SignPKCS1v15$ -benchtime=1s rsa_test.go
141+
func BenchmarkRSA_SignPKCS1v15(b *testing.B) {
142+
privateKey, err := rsa.LoadPrivateKey("rsa.key")
143143
if err != nil {
144144
b.Fatal(err)
145145
}
146146

147-
digest := hash.SHA256(rsaBenchData)
148-
saltLength := 4
147+
hashed := hash.SHA256(rsaBenchData)
149148

150149
b.ReportAllocs()
151150
b.ResetTimer()
152151

153152
for i := 0; i < b.N; i++ {
154-
_, err := privateKey.SignPSS(digest, saltLength)
153+
_, err := privateKey.SignPKCS1v15(hashed)
155154
if err != nil {
156155
b.Fatal(err)
157156
}
158157
}
159158
}
160159

161-
// go test -v -bench=^BenchmarkRSA_SignPKCS1v15$ -benchtime=1s rsa_test.go
162-
func BenchmarkRSA_SignPKCS1v15(b *testing.B) {
163-
privateKey, err := rsa.LoadPublicKey("rsa.key")
160+
// go test -v -bench=^BenchmarkRSA_SignPSS$ -benchtime=1s rsa_test.go
161+
func BenchmarkRSA_SignPSS(b *testing.B) {
162+
privateKey, err := rsa.LoadPrivateKey("rsa.key")
164163
if err != nil {
165164
b.Fatal(err)
166165
}
167166

168-
hashed := hash.SHA256(rsaBenchData)
167+
digest := hash.SHA256(rsaBenchData)
169168

170169
b.ReportAllocs()
171170
b.ResetTimer()
172171

173172
for i := 0; i < b.N; i++ {
174-
_, err := privateKey.SignPKCS1v15(hashed)
173+
_, err := privateKey.SignPSS(digest)
175174
if err != nil {
176175
b.Fatal(err)
177176
}
178177
}
179178
}
180179

181-
// go test -v -bench=^BenchmarkRSA_VerifyPSS$ -benchtime=1s rsa_test.go
182-
func BenchmarkRSA_VerifyPSS(b *testing.B) {
183-
privateKey, err := rsa.LoadPublicKey("rsa.key")
180+
// go test -v -bench=^BenchmarkRSA_VerifyPKCS1v15$ -benchtime=1s rsa_test.go
181+
func BenchmarkRSA_VerifyPKCS1v15(b *testing.B) {
182+
privateKey, err := rsa.LoadPrivateKey("rsa.key")
184183
if err != nil {
185184
b.Fatal(err)
186185
}
@@ -190,10 +189,9 @@ func BenchmarkRSA_VerifyPSS(b *testing.B) {
190189
b.Fatal(err)
191190
}
192191

193-
digest := hash.SHA256(rsaBenchData)
194-
saltLength := 4
192+
hashed := hash.SHA256(rsaBenchData)
195193

196-
sign, err := privateKey.SignPSS(digest, saltLength)
194+
sign, err := privateKey.SignPKCS1v15(hashed)
197195
if err != nil {
198196
b.Fatal(err)
199197
}
@@ -202,16 +200,16 @@ func BenchmarkRSA_VerifyPSS(b *testing.B) {
202200
b.ResetTimer()
203201

204202
for i := 0; i < b.N; i++ {
205-
err = publicKey.VerifyPSS(digest, sign, saltLength)
203+
err = publicKey.VerifyPKCS1v15(hashed, sign)
206204
if err != nil {
207205
b.Fatal(err)
208206
}
209207
}
210208
}
211209

212-
// go test -v -bench=^BenchmarkRSA_VerifyPKCS1v15$ -benchtime=1s rsa_test.go
213-
func BenchmarkRSA_VerifyPKCS1v15(b *testing.B) {
214-
privateKey, err := rsa.LoadPublicKey("rsa.key")
210+
// go test -v -bench=^BenchmarkRSA_VerifyPSS$ -benchtime=1s rsa_test.go
211+
func BenchmarkRSA_VerifyPSS(b *testing.B) {
212+
privateKey, err := rsa.LoadPrivateKey("rsa.key")
215213
if err != nil {
216214
b.Fatal(err)
217215
}
@@ -221,9 +219,9 @@ func BenchmarkRSA_VerifyPKCS1v15(b *testing.B) {
221219
b.Fatal(err)
222220
}
223221

224-
hashed := hash.SHA256(rsaBenchData)
222+
digest := hash.SHA256(rsaBenchData)
225223

226-
sign, err := privateKey.SignPKCS1v15(hashed)
224+
sign, err := privateKey.SignPSS(digest)
227225
if err != nil {
228226
b.Fatal(err)
229227
}
@@ -232,7 +230,7 @@ func BenchmarkRSA_VerifyPKCS1v15(b *testing.B) {
232230
b.ResetTimer()
233231

234232
for i := 0; i < b.N; i++ {
235-
err = publicKey.VerifyPKCS1v15(hashed, sign)
233+
err = publicKey.VerifyPSS(digest, sign)
236234
if err != nil {
237235
b.Fatal(err)
238236
}

_icons/coverage.svg

Lines changed: 2 additions & 2 deletions
Loading

rsa/options.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ type Config struct {
8686
random io.Reader
8787
hash hash.Hash
8888
cryptoHash crypto.Hash
89+
saltLength int
8990
}
9091

9192
func newConfig() *Config {
@@ -94,6 +95,7 @@ func newConfig() *Config {
9495
random: rand.Reader,
9596
hash: sha256.New(),
9697
cryptoHash: crypto.SHA256,
98+
saltLength: rsa.PSSSaltLengthAuto,
9799
}
98100

99101
return conf
@@ -143,3 +145,10 @@ func WithCryptoHash(hash crypto.Hash) Option {
143145
conf.cryptoHash = hash
144146
}
145147
}
148+
149+
// WithSalt sets salt length to config.
150+
func WithSalt(saltLength int) Option {
151+
return func(conf *Config) {
152+
conf.saltLength = saltLength
153+
}
154+
}

rsa/options_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,14 @@ func TestKeyConfig(t *testing.T) {
6262
// go test -v -cover -run=^TestConfig$
6363
func TestConfig(t *testing.T) {
6464
hash := sha256.New()
65+
saltLength := 32
6566

6667
opts := []Option{
6768
WithHex(),
6869
WithRandom(rand.Reader),
6970
WithHash(hash),
7071
WithCryptoHash(crypto.SHA256),
72+
WithSalt(saltLength),
7173
}
7274

7375
conf := newConfig().Apply(opts...)
@@ -99,6 +101,10 @@ func TestConfig(t *testing.T) {
99101
}
100102

101103
if conf.cryptoHash != crypto.SHA256 {
102-
t.Fatalf("conf.cryptoHash %d != crypto.SHA256 %d", conf.cryptoHash, crypto.SHA256)
104+
t.Fatalf("got %d != expect %d", conf.cryptoHash, crypto.SHA256)
105+
}
106+
107+
if conf.saltLength != saltLength {
108+
t.Fatalf("got %d != expect %d", conf.saltLength, saltLength)
103109
}
104110
}

rsa/private.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package rsa
66

77
import (
88
"crypto/rsa"
9+
"fmt"
910
)
1011

1112
type PrivateKey struct {
@@ -62,9 +63,14 @@ func (pk PrivateKey) SignPKCS1v15(hashed []byte, opts ...Option) ([]byte, error)
6263
}
6364

6465
// SignPSS signs digest with pss.
65-
func (pk PrivateKey) SignPSS(digest []byte, saltLength int, opts ...Option) ([]byte, error) {
66+
func (pk PrivateKey) SignPSS(digest []byte, opts ...Option) ([]byte, error) {
6667
conf := newConfig().Apply(opts...)
67-
pssOpts := &rsa.PSSOptions{Hash: conf.cryptoHash, SaltLength: saltLength}
68+
69+
if !conf.cryptoHash.Available() {
70+
return nil, fmt.Errorf("cryptox/rsa: crypto hash %+v isn't available", conf.cryptoHash)
71+
}
72+
73+
pssOpts := &rsa.PSSOptions{Hash: conf.cryptoHash, SaltLength: conf.saltLength}
6874

6975
sign, err := rsa.SignPSS(conf.random, pk.key, conf.cryptoHash, digest, pssOpts)
7076
if err != nil {

rsa/private_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,34 +158,34 @@ func TestSignVerifyPSS(t *testing.T) {
158158
digest := sum[:]
159159

160160
// None
161-
sign, err := privateKey.SignPSS(digest, 0)
161+
sign, err := privateKey.SignPSS(digest)
162162
if err != nil {
163163
t.Fatal(err)
164164
}
165165

166-
err = publicKey.VerifyPSS(digest, sign, 0)
166+
err = publicKey.VerifyPSS(digest, sign)
167167
if err != nil {
168168
t.Fatal(err)
169169
}
170170

171171
// Hex
172-
sign, err = privateKey.SignPSS(digest, 0, WithHex())
172+
sign, err = privateKey.SignPSS(digest, WithHex())
173173
if err != nil {
174174
t.Fatal(err)
175175
}
176176

177-
err = publicKey.VerifyPSS(digest, sign, 0, WithHex())
177+
err = publicKey.VerifyPSS(digest, sign, WithHex())
178178
if err != nil {
179179
t.Fatal(err)
180180
}
181181

182182
// Base64
183-
sign, err = privateKey.SignPSS(digest, 0, WithBase64())
183+
sign, err = privateKey.SignPSS(digest, WithBase64())
184184
if err != nil {
185185
t.Fatal(err)
186186
}
187187

188-
err = publicKey.VerifyPSS(digest, sign, 0, WithBase64())
188+
err = publicKey.VerifyPSS(digest, sign, WithBase64())
189189
if err != nil {
190190
t.Fatal(err)
191191
}

0 commit comments

Comments
 (0)