-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfpe_test.go
172 lines (145 loc) · 5.83 KB
/
fpe_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package feistel_test
import (
"testing"
"github.com/cyrildever/feistel"
"github.com/cyrildever/feistel/common/utils/base256"
"github.com/cyrildever/feistel/common/utils/hash"
"github.com/cyrildever/feistel/exception"
utls "github.com/cyrildever/go-utls/common/utils"
"gotest.tools/assert"
)
// TestFPEEncrypt ...
func TestFPEEncrypt(t *testing.T) {
expected := "K¡(#q|r5*"
cipher := feistel.NewFPECipher(hash.SHA_256, "8ed9dcc1701c064f0fd7ae235f15143f989920e0ee9658bb7882c8d7d5f05692", 10)
found, err := cipher.Encrypt("Edgewhere")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, found.Len(), len("Edgewhere"))
assert.Equal(t, found.String(), expected)
assert.Equal(t, found.ToHex(), "2a5d07024f5a501409")
blake2, _ := feistel.NewFPECipher(hash.BLAKE2b, "8ed9dcc1701c064f0fd7ae235f15143f989920e0ee9658bb7882c8d7d5f05692", 10).Encrypt("Edgewhere")
assert.Assert(t, blake2.String() != expected)
expectedBlake2 := "¼u*$q0up¢"
assert.Equal(t, blake2.String(), expectedBlake2)
}
// TestFPEDecrypt ...
func TestFPEDecrypt(t *testing.T) {
nonFPE := "Edgewhere"
cipher := feistel.NewFPECipher(hash.SHA_256, "8ed9dcc1701c064f0fd7ae235f15143f989920e0ee9658bb7882c8d7d5f05692", 10)
found, err := cipher.Decrypt(base256.ToBase256Readable(utls.Must(utls.FromHex("3d7c0a0f51415a521054"))))
if err != nil {
t.Fatal(err)
}
assert.Equal(t, found, nonFPE)
ref := base256.Readable("K¡(#q|r5*")
expected := "Edgewhere"
found, err = cipher.Decrypt(base256.ToBase256Readable(utls.Must(utls.FromHex("2a5d07024f5a501409"))))
if err != nil {
t.Fatal(err)
}
assert.Equal(t, found, expected)
found, _ = cipher.Decrypt(ref)
assert.Equal(t, found, expected)
b256, err := base256.HexToReadable("2a5d07024f5a501409")
if err != nil {
t.Fatal(err)
}
assert.DeepEqual(t, b256, ref)
fromBlake2 := base256.Readable("¼u*$q0up¢")
blake2, _ := feistel.NewFPECipher(hash.BLAKE2b, "8ed9dcc1701c064f0fd7ae235f15143f989920e0ee9658bb7882c8d7d5f05692", 10).Decrypt(fromBlake2)
assert.Equal(t, blake2, expected)
}
// TestFPEEncryptDecrypt ...
func TestFPEEncryptDecrypt(t *testing.T) {
ref := "Edgewhere"
cipher := feistel.NewFPECipher(hash.SHA_256, "8ed9dcc1701c064f0fd7ae235f15143f989920e0ee9658bb7882c8d7d5f05692", 255)
encrypted, err := cipher.Encrypt(ref)
if err != nil {
t.Fatal(err)
}
decrypted, err := cipher.Decrypt(encrypted)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, decrypted, ref)
}
// TestReadableString ...
func TestReadableString(t *testing.T) {
source := "my-source-data"
cipher := feistel.NewFPECipher(hash.SHA_256, "some-32-byte-long-key-to-be-safe", 128)
obfuscated, err := cipher.EncryptString(source)
assert.DeepEqual(t, obfuscated.Bytes(), []byte{62, 125, 126, 123, 99, 124, 118, 109, 108, 121, 97, 49, 33, 101})
assert.NilError(t, err)
strAscii := obfuscated.String(true)
assert.Equal(t, strAscii, ">}~{c|vmlya1!e") // Fully readable because, by some chance, the underlying byte slice has all bytes ranging from 33 to 126 (see above)
assert.Equal(t, len(source), len(strAscii))
assert.DeepEqual(t, len(obfuscated.Bytes()), len(source)) // Always true
strNotAscii := obfuscated.String()
assert.Equal(t, strNotAscii, "`ÃÄÁ§Â¼²±¿¥RB©")
assert.Assert(t, len(source) != len(strNotAscii)) // Because of the use of multi-byte encoded characters to make them readable
assert.Equal(t, len(source), len([]rune(strNotAscii)))
deciphered, err := cipher.DecryptString(obfuscated)
assert.NilError(t, err)
assert.Equal(t, deciphered, source)
}
// TestReadableNumber ...
func TestReadableNumber(t *testing.T) {
source := uint64(123456789)
cipher := feistel.NewFPECipher(hash.SHA_256, "some-32-byte-long-key-to-be-safe", 128)
obfuscated, err := cipher.EncryptNumber(source)
assert.NilError(t, err)
assert.Equal(t, obfuscated.Uint64(), uint64(22780178))
assert.Equal(t, obfuscated.ToNumber(), "22780178")
assert.Equal(t, obfuscated.ToNumber(9), "022780178")
deobfuscated, err := cipher.DecryptNumber(obfuscated)
assert.NilError(t, err)
assert.Equal(t, deobfuscated, source)
// Numbers below 256 don't preserve length during encryption
source = uint64(123)
obfuscated, err = cipher.EncryptNumber(source)
assert.Error(t, err, "too small to preserve length") // Hence the error
_, ok := err.(*exception.TooSmallToPreserveLengthError)
assert.Assert(t, ok)
assert.Equal(t, obfuscated.Uint64(), uint64(24359))
assert.Equal(t, obfuscated.ToNumber(), "24359")
obfuscated, _ = base256.NumberToReadable(24359)
deobfuscated, err = cipher.DecryptNumber(obfuscated)
assert.NilError(t, err)
assert.Equal(t, deobfuscated, source)
source = uint64(18446744073709551615) // max accepted uint64 value in Go
obfuscated, err = cipher.EncryptNumber(source)
assert.NilError(t, err)
assert.Equal(t, obfuscated.Uint64(), uint64(17630367666640955566))
assert.Equal(t, obfuscated.ToNumber(), "17630367666640955566")
maxJSUnit := uint64(9007199254740991) // JavaScript's unsigned max value
source = maxJSUnit
obfuscated, _ = cipher.EncryptNumber(source)
assert.Equal(t, obfuscated.Uint64(), uint64(7873237593534198))
assert.Equal(t, obfuscated.ToNumber(), "7873237593534198")
deobfuscated, err = cipher.DecryptNumber(obfuscated)
assert.NilError(t, err)
assert.Equal(t, deobfuscated, maxJSUnit)
source = uint64(0)
obfuscated, err = cipher.EncryptNumber(source)
_, ok = err.(*exception.TooSmallToPreserveLengthError)
assert.Assert(t, ok)
assert.Equal(t, obfuscated.Uint64(), uint64(0))
obfuscated, _ = base256.NumberToReadable(0)
deobfuscated, err = cipher.DecryptNumber(obfuscated)
assert.NilError(t, err)
assert.Equal(t, deobfuscated, uint64(0))
}
func BenchmarkEncrypt(b *testing.B) {
cipher := feistel.NewFPECipher(hash.SHA_256, "8ed9dcc1701c064f0fd7ae235f15143f989920e0ee9658bb7882c8d7d5f05692", 10)
b.ReportAllocs()
b.ResetTimer()
b.StartTimer()
for i := 0; i < b.N; i++ {
_, err := cipher.Encrypt("Edgewhere")
if err != nil {
b.Fatal(err)
}
}
}