-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevp_test.go
85 lines (76 loc) · 2.22 KB
/
evp_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
package evp
import (
"encoding/hex"
"testing"
)
const (
blockLen int = 16
keyLen int = 32
goodKeyMD5 string = "fdbdf3419fff98bdb0241390f62a9db35f4aba29d77566377997314ebfc709f2"
goodIVMD5 string = "0b5ca7b1081f94b1ac12e3c8ba87d05a"
goodKey string = "0c8cde87480244c4d1bbd7401f70b7aebedf5a4453d01a7665db51aaf4d7dd72"
goodIV string = "6dea80f410c6fa07183a01eed7efcf6e"
password string = "password"
)
var (
badSalt = []byte("salt")
goodSalt = []byte("saltsalt")
)
func bytesToHex(b []byte) string {
return hex.EncodeToString(b)
}
func getOutputs(key, iv []byte) (hexKey, hexIV string, lenKey, lenIV int) {
hexKey = bytesToHex(key)
hexIV = bytesToHex(iv)
lenKey = len(key)
lenIV = len(iv)
return
}
func TestGoodSalt(t *testing.T) {
key, iv := BytesToKeyAES256CBC(goodSalt, []byte(password))
hexKey, hexIV, keyLength, ivLength := getOutputs(key, iv)
if hexKey != goodKey {
t.Fatalf("Wanted key '%s', got '%s'\n", goodKey, hexKey)
}
if hexIV != goodIV {
t.Fatalf("Wanted IV '%s', got '%s'\n", goodIV, hexIV)
}
if keyLength != 32 {
t.Fatalf("Wanted key length %d, got %d\n", 32, keyLength)
}
if ivLength != 16 {
t.Fatalf("Wanted IV length %d, got %d\n", 16, ivLength)
}
}
func TestGoodSaltMD5(t *testing.T) {
key, iv := BytesToKeyAES256CBCMD5(goodSalt, []byte(password))
hexKey, hexIV, keyLength, ivLength := getOutputs(key, iv)
if hexKey != goodKeyMD5 {
t.Fatalf("Wanted key '%s', got '%s'\n", goodKeyMD5, hexKey)
}
if hexIV != goodIVMD5 {
t.Fatalf("Wanted IV '%s', got '%s'\n", goodIVMD5, hexIV)
}
if keyLength != 32 {
t.Fatalf("Wanted key length %d, got %d\n", 32, keyLength)
}
if ivLength != 16 {
t.Fatalf("Wanted IV length %d, got %d\n", 16, ivLength)
}
}
func TestBadSalt(t *testing.T) {
defer func() { recover() }()
key, iv := BytesToKeyAES256CBC(badSalt, []byte(password))
getOutputs(key, iv)
t.Fatalf("Expected a panic due to invalid salt length but one did not occur")
}
func TestBadPassword(t *testing.T) {
key, iv := BytesToKeyAES256CBC(goodSalt, []byte("badpassword"))
hexKey, hexIV, _, _ := getOutputs(key, iv)
if hexKey == goodKey {
t.Fatalf("Got a valid key using an invalid password!")
}
if hexIV == goodIV {
t.Fatalf("Got a valid IV using an invalid password!")
}
}