Skip to content

Commit

Permalink
性能优化,尝试减少内存分配
Browse files Browse the repository at this point in the history
  • Loading branch information
FishGoddess committed May 27, 2024
1 parent cab8408 commit 1426329
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 175 deletions.
2 changes: 1 addition & 1 deletion FUTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

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

### v0.3.x
Expand Down
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## 📜 历史版本的特性介绍 (Features in old versions)

### v0.4.2-alpha

> 此版本发布于 2024-05-27
* 性能优化,尝试减少内存分配

### v0.4.1-alpha

> 此版本发布于 2024-05-22
Expand Down
166 changes: 83 additions & 83 deletions README.en.md

Large diffs are not rendered by default.

166 changes: 83 additions & 83 deletions README.md

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions aes/aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func EncryptECB(key cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (c
return nil, err
}

bs = bs.Clone()
src := padding.Padding(bs, blockSize)
dst := src.Clone()

Expand Down Expand Up @@ -80,6 +81,7 @@ func EncryptCBC(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
return nil, err
}

bs = bs.Clone()
src := padding.Padding(bs, blockSize)
dst := src.Clone()

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

bs = bs.Clone()
src := padding.Padding(bs, blockSize)
dst := src.Clone()

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

bs = bs.Clone()
src := padding.Padding(bs, blockSize)
dst := src.Clone()

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

bs = bs.Clone()
src := padding.Padding(bs, blockSize)
dst := src.Clone()

Expand Down
5 changes: 5 additions & 0 deletions des/des.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func EncryptECB(key cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (c
return nil, err
}

bs = bs.Clone()
src := padding.Padding(bs, blockSize)
dst := src.Clone()

Expand Down Expand Up @@ -80,6 +81,7 @@ func EncryptCBC(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
return nil, err
}

bs = bs.Clone()
src := padding.Padding(bs, blockSize)
dst := src.Clone()

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

bs = bs.Clone()
src := padding.Padding(bs, blockSize)
dst := src.Clone()

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

bs = bs.Clone()
src := padding.Padding(bs, blockSize)
dst := src.Clone()

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

bs = bs.Clone()
src := padding.Padding(bs, blockSize)
dst := src.Clone()

Expand Down
5 changes: 5 additions & 0 deletions des/triple_des.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func EncryptECBTriple(key cryptox.Bytes, padding cryptox.Padding, bs cryptox.Byt
return nil, err
}

bs = bs.Clone()
src := padding.Padding(bs, blockSize)
dst := src.Clone()

Expand Down Expand Up @@ -80,6 +81,7 @@ func EncryptCBCTriple(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Paddi
return nil, err
}

bs = bs.Clone()
src := padding.Padding(bs, blockSize)
dst := src.Clone()

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

bs = bs.Clone()
src := padding.Padding(bs, blockSize)
dst := src.Clone()

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

bs = bs.Clone()
src := padding.Padding(bs, blockSize)
dst := src.Clone()

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

bs = bs.Clone()
src := padding.Padding(bs, blockSize)
dst := src.Clone()

Expand Down
10 changes: 2 additions & 8 deletions padding.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ var (
)

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

func (paddingNone) Padding(bs Bytes, blockSize int) Bytes {
return bs.Clone()
return bs
}

func (paddingNone) UndoPadding(bs Bytes, blockSize int) (Bytes, error) {
return bs.Clone(), nil
return bs, nil
}

type paddingZero struct{}

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

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

func (paddingZero) UndoPadding(bs Bytes, blockSize int) (Bytes, error) {
bs = bs.Clone()
length := len(bs)

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

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

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

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

Expand Down

0 comments on commit 1426329

Please sign in to comment.