-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom_test.go
62 lines (56 loc) · 2.01 KB
/
custom_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
package feistel_test
import (
"testing"
"github.com/cyrildever/feistel"
"github.com/cyrildever/feistel/exception"
utls "github.com/cyrildever/go-utls/common/utils"
"gotest.tools/assert"
)
// TestCustomCipher ...
func TestCustomCipher(t *testing.T) {
ref := "Edgewhere"
// Identical to cipher_test.go#TestEncrypt
keys := []string{
"8ed9dcc1701c064f0fd7ae235f15143f989920e0ee9658bb7882c8d7d5f05692",
"8ed9dcc1701c064f0fd7ae235f15143f989920e0ee9658bb7882c8d7d5f05692",
"8ed9dcc1701c064f0fd7ae235f15143f989920e0ee9658bb7882c8d7d5f05692",
"8ed9dcc1701c064f0fd7ae235f15143f989920e0ee9658bb7882c8d7d5f05692",
"8ed9dcc1701c064f0fd7ae235f15143f989920e0ee9658bb7882c8d7d5f05692",
"8ed9dcc1701c064f0fd7ae235f15143f989920e0ee9658bb7882c8d7d5f05692",
"8ed9dcc1701c064f0fd7ae235f15143f989920e0ee9658bb7882c8d7d5f05692",
"8ed9dcc1701c064f0fd7ae235f15143f989920e0ee9658bb7882c8d7d5f05692",
"8ed9dcc1701c064f0fd7ae235f15143f989920e0ee9658bb7882c8d7d5f05692",
"8ed9dcc1701c064f0fd7ae235f15143f989920e0ee9658bb7882c8d7d5f05692",
}
expected := "3d7c0a0f51415a521054"
cipher := feistel.NewCustomCipher(keys)
found, err := cipher.Encrypt(ref)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, utls.ToHex(found), expected)
// Another test with custom keys
keys = []string{
"1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
"9876543210fedcba9876543210fedcba9876543210fedcba9876543210fedcba",
"abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
}
expected = "445951465c5a19613633"
cipher = feistel.NewCustomCipher(keys)
found, err = cipher.Encrypt(ref)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, utls.ToHex(found), expected)
deciphered, err := cipher.Decrypt(found)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, deciphered, ref)
// Not enough keys
cipher = feistel.NewCustomCipher([]string{})
_, err = cipher.Encrypt(ref)
assert.Error(t, err, "wrong cipher parameters: keys and rounds can't be null")
_, ok := err.(*exception.WrongCipherParametersError)
assert.Assert(t, ok)
}