-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes_test.go
56 lines (44 loc) · 1.35 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
package main
import (
"crypto/aes"
"testing"
)
func TestEncryptAES128CBC(t *testing.T) {
planTextMessage := "Burning 'em, if you ain't quick and nimble\nI go crazy when I hea"
key := []byte("Yellow submarine")
block, err := aes.NewCipher(key)
if err != nil {
panic("cannot create cipher")
}
vi, err := GenRandomBytes(16)
if err != nil {
panic("cannot crate iv")
}
cryptedMessage := make([]byte, len(planTextMessage))
resultMessage := make([]byte, len(planTextMessage))
EncryptAES128CBC(block, cryptedMessage, []byte(planTextMessage), vi)
DecryptAES128CBC(block, resultMessage, cryptedMessage, vi)
if string(resultMessage) != planTextMessage {
t.Error("innocorrect result", resultMessage)
}
}
func TestAddPaddingToBlock(t *testing.T) {
input := []byte("YELLOW SUBMARINE")
result := AddPaddingToBlock(input, 20)
if len(result) != 20 {
t.Error("innocorrect size of result", result)
}
if string(result) != "YELLOW SUBMARINE\x04\x04\x04\x04" {
t.Error("innocorrect result", result)
}
}
func TestAddPaddingToBlock_OverBlockSize(t *testing.T) {
input := []byte("YELLOW SUBMARINE|YELLOW SUBMARINE")
result := AddPaddingToBlock(input, 20)
if len(result) != 40 {
t.Error("innocorrect size of result", result)
}
if string(result) != "YELLOW SUBMARINE|YELLOW SUBMARINE\x07\x07\x07\x07\x07\x07\x07" {
t.Error("innocorrect result", result)
}
}