Skip to content

Commit 8b3c49b

Browse files
committed
aes
1 parent 0a3f997 commit 8b3c49b

File tree

8 files changed

+434
-406
lines changed

8 files changed

+434
-406
lines changed

_examples/aes.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import (
88
"bytes"
99
"fmt"
1010

11-
"github.com/FishGoddess/cryptox"
1211
"github.com/FishGoddess/cryptox/aes"
12+
"github.com/FishGoddess/cryptox/bytes/encoding"
13+
"github.com/FishGoddess/cryptox/bytes/padding"
1314
)
1415

1516
func main() {
@@ -21,26 +22,20 @@ func main() {
2122
msg := []byte("你好,世界")
2223
fmt.Printf("msg: %s\n", msg)
2324

24-
// We use ctr mode and no padding to encrypt data.
25-
// Of course, you can choose another mode if you want.
26-
// Also, you can choose no/zero/pkcs5/pkcs7 to padding data.
27-
encrypted, err := aes.EncryptCTR(key, iv, cryptox.PaddingNone, msg)
25+
// Use ctr mode to encrypt data with no padding and encoding base64.
26+
encrypt, err := aes.EncryptCTR(msg, key, iv, padding.None, encoding.Base64)
2827
if err != nil {
2928
panic(err)
3029
}
3130

32-
fmt.Println("encrypted:", encrypted)
33-
fmt.Println("encrypted hex:", encrypted.Hex())
34-
fmt.Println("encrypted base64:", encrypted.Base64())
31+
fmt.Printf("encrypt: %s\n", encrypt)
3532

36-
// We use ctr mode and no padding to decrypt data.
37-
// Of course, you can choose another mode if you want.
38-
// Also, you can choose no/zero/pkcs5/pkcs7 to undo padding data.
39-
decrypted, err := aes.DecryptCTR(key, iv, cryptox.PaddingNone, encrypted)
33+
// Decrypt data in the same way.
34+
decrypt, err := aes.DecryptCTR(encrypt, key, iv, padding.None, encoding.Base64)
4035
if err != nil {
4136
panic(err)
4237
}
4338

44-
fmt.Printf("decrypted: %s\n", decrypted)
45-
fmt.Println("decrypted == msg", bytes.Equal(decrypted, msg))
39+
fmt.Printf("decrypt: %s\n", decrypt)
40+
fmt.Printf("decrypt is right: %+v\n", bytes.Equal(decrypt, msg))
4641
}

_examples/aes_test.go

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,98 +7,99 @@ package main
77
import (
88
"testing"
99

10-
"github.com/FishGoddess/cryptox"
1110
"github.com/FishGoddess/cryptox/aes"
11+
"github.com/FishGoddess/cryptox/bytes/encoding"
12+
"github.com/FishGoddess/cryptox/bytes/padding"
1213
)
1314

1415
var (
1516
aesBenchKey = []byte("12345678876543211234567887654321")
1617
aesBenchIV = []byte("8765432112345678")
1718
aesBenchNonce = []byte("123456abcdef")
18-
aesBenchMsg = cryptox.GenerateBytes(128)
19+
aesBenchMsg = make([]byte, 128)
1920
)
2021

21-
// go test -v -bench=^BenchmarkAESEncryptECB$ -benchtime=1s aes_test.go
22-
func BenchmarkAESEncryptECB(b *testing.B) {
22+
// go test -v -bench=^BenchmarkAES_EncryptECB$ -benchtime=1s des_test.go
23+
func BenchmarkAES_EncryptECB(b *testing.B) {
2324
b.ReportAllocs()
2425
b.ResetTimer()
2526

2627
for i := 0; i < b.N; i++ {
27-
_, err := aes.EncryptECB(aesBenchKey, cryptox.PaddingPKCS7, aesBenchMsg)
28+
_, err := aes.EncryptECB(aesBenchMsg, aesBenchKey, padding.PKCS7, encoding.None)
2829
if err != nil {
2930
b.Fatal(err)
3031
}
3132
}
3233
}
3334

34-
// go test -v -bench=^BenchmarkAESEncryptCBC$ -benchtime=1s aes_test.go
35-
func BenchmarkAESEncryptCBC(b *testing.B) {
35+
// go test -v -bench=^BenchmarkAES_EncryptCBC$ -benchtime=1s des_test.go
36+
func BenchmarkAES_EncryptCBC(b *testing.B) {
3637
b.ReportAllocs()
3738
b.ResetTimer()
3839

3940
for i := 0; i < b.N; i++ {
40-
_, err := aes.EncryptCBC(aesBenchKey, aesBenchIV, cryptox.PaddingPKCS7, aesBenchMsg)
41+
_, err := aes.EncryptCBC(aesBenchMsg, aesBenchKey, aesBenchIV, padding.PKCS7, encoding.None)
4142
if err != nil {
4243
b.Fatal(err)
4344
}
4445
}
4546
}
4647

47-
// go test -v -bench=^BenchmarkAESEncryptCFB$ -benchtime=1s aes_test.go
48-
func BenchmarkAESEncryptCFB(b *testing.B) {
48+
// go test -v -bench=^BenchmarkAES_EncryptCFB$ -benchtime=1s des_test.go
49+
func BenchmarkAES_EncryptCFB(b *testing.B) {
4950
b.ReportAllocs()
5051
b.ResetTimer()
5152

5253
for i := 0; i < b.N; i++ {
53-
_, err := aes.EncryptCFB(aesBenchKey, aesBenchIV, cryptox.PaddingNone, aesBenchMsg)
54+
_, err := aes.EncryptCFB(aesBenchMsg, aesBenchKey, aesBenchIV, padding.None, encoding.None)
5455
if err != nil {
5556
b.Fatal(err)
5657
}
5758
}
5859
}
5960

60-
// go test -v -bench=^BenchmarkAESEncryptOFB$ -benchtime=1s aes_test.go
61-
func BenchmarkAESEncryptOFB(b *testing.B) {
61+
// go test -v -bench=^BenchmarkAES_EncryptOFB$ -benchtime=1s des_test.go
62+
func BenchmarkAES_EncryptOFB(b *testing.B) {
6263
b.ReportAllocs()
6364
b.ResetTimer()
6465

6566
for i := 0; i < b.N; i++ {
66-
_, err := aes.EncryptOFB(aesBenchKey, aesBenchIV, cryptox.PaddingNone, aesBenchMsg)
67+
_, err := aes.EncryptOFB(aesBenchMsg, aesBenchKey, aesBenchIV, padding.None, encoding.None)
6768
if err != nil {
6869
b.Fatal(err)
6970
}
7071
}
7172
}
7273

73-
// go test -v -bench=^BenchmarkAESEncryptCTR$ -benchtime=1s aes_test.go
74-
func BenchmarkAESEncryptCTR(b *testing.B) {
74+
// go test -v -bench=^BenchmarkAES_EncryptCTR$ -benchtime=1s des_test.go
75+
func BenchmarkAES_EncryptCTR(b *testing.B) {
7576
b.ReportAllocs()
7677
b.ResetTimer()
7778

7879
for i := 0; i < b.N; i++ {
79-
_, err := aes.EncryptCTR(aesBenchKey, aesBenchIV, cryptox.PaddingNone, aesBenchMsg)
80+
_, err := aes.EncryptCTR(aesBenchMsg, aesBenchKey, aesBenchIV, padding.None, encoding.None)
8081
if err != nil {
8182
b.Fatal(err)
8283
}
8384
}
8485
}
8586

8687
// go test -v -bench=^BenchmarkAESEncryptGCM$ -benchtime=1s aes_test.go
87-
func BenchmarkAESEncryptGCM(b *testing.B) {
88+
func BenchmarkAES_EncryptGCM(b *testing.B) {
8889
b.ReportAllocs()
8990
b.ResetTimer()
9091

9192
for i := 0; i < b.N; i++ {
92-
_, err := aes.EncryptGCM(aesBenchKey, aesBenchNonce, nil, aesBenchMsg)
93+
_, err := aes.EncryptGCM(aesBenchMsg, aesBenchKey, aesBenchNonce, nil, encoding.None)
9394
if err != nil {
9495
b.Fatal(err)
9596
}
9697
}
9798
}
9899

99-
// go test -v -bench=^BenchmarkAESDecryptECB$ -benchtime=1s aes_test.go
100-
func BenchmarkAESDecryptECB(b *testing.B) {
101-
encrypted, err := aes.EncryptECB(aesBenchKey, cryptox.PaddingPKCS7, aesBenchMsg)
100+
// go test -v -bench=^BenchmarkAES_DecryptECB$ -benchtime=1s des_test.go
101+
func BenchmarkAES_DecryptECB(b *testing.B) {
102+
encrypted, err := aes.EncryptECB(aesBenchMsg, aesBenchKey, padding.PKCS7, encoding.None)
102103
if err != nil {
103104
b.Fatal(err)
104105
}
@@ -107,16 +108,16 @@ func BenchmarkAESDecryptECB(b *testing.B) {
107108
b.ResetTimer()
108109

109110
for i := 0; i < b.N; i++ {
110-
_, err := aes.DecryptECB(aesBenchKey, cryptox.PaddingPKCS7, encrypted)
111+
_, err := aes.DecryptECB(encrypted, aesBenchKey, padding.PKCS7, encoding.None)
111112
if err != nil {
112113
b.Fatal(err)
113114
}
114115
}
115116
}
116117

117-
// go test -v -bench=^BenchmarkAESDecryptCBC$ -benchtime=1s aes_test.go
118-
func BenchmarkAESDecryptCBC(b *testing.B) {
119-
encrypted, err := aes.EncryptCBC(aesBenchKey, aesBenchIV, cryptox.PaddingPKCS7, aesBenchMsg)
118+
// go test -v -bench=^BenchmarkAES_DecryptCBC$ -benchtime=1s des_test.go
119+
func BenchmarkAES_DecryptCBC(b *testing.B) {
120+
encrypted, err := aes.EncryptCBC(aesBenchMsg, aesBenchKey, aesBenchIV, padding.PKCS7, encoding.None)
120121
if err != nil {
121122
b.Fatal(err)
122123
}
@@ -125,16 +126,16 @@ func BenchmarkAESDecryptCBC(b *testing.B) {
125126
b.ResetTimer()
126127

127128
for i := 0; i < b.N; i++ {
128-
_, err := aes.DecryptCBC(aesBenchKey, aesBenchIV, cryptox.PaddingPKCS7, encrypted)
129+
_, err := aes.DecryptCBC(encrypted, aesBenchKey, aesBenchIV, padding.PKCS7, encoding.None)
129130
if err != nil {
130131
b.Fatal(err)
131132
}
132133
}
133134
}
134135

135-
// go test -v -bench=^BenchmarkAESDecryptCFB$ -benchtime=1s aes_test.go
136-
func BenchmarkAESDecryptCFB(b *testing.B) {
137-
encrypted, err := aes.EncryptCFB(aesBenchKey, aesBenchIV, cryptox.PaddingNone, aesBenchMsg)
136+
// go test -v -bench=^BenchmarkAES_DecryptCFB$ -benchtime=1s des_test.go
137+
func BenchmarkAES_DecryptCFB(b *testing.B) {
138+
encrypted, err := aes.EncryptCFB(aesBenchMsg, aesBenchKey, aesBenchIV, padding.None, encoding.None)
138139
if err != nil {
139140
b.Fatal(err)
140141
}
@@ -143,16 +144,16 @@ func BenchmarkAESDecryptCFB(b *testing.B) {
143144
b.ResetTimer()
144145

145146
for i := 0; i < b.N; i++ {
146-
_, err := aes.DecryptCFB(aesBenchKey, aesBenchIV, cryptox.PaddingNone, encrypted)
147+
_, err := aes.DecryptCFB(encrypted, aesBenchKey, aesBenchIV, padding.None, encoding.None)
147148
if err != nil {
148149
b.Fatal(err)
149150
}
150151
}
151152
}
152153

153-
// go test -v -bench=^BenchmarkAESDecryptOFB$ -benchtime=1s aes_test.go
154-
func BenchmarkAESDecryptOFB(b *testing.B) {
155-
encrypted, err := aes.EncryptOFB(aesBenchKey, aesBenchIV, cryptox.PaddingNone, aesBenchMsg)
154+
// go test -v -bench=^BenchmarkAES_DecryptOFB$ -benchtime=1s des_test.go
155+
func BenchmarkAES_DecryptOFB(b *testing.B) {
156+
encrypted, err := aes.EncryptOFB(aesBenchMsg, aesBenchKey, aesBenchIV, padding.None, encoding.None)
156157
if err != nil {
157158
b.Fatal(err)
158159
}
@@ -161,16 +162,16 @@ func BenchmarkAESDecryptOFB(b *testing.B) {
161162
b.ResetTimer()
162163

163164
for i := 0; i < b.N; i++ {
164-
_, err := aes.DecryptOFB(aesBenchKey, aesBenchIV, cryptox.PaddingNone, encrypted)
165+
_, err := aes.DecryptOFB(encrypted, aesBenchKey, aesBenchIV, padding.None, encoding.None)
165166
if err != nil {
166167
b.Fatal(err)
167168
}
168169
}
169170
}
170171

171-
// go test -v -bench=^BenchmarkAESDecryptCTR$ -benchtime=1s aes_test.go
172-
func BenchmarkAESDecryptCTR(b *testing.B) {
173-
encrypted, err := aes.EncryptCTR(aesBenchKey, aesBenchIV, cryptox.PaddingNone, aesBenchMsg)
172+
// go test -v -bench=^BenchmarkAES_DecryptCTR$ -benchtime=1s des_test.go
173+
func BenchmarkAES_DecryptCTR(b *testing.B) {
174+
encrypted, err := aes.EncryptCTR(aesBenchMsg, aesBenchKey, aesBenchIV, padding.None, encoding.None)
174175
if err != nil {
175176
b.Fatal(err)
176177
}
@@ -179,16 +180,16 @@ func BenchmarkAESDecryptCTR(b *testing.B) {
179180
b.ResetTimer()
180181

181182
for i := 0; i < b.N; i++ {
182-
_, err := aes.DecryptCTR(aesBenchKey, aesBenchIV, cryptox.PaddingNone, encrypted)
183+
_, err := aes.DecryptCTR(encrypted, aesBenchKey, aesBenchIV, padding.None, encoding.None)
183184
if err != nil {
184185
b.Fatal(err)
185186
}
186187
}
187188
}
188189

189190
// go test -v -bench=^BenchmarkAESDecryptGCM$ -benchtime=1s aes_test.go
190-
func BenchmarkAESDecryptGCM(b *testing.B) {
191-
encrypted, err := aes.EncryptGCM(aesBenchKey, aesBenchNonce, nil, aesBenchMsg)
191+
func BenchmarkAES_DecryptGCM(b *testing.B) {
192+
encrypted, err := aes.EncryptGCM(aesBenchMsg, aesBenchKey, aesBenchNonce, nil, encoding.None)
192193
if err != nil {
193194
b.Fatal(err)
194195
}
@@ -197,7 +198,7 @@ func BenchmarkAESDecryptGCM(b *testing.B) {
197198
b.ResetTimer()
198199

199200
for i := 0; i < b.N; i++ {
200-
_, err := aes.DecryptGCM(aesBenchKey, aesBenchNonce, nil, encrypted)
201+
_, err := aes.DecryptGCM(encrypted, aesBenchKey, aesBenchNonce, nil, encoding.None)
201202
if err != nil {
202203
b.Fatal(err)
203204
}

_examples/des.go

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,48 +22,20 @@ func main() {
2222
msg := []byte("你好,世界")
2323
fmt.Printf("msg: %s\n", msg)
2424

25-
// For example, we can use ctr mode and no padding to encrypt data.
26-
encrypt, err := des.EncryptCTR(msg, key, iv, padding.PaddingNone, encoding.None)
25+
// Use ctr mode to encrypt data with no padding and encoding base64.
26+
encrypt, err := des.EncryptCTR(msg, key, iv, padding.None, encoding.Base64)
2727
if err != nil {
2828
panic(err)
2929
}
3030

31-
// Use encoding to output hex bytes.
32-
encryptHex, err := des.EncryptCTR(msg, key, iv, padding.PaddingNone, encoding.Hex)
33-
if err != nil {
34-
panic(err)
35-
}
36-
37-
// Use encoding to output base64 bytes.
38-
encryptBase64, err := des.EncryptCTR(msg, key, iv, padding.PaddingNone, encoding.Base64)
39-
if err != nil {
40-
panic(err)
41-
}
42-
43-
fmt.Printf("encrypt: %+v\n", encrypt)
44-
fmt.Printf("encrypt hex: %s", encryptHex)
45-
fmt.Printf("encrypt base64: %s", encryptBase64)
46-
47-
// We use ctr mode and no padding to decrypt data.
48-
decrypt, err := des.DecryptCTR(encrypt, key, iv, padding.PaddingNone, encoding.None)
49-
if err != nil {
50-
panic(err)
51-
}
52-
53-
// Use encoding to input hex bytes.
54-
decryptHex, err := des.DecryptCTR(encryptHex, key, iv, padding.PaddingNone, encoding.Hex)
55-
if err != nil {
56-
panic(err)
57-
}
31+
fmt.Printf("encrypt: %s\n", encrypt)
5832

59-
// Use encoding to input base64 bytes..
60-
decryptBase64, err := des.DecryptCTR(encryptBase64, key, iv, padding.PaddingNone, encoding.Base64)
33+
// Decrypt data in the same way.
34+
decrypt, err := des.DecryptCTR(encrypt, key, iv, padding.None, encoding.Base64)
6135
if err != nil {
6236
panic(err)
6337
}
6438

6539
fmt.Printf("decrypt: %s\n", decrypt)
66-
fmt.Printf("decrypt hex: %s\n", decryptHex)
67-
fmt.Printf("decrypt base64: %s\n", decryptBase64)
68-
fmt.Println("decrypt is right: %+v", bytes.Equal(decrypt, msg))
40+
fmt.Printf("decrypt is right: %+v\n", bytes.Equal(decrypt, msg))
6941
}

0 commit comments

Comments
 (0)