-
Notifications
You must be signed in to change notification settings - Fork 48
/
aes_test.go
executable file
·120 lines (103 loc) · 3.85 KB
/
aes_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
package openssl
import (
"encoding/base64"
"github.com/stretchr/testify/assert"
"reflect"
"testing"
)
func TestAes(t *testing.T) {
src := []byte("1234567890_QWERTY")
key := []byte("12345123451234512345123451234512")
dst, err := AesECBEncrypt(src, key, PKCS7_PADDING)
assert.NoError(t, err)
t.Log(base64.StdEncoding.EncodeToString(dst))
assert.Equal(t, base64.StdEncoding.EncodeToString(dst), "MdzAshBM1s7uxcblqWIyTY0s2tcEpGg+OwaQzHMd45o=")
}
func TestAesEncrypt(t *testing.T) {
src := []byte("123456")
// AES-128-ECB, PKCS7_PADDING
key := []byte("1234512345123451")
dst, err := AesECBEncrypt(src, key, PKCS7_PADDING)
assert.NoError(t, err)
t.Log(base64.StdEncoding.EncodeToString(dst))
assert.Equal(t, base64.StdEncoding.EncodeToString(dst), "SpfAShHImQhWjd/21Pgz2Q==")
// AES-192-ECB, PKCS7_PADDING
key = []byte("123451234512345123451234")
dst, err = AesECBEncrypt(src, key, PKCS7_PADDING)
assert.NoError(t, err)
t.Log(base64.StdEncoding.EncodeToString(dst))
assert.Equal(t, base64.StdEncoding.EncodeToString(dst), "vOwA1oZknZ54rgXETVYwMg==")
// AES-256-ECB, PKCS7_PADDING
key = []byte("12345123451234512345123451234512")
dst, err = AesECBEncrypt(src, key, PKCS7_PADDING)
assert.NoError(t, err)
t.Log(base64.StdEncoding.EncodeToString(dst))
assert.Equal(t, base64.StdEncoding.EncodeToString(dst), "F+tlXjWffI4xt656KVwgLg==")
}
func TestAesECBDecrypt(t *testing.T) {
src, err := base64.StdEncoding.DecodeString("SpfAShHImQhWjd/21Pgz2Q==")
assert.NoError(t, err)
// AES-128-ECB, PKCS7_PADDING
key := []byte("1234512345123451")
dst, err := AesECBDecrypt(src, key, PKCS7_PADDING)
assert.NoError(t, err)
t.Log(string(dst))
assert.Equal(t, dst, []byte("123456"))
// AES-192-ECB, PKCS7_PADDING
src, _ = base64.StdEncoding.DecodeString("vOwA1oZknZ54rgXETVYwMg==")
key = []byte("123451234512345123451234")
dst, err = AesECBDecrypt(src, key, PKCS7_PADDING)
assert.NoError(t, err)
t.Log(string(dst))
assert.Equal(t, dst, []byte("123456"))
// AES-256-ECB, PKCS7_PADDING
src, _ = base64.StdEncoding.DecodeString("F+tlXjWffI4xt656KVwgLg==")
key = []byte("12345123451234512345123451234512")
dst, err = AesECBDecrypt(src, key, PKCS7_PADDING)
assert.NoError(t, err)
t.Log(string(dst))
assert.Equal(t, dst, []byte("123456"))
}
func TestAesCBCEncrypt(t *testing.T) {
src := []byte("123456")
iv := []byte("6789067890678906")
// AES-128-ECB, PKCS7_PADDING
key := []byte("1234512345123451")
dst, err := AesCBCEncrypt(src, key, iv, PKCS7_PADDING)
assert.NoError(t, err)
t.Log(base64.StdEncoding.EncodeToString(dst))
assert.Equal(t, base64.StdEncoding.EncodeToString(dst), "0huM2ppRyYZmZnzdoCL/tA==")
}
func TestAesCBCDecrypt(t *testing.T) {
src, err := base64.StdEncoding.DecodeString("0huM2ppRyYZmZnzdoCL/tA==")
assert.NoError(t, err)
iv := []byte("6789067890678906")
// AES-128-ECB, PKCS7_PADDING
key := []byte("1234512345123451")
dst, err := AesCBCDecrypt(src, key, iv, PKCS7_PADDING)
assert.NoError(t, err)
t.Log(string(dst))
assert.Equal(t, dst, []byte("123456"))
}
func Test_aesKeyPending(t *testing.T) {
type args struct {
key []byte
}
tests := []struct {
name string
args args
want []byte
}{
{name: "key < 16", args: args{key: []byte("12354678901234")}, want: append([]byte("12354678901234"), []byte{0, 0}...)},
{name: "key < 24", args: args{key: []byte("1235467890123546789012")}, want: append([]byte("1235467890123546789012"), []byte{0, 0}...)},
{name: "key < 32", args: args{key: []byte("123546789012354678901235467890")}, want: append([]byte("123546789012354678901235467890"), []byte{0, 0}...)},
{name: "key > 32", args: args{key: []byte("1235467890123546789012354678901234")}, want: []byte("12354678901235467890123546789012")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := aesKeyPending(tt.args.key); !reflect.DeepEqual(got, tt.want) {
t.Errorf("aesKeyPending() = %v, want %v", got, tt.want)
}
})
}
}