-
Notifications
You must be signed in to change notification settings - Fork 0
/
vaes128_test.go
116 lines (108 loc) · 2.86 KB
/
vaes128_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
package vaes128
import (
"testing"
)
// AES test: IV length = 0, message key length = 16
func TestAES128AES(t *testing.T) {
key := New("", "1234567890123456")
plaintext := []byte("Hello!")
ciphertext, err := key.Encrypt(plaintext)
if err != nil {
t.Error(err)
}
decrypted, err := key.Decrypt(ciphertext)
if err != nil {
t.Error(err)
}
if string(decrypted) != string(plaintext) {
t.Error("Decrypted plaintext does not match original plaintext")
}
}
// VAES test: IV length = 12, message key length = 12
func TestVAES128(t *testing.T) {
key := New("0123456789abc", "123456789012")
plaintext := []byte("Hello!")
ciphertext, err := key.Encrypt(plaintext)
if err != nil {
t.Error(err)
}
decrypted, err := key.Decrypt(ciphertext)
if err != nil {
t.Error(err)
}
if string(decrypted) != string(plaintext) {
t.Error("Decrypted plaintext does not match original plaintext")
}
}
// VAES test: IV length = 4, message key length = 12
func TestVAES128LenDiff(t *testing.T) {
key := New("0123", "123456789012")
plaintext := []byte("Hello!")
ciphertext, err := key.Encrypt(plaintext)
if err != nil {
t.Error(err)
}
decrypted, err := key.Decrypt(ciphertext)
if err != nil {
t.Error(err)
}
if string(decrypted) != string(plaintext) {
t.Error("Decrypted plaintext does not match original plaintext")
}
}
// VAES test: IV length = 12, message key length = 12, plain length = 0
func TestVAES128Len0(t *testing.T) {
key := New("0123456789abc", "123456789012")
plaintext := []byte("")
ciphertext, err := key.Encrypt(plaintext)
if err != nil {
t.Error(err)
}
decrypted, err := key.Decrypt(ciphertext)
if err != nil {
t.Error(err)
}
if string(decrypted) != string(plaintext) {
t.Error("Decrypted plaintext does not match original plaintext")
}
}
// VAES test: IV length = 12, message key length = 12, plain length = multiple of 16 (32)
func TestVAES128Len16(t *testing.T) {
key := New("0123456789abc", "123456789012")
plaintext := []byte("12345678901234567890123456789012")
ciphertext, err := key.Encrypt(plaintext)
if err != nil {
t.Error(err)
}
decrypted, err := key.Decrypt(ciphertext)
if err != nil {
t.Error(err)
}
if string(decrypted) != string(plaintext) {
t.Error("Decrypted plaintext does not match original plaintext")
}
}
// VAES test: hstr
func TestVAES128Hstr(t *testing.T) {
key := New("0123456789abc", "123456789012")
plaintext := []byte("hello!")
ciphertext, err := key.EncryptHstr(plaintext)
if err != nil {
t.Error(err)
}
decrypted, err := key.DecryptHstr(ciphertext)
if err != nil {
t.Error(err)
}
if string(decrypted) != string(plaintext) {
t.Error("Decrypted plaintext does not match original plaintext")
}
}
func BenchmarkVAES128(b *testing.B) {
key := New("0123456789abc", "123456789012")
plaintext := []byte("hello!")
for i := 0; i < b.N; i++ {
ciphertext, _ := key.Encrypt(plaintext)
_, _ = key.Decrypt(ciphertext)
}
}