This repository has been archived by the owner on Nov 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
crypto_test.go
82 lines (74 loc) · 2.14 KB
/
crypto_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
package kerb
import (
"bytes"
"encoding/hex"
"testing"
)
func mustHexDecode(str string) []byte {
d, err := hex.DecodeString(str)
if err != nil {
panic(err)
}
return d
}
var desStringTests = []struct {
salt, pass, key string
}{
{"ATHENA.MIT.EDUraeburn", "password", "cbc22fae235298e3"},
{"WHITEHOUSE.GOVdanny", "potatoe", "df3d32a74fd92a01"},
{"EXAMPLE.COMpianist", "\U0001D11E", "4ffb26bab0cd9413"},
{"ATHENA.MIT.EDUJuri\u0161i\u0107", "\u00df", "62c81a5232b5e69d"},
{"AAAAAAAA", "11119999", "984054d0f1a73e31"},
{"FFFFAAAA", "NNNN6666", "c4bf6b25adf7a4f8"},
}
func TestDesStringKey(t *testing.T) {
for i, d := range desStringTests {
key := desStringKey(d.pass, d.salt)
if !bytes.Equal(key, mustHexDecode(d.key)) {
t.Errorf("Test %d failed, got %x expected %s\n", i, key, d.key)
}
}
}
var gssDesTests = []struct {
data, key, out string
}{
{"7654321 Now is the time for ", "0123456789abcdef", "f1d30f6849312ca4"},
}
func TestGssDes(t *testing.T) {
for i, d := range gssDesTests {
k := mustLoadKey(cryptDesCbcMd5, mustHexDecode(d.key))
chk, err := k.Sign(signGssDes, 0, []byte(d.data))
if err != nil {
t.Errorf("Test %d failed %v\n", i, err)
}
if !bytes.Equal(chk, mustHexDecode(d.out)) {
t.Errorf("Test %d failed got %x expected %s\n", i, chk, d.out)
}
}
}
var cryptTests = []struct {
algo int
key string
data string
}{
{cryptDesCbcMd5, "cbc22fae235298e3", "0123456789abcdef"},
{cryptDesCbcMd5, "cbc22fae235298e3", "0123456789"},
{cryptDesCbcMd5, "cbc22fae235298e3", "0123456789abcdef0123"},
{cryptDesCbcMd4, "cbc22fae235298e3", "0123456789abcdef"},
{cryptDesCbcMd4, "cbc22fae235298e3", "0123456789"},
{cryptDesCbcMd4, "cbc22fae235298e3", "0123456789abcdef0123"},
}
func TestCrypt(t *testing.T) {
for i, d := range cryptTests {
key := mustLoadKey(d.algo, mustHexDecode(d.key))
enc := key.Encrypt(nil, paEncryptedTimestampKey, mustHexDecode(d.data))
dec, err := key.Decrypt(nil, d.algo, paEncryptedTimestampKey, enc)
if err != nil {
t.Errorf("Test %d failed %v\n", i, err)
continue
}
if !bytes.HasPrefix(dec, mustHexDecode(d.data)) {
t.Errorf("Test %d failed got %x expected %s\n", i, dec, d.data)
}
}
}