Skip to content

Commit 9456bb2

Browse files
authored
Merge pull request #4 from FishGoddess/develop
v0.4.2-alpha
2 parents eebb7b0 + 1426329 commit 9456bb2

File tree

11 files changed

+319
-179
lines changed

11 files changed

+319
-179
lines changed

FUTURE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
* [x] 调整 hash 包使用姿势
1515
* [x] 大规模重构,优化代码使用
16-
* [ ] 性能优化,尝试减少内存分配
16+
* [x] 性能优化,尝试减少内存分配
1717
* [ ] 继续完善单元测试,提升覆盖率到 90%
1818

1919
### v0.3.x

HISTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## 📜 历史版本的特性介绍 (Features in old versions)
22

3+
### v0.4.2-alpha
4+
5+
> 此版本发布于 2024-05-27
6+
7+
* 性能优化,尝试减少内存分配
8+
39
### v0.4.1-alpha
410

511
> 此版本发布于 2024-05-22

README.en.md

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

README.md

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

aes/aes.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func EncryptECB(key cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (c
2828
return nil, err
2929
}
3030

31+
bs = bs.Clone()
3132
src := padding.Padding(bs, blockSize)
3233
dst := src.Clone()
3334

@@ -80,6 +81,7 @@ func EncryptCBC(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
8081
return nil, err
8182
}
8283

84+
bs = bs.Clone()
8385
src := padding.Padding(bs, blockSize)
8486
dst := src.Clone()
8587

@@ -106,6 +108,7 @@ func EncryptCFB(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
106108
return nil, err
107109
}
108110

111+
bs = bs.Clone()
109112
src := padding.Padding(bs, blockSize)
110113
dst := src.Clone()
111114

@@ -132,6 +135,7 @@ func EncryptOFB(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
132135
return nil, err
133136
}
134137

138+
bs = bs.Clone()
135139
src := padding.Padding(bs, blockSize)
136140
dst := src.Clone()
137141

@@ -158,6 +162,7 @@ func EncryptCTR(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
158162
return nil, err
159163
}
160164

165+
bs = bs.Clone()
161166
src := padding.Padding(bs, blockSize)
162167
dst := src.Clone()
163168

@@ -179,7 +184,6 @@ func DecryptCTR(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
179184
}
180185

181186
// EncryptGCM uses gcm mode to encrypt bs.
182-
// NOTICE: This is an experimental function, and we haven't tested it enough yet, so be careful when using it.
183187
func EncryptGCM(key cryptox.Bytes, nonce cryptox.Bytes, additional cryptox.Bytes, bs cryptox.Bytes) (cryptox.Bytes, error) {
184188
block, _, err := newBlock(key)
185189
if err != nil {
@@ -199,7 +203,6 @@ func EncryptGCM(key cryptox.Bytes, nonce cryptox.Bytes, additional cryptox.Bytes
199203
}
200204

201205
// DecryptGCM uses gcm mode to decrypt bs.
202-
// NOTICE: This is an experimental function, and we haven't tested it enough yet, so be careful when using it.
203206
func DecryptGCM(key cryptox.Bytes, nonce cryptox.Bytes, additional cryptox.Bytes, bs cryptox.Bytes) (cryptox.Bytes, error) {
204207
block, _, err := newBlock(key)
205208
if err != nil {

bytes_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ func TestParseHex(t *testing.T) {
113113
}
114114
}
115115

116-
// go test -v -cover -count=1 -test.cpu=1 -run=^TestFromBase64$
117-
func TestFromBase64(t *testing.T) {
116+
// go test -v -cover -count=1 -test.cpu=1 -run=^TestParseBase64$
117+
func TestParseBase64(t *testing.T) {
118118
cases := map[string]string{
119119
"": "",
120120
"MTIz": "123",

des/des.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func EncryptECB(key cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (c
2828
return nil, err
2929
}
3030

31+
bs = bs.Clone()
3132
src := padding.Padding(bs, blockSize)
3233
dst := src.Clone()
3334

@@ -80,6 +81,7 @@ func EncryptCBC(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
8081
return nil, err
8182
}
8283

84+
bs = bs.Clone()
8385
src := padding.Padding(bs, blockSize)
8486
dst := src.Clone()
8587

@@ -106,6 +108,7 @@ func EncryptCFB(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
106108
return nil, err
107109
}
108110

111+
bs = bs.Clone()
109112
src := padding.Padding(bs, blockSize)
110113
dst := src.Clone()
111114

@@ -132,6 +135,7 @@ func EncryptOFB(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
132135
return nil, err
133136
}
134137

138+
bs = bs.Clone()
135139
src := padding.Padding(bs, blockSize)
136140
dst := src.Clone()
137141

@@ -158,6 +162,7 @@ func EncryptCTR(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
158162
return nil, err
159163
}
160164

165+
bs = bs.Clone()
161166
src := padding.Padding(bs, blockSize)
162167
dst := src.Clone()
163168

des/triple_des.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func EncryptECBTriple(key cryptox.Bytes, padding cryptox.Padding, bs cryptox.Byt
2828
return nil, err
2929
}
3030

31+
bs = bs.Clone()
3132
src := padding.Padding(bs, blockSize)
3233
dst := src.Clone()
3334

@@ -80,6 +81,7 @@ func EncryptCBCTriple(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Paddi
8081
return nil, err
8182
}
8283

84+
bs = bs.Clone()
8385
src := padding.Padding(bs, blockSize)
8486
dst := src.Clone()
8587

@@ -106,6 +108,7 @@ func EncryptCFBTriple(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Paddi
106108
return nil, err
107109
}
108110

111+
bs = bs.Clone()
109112
src := padding.Padding(bs, blockSize)
110113
dst := src.Clone()
111114

@@ -132,6 +135,7 @@ func EncryptOFBTriple(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Paddi
132135
return nil, err
133136
}
134137

138+
bs = bs.Clone()
135139
src := padding.Padding(bs, blockSize)
136140
dst := src.Clone()
137141

@@ -158,6 +162,7 @@ func EncryptCTRTriple(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Paddi
158162
return nil, err
159163
}
160164

165+
bs = bs.Clone()
161166
src := padding.Padding(bs, blockSize)
162167
dst := src.Clone()
163168

padding.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ var (
1414
)
1515

1616
// Padding paddings and undo paddings to a byte slice.
17-
// You should know the returned bytes is always cloned from the passed bytes,
18-
// so they are two different byte slices.
1917
type Padding interface {
2018
Padding(bs Bytes, blockSize int) Bytes
2119
UndoPadding(bs Bytes, blockSize int) (Bytes, error)
@@ -24,17 +22,16 @@ type Padding interface {
2422
type paddingNone struct{}
2523

2624
func (paddingNone) Padding(bs Bytes, blockSize int) Bytes {
27-
return bs.Clone()
25+
return bs
2826
}
2927

3028
func (paddingNone) UndoPadding(bs Bytes, blockSize int) (Bytes, error) {
31-
return bs.Clone(), nil
29+
return bs, nil
3230
}
3331

3432
type paddingZero struct{}
3533

3634
func (paddingZero) Padding(bs Bytes, blockSize int) Bytes {
37-
bs = bs.Clone()
3835
padding := blockSize - (len(bs) % blockSize)
3936

4037
for i := 0; i < padding; i++ {
@@ -45,7 +42,6 @@ func (paddingZero) Padding(bs Bytes, blockSize int) Bytes {
4542
}
4643

4744
func (paddingZero) UndoPadding(bs Bytes, blockSize int) (Bytes, error) {
48-
bs = bs.Clone()
4945
length := len(bs)
5046

5147
var i int
@@ -66,7 +62,6 @@ func (paddingZero) UndoPadding(bs Bytes, blockSize int) (Bytes, error) {
6662
type paddingPKCS7 struct{}
6763

6864
func (paddingPKCS7) Padding(bs Bytes, blockSize int) Bytes {
69-
bs = bs.Clone()
7065
padding := blockSize - (len(bs) % blockSize)
7166

7267
for i := 0; i < padding; i++ {
@@ -77,7 +72,6 @@ func (paddingPKCS7) Padding(bs Bytes, blockSize int) Bytes {
7772
}
7873

7974
func (paddingPKCS7) UndoPadding(bs Bytes, blockSize int) (Bytes, error) {
80-
bs = bs.Clone()
8175
length := len(bs)
8276
number := int(bs[length-1])
8377

rsa/private_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,89 @@ func TestPrivateKey(t *testing.T) {
9393
}
9494
}
9595

96+
// go test -v -cover -count=1 -test.cpu=1 -run=^TestPrivateKeyDecryptPKCS1v15$
97+
func TestPrivateKeyDecryptPKCS1v15(t *testing.T) {
98+
publicKey := newTestPublicKey(t)
99+
privateKey := newTestPrivateKey(t)
100+
101+
cases := []string{
102+
"", "123", "你好,世界",
103+
}
104+
105+
for _, msg := range cases {
106+
encrypted, err := publicKey.EncryptPKCS1v15(cryptox.Bytes(msg))
107+
if err != nil {
108+
t.Fatal(err)
109+
}
110+
111+
decrypted, err := privateKey.DecryptPKCS1v15(encrypted)
112+
if err != nil {
113+
t.Fatal(err)
114+
}
115+
116+
if string(decrypted) != msg {
117+
t.Fatalf("decrypted %s != msg %s", decrypted, msg)
118+
}
119+
}
120+
}
121+
122+
// go test -v -cover -count=1 -test.cpu=1 -run=^TestPrivateKeyDecryptPKCS1v15SessionKey$
123+
func TestPrivateKeyDecryptPKCS1v15SessionKey(t *testing.T) {
124+
publicKey := newTestPublicKey(t)
125+
privateKey := newTestPrivateKey(t)
126+
127+
cases := []string{
128+
"", "123", "你好,世界",
129+
}
130+
131+
for _, msg := range cases {
132+
encrypted, err := publicKey.EncryptPKCS1v15(cryptox.Bytes(msg))
133+
if err != nil {
134+
t.Fatal(err)
135+
}
136+
137+
sessionKey := cryptox.GenerateBytes(32)
138+
if err = privateKey.DecryptPKCS1v15SessionKey(encrypted, sessionKey); err != nil {
139+
t.Fatal(err)
140+
}
141+
142+
decrypted, err := privateKey.DecryptPKCS1v15(encrypted)
143+
if err != nil {
144+
t.Fatal(err)
145+
}
146+
147+
if string(decrypted) != msg {
148+
t.Fatalf("decrypted %s != msg %s", decrypted, msg)
149+
}
150+
}
151+
}
152+
153+
// go test -v -cover -count=1 -test.cpu=1 -run=^TestPrivateKeyDecryptOAEP$
154+
func TestPrivateKeyDecryptOAEP(t *testing.T) {
155+
publicKey := newTestPublicKey(t)
156+
privateKey := newTestPrivateKey(t)
157+
158+
cases := []string{
159+
"", "123", "你好,世界",
160+
}
161+
162+
for _, msg := range cases {
163+
encrypted, err := publicKey.EncryptOAEP(cryptox.Bytes(msg), cryptox.Bytes(msg))
164+
if err != nil {
165+
t.Fatal(err)
166+
}
167+
168+
decrypted, err := privateKey.DecryptOAEP(encrypted, cryptox.Bytes(msg))
169+
if err != nil {
170+
t.Fatal(err)
171+
}
172+
173+
if string(decrypted) != msg {
174+
t.Fatalf("decrypted %s != msg %s", decrypted, msg)
175+
}
176+
}
177+
}
178+
96179
// go test -v -cover -count=1 -test.cpu=1 -run=^TestPrivateKeySignPKCS1v15$
97180
func TestPrivateKeySignPKCS1v15(t *testing.T) {
98181
publicKey := newTestPublicKey(t)

0 commit comments

Comments
 (0)