-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmcrypt_test.go
54 lines (47 loc) · 1.3 KB
/
mcrypt_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
package mcrypt
import (
"reflect"
"testing"
)
type encryptionTest struct {
algo string
mode string
key string
ivSize int
plaintext string
encrypted []byte
}
var tests = []encryptionTest{
{"cast-256", "ecb", "here is a random key of 32 bytes", 16, "hello", []byte{53, 190, 73, 3, 192, 5, 42, 202, 55, 77, 85, 218, 189, 169, 253, 147}},
{"rijndael-256", "cbc", "here is a random key of 32 bytes", 32, "hello", []byte{196, 120, 32, 94, 41, 142, 113, 20, 109, 123, 95, 30, 255, 227, 63, 219, 32, 175, 35, 54, 65, 24, 130, 254, 2, 88, 201, 226, 1, 2, 235, 252}},
}
func TestEncrypt(t *testing.T) {
for _, pair := range tests {
key := []byte(pair.key)
plaintext := []byte(pair.plaintext)
iv := make([]byte, pair.ivSize)
s, _ := Encrypt(key, iv, plaintext, pair.algo, pair.mode)
if !reflect.DeepEqual(s, pair.encrypted) {
t.Error(
"For", pair.plaintext,
"expected", pair.encrypted,
"got", s,
)
}
}
}
func TestDecrypt(t *testing.T) {
for _, pair := range tests {
key := []byte(pair.key)
iv := make([]byte, pair.ivSize)
s, _ := Decrypt(key, iv, pair.encrypted, pair.algo, pair.mode)
bytesPlaintext := []byte(pair.plaintext)
if !reflect.DeepEqual(s, bytesPlaintext) {
t.Error(
"For", pair.encrypted,
"expected", bytesPlaintext,
"got", s,
)
}
}
}