forked from TencentBlueKing/gopkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes_gcm_test.go
159 lines (134 loc) · 4.04 KB
/
aes_gcm_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package cryptography
import (
"strconv"
"testing"
"time"
. "github.com/onsi/ginkgo/v2"
"github.com/stretchr/testify/assert"
)
const (
AESTestKey string = "AES256Key-32Characters1234567890"
Nonce string = "WA7ChYcNFnCS"
)
var _ = Describe("Cryptography", func() {
Describe("NewAESGcm", func() {
It("ok", func() {
a, err := NewAESGcm([]byte(AESTestKey), []byte(Nonce))
assert.NoError(GinkgoT(), err)
assert.NotNil(GinkgoT(), a)
})
It("invalid key", func() {
_, err := NewAESGcm([]byte("abc"), []byte(Nonce))
assert.Error(GinkgoT(), err)
assert.Contains(GinkgoT(), err.Error(), "invalid key, should be 16 or 32 bytes")
})
It("invalid nonce", func() {
_, err := NewAESGcm([]byte(AESTestKey), []byte("abc"))
assert.Error(GinkgoT(), err)
assert.Contains(GinkgoT(), err.Error(), "invalid nonce, should be 12 bytes")
})
})
Describe("Encrypt and Decrypt", func() {
var aesGcm *AESGcm
var plaintext []byte
var encryptedText []byte
// var encryptedTextBase64 string
BeforeEach(func() {
aesGcm, _ = NewAESGcm([]byte(AESTestKey), []byte(Nonce))
plaintext = []byte("exampleplaintext")
encryptedText = []byte{148, 205, 172, 75, 6, 179, 220, 244, 255, 30, 115, 122, 55, 205, 243, 240, 125, 149, 164, 203, 228, 253, 252, 76, 222, 14, 124, 180, 56, 36, 142, 80}
// encryptedTextBase64 = base64.StdEncoding.EncodeToString(encryptedText)
})
Describe("Encrypt", func() {
It("ok", func() {
et := aesGcm.Encrypt(plaintext)
assert.Equal(GinkgoT(), encryptedText, et)
})
})
Describe("Decrypt", func() {
It("ok", func() {
dt, err := aesGcm.Decrypt(encryptedText)
assert.NoError(GinkgoT(), err)
assert.Equal(GinkgoT(), plaintext, dt)
})
})
Describe("EncryptToString", func() {
It("ok", func() {
et := aesGcm.EncryptToString(plaintext)
assert.Equal(GinkgoT(), string(encryptedText), et)
})
})
Describe("DecryptString", func() {
It("ok", func() {
dt, err := aesGcm.DecryptString(string(encryptedText))
assert.NoError(GinkgoT(), err)
assert.Equal(GinkgoT(), plaintext, dt)
})
})
// Describe("EncryptToBase64", func() {
// It("ok", func() {
// et := aesGcm.EncryptToBase64(plaintext)
// assert.Equal(GinkgoT(), encryptedTextBase64, et)
// })
// })
//
// Describe("DecryptFromBase64", func() {
// It("ok", func() {
// dt, err := aesGcm.DecryptFromBase64([]byte(encryptedTextBase64))
// assert.NoError(GinkgoT(), err)
// assert.Equal(GinkgoT(), string(plaintext), dt)
// })
//
// })
})
})
func setup() []byte {
nonce := []byte(strconv.Itoa(int(time.Now().UTC().Unix())))[:NonceByteSize]
return nonce
}
func benchmarkAESGCMEncrypt(b *testing.B) {
text := "http://www.test.com?foo=bar&hello=world"
nonce := setup()
aesgcm, _ := NewAESGcm([]byte(AESTestKey), nonce)
input := []byte(text)
for i := 0; i < b.N; i++ {
aesgcm.Encrypt(input)
}
}
func benchmarkAESGCMDecrypt(b *testing.B) {
text := "http://www.test.com?foo=bar&hello=world"
nonce := setup()
aesgcm, _ := NewAESGcm([]byte(AESTestKey), nonce)
input := []byte(text)
encryptedText := aesgcm.Encrypt(input)
for i := 0; i < b.N; i++ {
_, _ = aesgcm.Decrypt(encryptedText)
}
}
func benchmarkAESGCMEncryptToString(b *testing.B) {
text := []byte("http://www.test.com?foo=bar&hello=world")
nonce := setup()
aesgcm, _ := NewAESGcm([]byte(AESTestKey), nonce)
// input := []byte(text)
for i := 0; i < b.N; i++ {
aesgcm.EncryptToString(text)
}
}
func benchmarkAESGCMDecryptString(b *testing.B) {
text := []byte("http://www.test.com?foo=bar&hello=world")
nonce := setup()
aesgcm, _ := NewAESGcm([]byte(AESTestKey), nonce)
// input := []byte(text)
encryptedText := aesgcm.EncryptToString(text)
for i := 0; i < b.N; i++ {
_, _ = aesgcm.DecryptString(encryptedText)
}
}
func BenchmarkAESGCMEncryptDecrypt(b *testing.B) {
b.Run("cipher", func(b *testing.B) {
b.Run("Encrypt", benchmarkAESGCMEncrypt)
b.Run("Decrypt", benchmarkAESGCMDecrypt)
b.Run("EncryptString", benchmarkAESGCMEncryptToString)
b.Run("DecryptString", benchmarkAESGCMDecryptString)
})
}