Skip to content

Commit

Permalink
调整 aes 性能测试文件
Browse files Browse the repository at this point in the history
  • Loading branch information
FishGoddess committed May 21, 2024
1 parent ae424ec commit d50d8b3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 69 deletions.
1 change: 1 addition & 0 deletions FUTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

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

### v0.3.x
Expand Down
27 changes: 14 additions & 13 deletions _examples/aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package main

import (
"bytes"
"fmt"

"github.com/FishGoddess/cryptox"
Expand All @@ -17,29 +18,29 @@ func main() {
key := []byte("12345678876543211234567887654321")
iv := []byte("8765432112345678")

plain := []byte("你好,世界")
fmt.Println("plain:", plain)
msg := []byte("你好,世界")
fmt.Printf("msg: %s\n", msg)

// We use ctr mode and no padding to encrypt data.
// Of course, you can choose ecb/cbc/cfb/ofb/ctr if you want.
// Of course, you can choose another mode if you want.
// Also, you can choose no/zero/pkcs5/pkcs7 to padding data.
crypted, err := aes.New(key).EncryptCTR(cryptox.PaddingNone, iv, plain)
encrypted, err := aes.EncryptCTR(key, iv, cryptox.PaddingNone, msg)
if err != nil {
panic(err)
}

fmt.Println("crypted:", crypted)
fmt.Println("cryptedHex:", crypted.Hex())
fmt.Println("cryptedBase64:", crypted.Base64())
fmt.Println("encrypted:", encrypted)
fmt.Println("encrypted hex:", encrypted.Hex())
fmt.Println("encrypted base64:", encrypted.Base64())

// We use ctr mode and no unPadding to decrypt data.
// Of course, you can choose ecb/cbc/cfb/ofb/ctr if you want.
// Also, you can choose no/zero/pkcs5/pkcs7 to unPadding data.
newPlain, err := aes.New(key).DecryptCTR(cryptox.UnPaddingNone, iv, crypted)
// We use ctr mode and no padding to decrypt data.
// Of course, you can choose another mode if you want.
// Also, you can choose no/zero/pkcs5/pkcs7 to undo padding data.
decrypted, err := aes.DecryptCTR(key, iv, cryptox.PaddingNone, encrypted)
if err != nil {
panic(err)
}

fmt.Println("newPlain:", newPlain)
fmt.Println("newPlain == plain", newPlain.String() == plain.String())
fmt.Printf("decrypted: %s\n", decrypted)
fmt.Println("decrypted == msg", bytes.Equal(decrypted, msg))
}
62 changes: 22 additions & 40 deletions _examples/aes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@ import (
)

var (
aesBenchKey = []byte("12345678876543211234567887654321")
aesBenchIV = []byte("8765432112345678")
aesBenchNonce = []byte("123456abcdef")
aesBenchPlain, _ = cryptox.GenerateBytes(128)
aesBenchKey = []byte("12345678876543211234567887654321")
aesBenchIV = []byte("8765432112345678")
aesBenchNonce = []byte("123456abcdef")
aesBenchMsg = cryptox.GenerateBytes(128)
)

// go test -v -bench=^BenchmarkAESEncryptECB$ -benchtime=1s aes_test.go
func BenchmarkAESEncryptECB(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()

aesObj := aes.New(aesBenchKey)
for i := 0; i < b.N; i++ {
_, err := aesObj.EncryptECB(cryptox.PaddingPKCS7, aesBenchPlain)
_, err := aes.EncryptECB(aesBenchKey, cryptox.PaddingPKCS7, aesBenchMsg)
if err != nil {
b.Fatal(err)
}
Expand All @@ -37,9 +36,8 @@ func BenchmarkAESEncryptCBC(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()

aesObj := aes.New(aesBenchKey)
for i := 0; i < b.N; i++ {
_, err := aesObj.EncryptCBC(cryptox.PaddingPKCS7, aesBenchIV, aesBenchPlain)
_, err := aes.EncryptCBC(aesBenchKey, aesBenchIV, cryptox.PaddingPKCS7, aesBenchMsg)
if err != nil {
b.Fatal(err)
}
Expand All @@ -51,9 +49,8 @@ func BenchmarkAESEncryptCFB(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()

aesObj := aes.New(aesBenchKey)
for i := 0; i < b.N; i++ {
_, err := aesObj.EncryptCFB(cryptox.PaddingNone, aesBenchIV, aesBenchPlain)
_, err := aes.EncryptCFB(aesBenchKey, aesBenchIV, cryptox.PaddingNone, aesBenchMsg)
if err != nil {
b.Fatal(err)
}
Expand All @@ -65,9 +62,8 @@ func BenchmarkAESEncryptOFB(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()

aesObj := aes.New(aesBenchKey)
for i := 0; i < b.N; i++ {
_, err := aesObj.EncryptOFB(cryptox.PaddingNone, aesBenchIV, aesBenchPlain)
_, err := aes.EncryptOFB(aesBenchKey, aesBenchIV, cryptox.PaddingNone, aesBenchMsg)
if err != nil {
b.Fatal(err)
}
Expand All @@ -79,9 +75,8 @@ func BenchmarkAESEncryptCTR(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()

aesObj := aes.New(aesBenchKey)
for i := 0; i < b.N; i++ {
_, err := aesObj.EncryptCTR(cryptox.PaddingNone, aesBenchIV, aesBenchPlain)
_, err := aes.EncryptCTR(aesBenchKey, aesBenchIV, cryptox.PaddingNone, aesBenchMsg)
if err != nil {
b.Fatal(err)
}
Expand All @@ -93,9 +88,8 @@ func BenchmarkAESEncryptGCM(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()

aesObj := aes.New(aesBenchKey)
for i := 0; i < b.N; i++ {
_, err := aesObj.EncryptGCM(aesBenchNonce, aesBenchPlain, nil)
_, err := aes.EncryptGCM(aesBenchKey, aesBenchNonce, nil, aesBenchMsg)
if err != nil {
b.Fatal(err)
}
Expand All @@ -104,9 +98,7 @@ func BenchmarkAESEncryptGCM(b *testing.B) {

// go test -v -bench=^BenchmarkAESDecryptECB$ -benchtime=1s aes_test.go
func BenchmarkAESDecryptECB(b *testing.B) {
aesObj := aes.New(aesBenchKey)

benchCrypted, err := aesObj.EncryptECB(cryptox.PaddingPKCS7, aesBenchPlain)
encrypted, err := aes.EncryptECB(aesBenchKey, cryptox.PaddingPKCS7, aesBenchMsg)
if err != nil {
b.Fatal(err)
}
Expand All @@ -115,7 +107,7 @@ func BenchmarkAESDecryptECB(b *testing.B) {
b.ResetTimer()

for i := 0; i < b.N; i++ {
_, err := aesObj.DecryptECB(cryptox.UnPaddingPKCS7, benchCrypted)
_, err := aes.DecryptECB(aesBenchKey, cryptox.PaddingPKCS7, encrypted)
if err != nil {
b.Fatal(err)
}
Expand All @@ -124,9 +116,7 @@ func BenchmarkAESDecryptECB(b *testing.B) {

// go test -v -bench=^BenchmarkAESDecryptCBC$ -benchtime=1s aes_test.go
func BenchmarkAESDecryptCBC(b *testing.B) {
aesObj := aes.New(aesBenchKey)

benchCrypted, err := aesObj.EncryptCBC(cryptox.PaddingPKCS7, aesBenchIV, aesBenchPlain)
encrypted, err := aes.EncryptCBC(aesBenchKey, aesBenchIV, cryptox.PaddingPKCS7, aesBenchMsg)
if err != nil {
b.Fatal(err)
}
Expand All @@ -135,7 +125,7 @@ func BenchmarkAESDecryptCBC(b *testing.B) {
b.ResetTimer()

for i := 0; i < b.N; i++ {
_, err := aesObj.DecryptCBC(cryptox.UnPaddingPKCS7, aesBenchIV, benchCrypted)
_, err := aes.DecryptCBC(aesBenchKey, aesBenchIV, cryptox.PaddingPKCS7, encrypted)
if err != nil {
b.Fatal(err)
}
Expand All @@ -144,9 +134,7 @@ func BenchmarkAESDecryptCBC(b *testing.B) {

// go test -v -bench=^BenchmarkAESDecryptCFB$ -benchtime=1s aes_test.go
func BenchmarkAESDecryptCFB(b *testing.B) {
aesObj := aes.New(aesBenchKey)

benchCrypted, err := aesObj.EncryptCFB(cryptox.PaddingNone, aesBenchIV, aesBenchPlain)
encrypted, err := aes.EncryptCFB(aesBenchKey, aesBenchIV, cryptox.PaddingNone, aesBenchMsg)
if err != nil {
b.Fatal(err)
}
Expand All @@ -155,7 +143,7 @@ func BenchmarkAESDecryptCFB(b *testing.B) {
b.ResetTimer()

for i := 0; i < b.N; i++ {
_, err := aesObj.DecryptCFB(cryptox.UnPaddingNone, aesBenchIV, benchCrypted)
_, err := aes.DecryptCFB(aesBenchKey, aesBenchIV, cryptox.PaddingNone, encrypted)
if err != nil {
b.Fatal(err)
}
Expand All @@ -164,9 +152,7 @@ func BenchmarkAESDecryptCFB(b *testing.B) {

// go test -v -bench=^BenchmarkAESDecryptOFB$ -benchtime=1s aes_test.go
func BenchmarkAESDecryptOFB(b *testing.B) {
aesObj := aes.New(aesBenchKey)

benchCrypted, err := aesObj.EncryptOFB(cryptox.PaddingNone, aesBenchIV, aesBenchPlain)
encrypted, err := aes.EncryptOFB(aesBenchKey, aesBenchIV, cryptox.PaddingNone, aesBenchMsg)
if err != nil {
b.Fatal(err)
}
Expand All @@ -175,7 +161,7 @@ func BenchmarkAESDecryptOFB(b *testing.B) {
b.ResetTimer()

for i := 0; i < b.N; i++ {
_, err := aesObj.DecryptOFB(cryptox.UnPaddingNone, aesBenchIV, benchCrypted)
_, err := aes.DecryptOFB(aesBenchKey, aesBenchIV, cryptox.PaddingNone, encrypted)
if err != nil {
b.Fatal(err)
}
Expand All @@ -184,9 +170,7 @@ func BenchmarkAESDecryptOFB(b *testing.B) {

// go test -v -bench=^BenchmarkAESDecryptCTR$ -benchtime=1s aes_test.go
func BenchmarkAESDecryptCTR(b *testing.B) {
aesObj := aes.New(aesBenchKey)

benchCrypted, err := aesObj.EncryptCTR(cryptox.PaddingNone, aesBenchIV, aesBenchPlain)
encrypted, err := aes.EncryptCTR(aesBenchKey, aesBenchIV, cryptox.PaddingNone, aesBenchMsg)
if err != nil {
b.Fatal(err)
}
Expand All @@ -195,7 +179,7 @@ func BenchmarkAESDecryptCTR(b *testing.B) {
b.ResetTimer()

for i := 0; i < b.N; i++ {
_, err := aesObj.DecryptCTR(cryptox.UnPaddingNone, aesBenchIV, benchCrypted)
_, err := aes.DecryptCTR(aesBenchKey, aesBenchIV, cryptox.PaddingNone, encrypted)
if err != nil {
b.Fatal(err)
}
Expand All @@ -204,9 +188,7 @@ func BenchmarkAESDecryptCTR(b *testing.B) {

// go test -v -bench=^BenchmarkAESDecryptGCM$ -benchtime=1s aes_test.go
func BenchmarkAESDecryptGCM(b *testing.B) {
aesObj := aes.New(aesBenchKey)

benchCrypted, err := aesObj.EncryptGCM(aesBenchNonce, aesBenchPlain, nil)
encrypted, err := aes.EncryptGCM(aesBenchKey, aesBenchNonce, nil, aesBenchMsg)
if err != nil {
b.Fatal(err)
}
Expand All @@ -215,7 +197,7 @@ func BenchmarkAESDecryptGCM(b *testing.B) {
b.ResetTimer()

for i := 0; i < b.N; i++ {
_, err := aesObj.DecryptGCM(aesBenchNonce, benchCrypted, nil)
_, err := aes.DecryptGCM(aesBenchKey, aesBenchNonce, nil, encrypted)
if err != nil {
b.Fatal(err)
}
Expand Down
16 changes: 8 additions & 8 deletions aes/aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func DecryptCFB(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
return padding.UndoPadding(dst, blockSize)
}

func EncryptCTR(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
func EncryptOFB(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
block, blockSize, err := newBlock(key)
if err != nil {
return nil, err
Expand All @@ -135,11 +135,11 @@ func EncryptCTR(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
src := padding.Padding(bs, blockSize)
dst := src.Clone()

cipher.NewCTR(block, iv).XORKeyStream(dst, src)
cipher.NewOFB(block, iv).XORKeyStream(dst, src)
return dst, nil
}

func DecryptCTR(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
func DecryptOFB(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
block, blockSize, err := newBlock(key)
if err != nil {
return nil, err
Expand All @@ -148,11 +148,11 @@ func DecryptCTR(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
src := bs
dst := bs.Clone()

cipher.NewCTR(block, iv).XORKeyStream(dst, src)
cipher.NewOFB(block, iv).XORKeyStream(dst, src)
return padding.UndoPadding(dst, blockSize)
}

func EncryptOFB(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
func EncryptCTR(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
block, blockSize, err := newBlock(key)
if err != nil {
return nil, err
Expand All @@ -161,11 +161,11 @@ func EncryptOFB(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
src := padding.Padding(bs, blockSize)
dst := src.Clone()

cipher.NewOFB(block, iv).XORKeyStream(dst, src)
cipher.NewCTR(block, iv).XORKeyStream(dst, src)
return dst, nil
}

func DecryptOFB(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
func DecryptCTR(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
block, blockSize, err := newBlock(key)
if err != nil {
return nil, err
Expand All @@ -174,7 +174,7 @@ func DecryptOFB(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
src := bs
dst := bs.Clone()

cipher.NewOFB(block, iv).XORKeyStream(dst, src)
cipher.NewCTR(block, iv).XORKeyStream(dst, src)
return padding.UndoPadding(dst, blockSize)
}

Expand Down
16 changes: 8 additions & 8 deletions aes/aes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ func TestCFB(t *testing.T) {
}
}

// go test -v -cover -count=1 -test.cpu=1 -run=^TestCTR$
func TestCTR(t *testing.T) {
// go test -v -cover -count=1 -test.cpu=1 -run=^TestOFB$
func TestOFB(t *testing.T) {
cases := map[string]*testResult{
"": {
bs: []byte{129, 42, 238, 182, 0, 143, 73, 239, 33, 57, 26, 89, 78, 230, 185, 139},
Expand All @@ -208,7 +208,7 @@ func TestCTR(t *testing.T) {
}

for input, expect := range cases {
encrypted, err := EncryptCTR(testKey, testIV, cryptox.PaddingPKCS7, []byte(input))
encrypted, err := EncryptOFB(testKey, testIV, cryptox.PaddingPKCS7, []byte(input))
if err != nil {
t.Fatal(err)
}
Expand All @@ -217,7 +217,7 @@ func TestCTR(t *testing.T) {
t.Fatal(err)
}

decrypted, err := DecryptCTR(testKey, testIV, cryptox.PaddingPKCS7, encrypted)
decrypted, err := DecryptOFB(testKey, testIV, cryptox.PaddingPKCS7, encrypted)
if err != nil {
t.Fatal(err)
}
Expand All @@ -228,8 +228,8 @@ func TestCTR(t *testing.T) {
}
}

// go test -v -cover -count=1 -test.cpu=1 -run=^TestOFB$
func TestOFB(t *testing.T) {
// go test -v -cover -count=1 -test.cpu=1 -run=^TestCTR$
func TestCTR(t *testing.T) {
cases := map[string]*testResult{
"": {
bs: []byte{129, 42, 238, 182, 0, 143, 73, 239, 33, 57, 26, 89, 78, 230, 185, 139},
Expand All @@ -249,7 +249,7 @@ func TestOFB(t *testing.T) {
}

for input, expect := range cases {
encrypted, err := EncryptOFB(testKey, testIV, cryptox.PaddingPKCS7, []byte(input))
encrypted, err := EncryptCTR(testKey, testIV, cryptox.PaddingPKCS7, []byte(input))
if err != nil {
t.Fatal(err)
}
Expand All @@ -258,7 +258,7 @@ func TestOFB(t *testing.T) {
t.Fatal(err)
}

decrypted, err := DecryptOFB(testKey, testIV, cryptox.PaddingPKCS7, encrypted)
decrypted, err := DecryptCTR(testKey, testIV, cryptox.PaddingPKCS7, encrypted)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit d50d8b3

Please sign in to comment.