-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcipher_test.go
154 lines (139 loc) · 2.76 KB
/
cipher_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
package ws
import (
"fmt"
"math/rand"
"reflect"
"testing"
)
func TestCipher(t *testing.T) {
for i, test := range []struct {
in []byte
mask [4]byte
}{
{
in: []byte("Hello, XOR!"),
mask: [4]byte{1, 2, 3, 4},
},
{
in: []byte("Hello, XOR!"),
mask: [4]byte{255, 255, 255, 255},
},
} {
t.Run(fmt.Sprintf("#%d", i), func(t *testing.T) {
// naive implementation of xor-cipher
exp := cipherNaive(test.in, test.mask, 0)
res := make([]byte, len(test.in))
copy(res, test.in)
Cipher(res, test.mask, 0)
if !reflect.DeepEqual(res, exp) {
t.Errorf("Cipher(%v, %v):\nact:\t%v\nexp:\t%v\n", test.in, test.mask, res, exp)
}
})
}
}
func TestCipherChops(t *testing.T) {
for n := 2; n <= 1024; n <<= 1 {
t.Run(fmt.Sprintf("%d", n), func(t *testing.T) {
p := make([]byte, n)
b := make([]byte, n)
var m [4]byte
_, err := rand.Read(p)
if err != nil {
t.Fatal(err)
}
_, err = rand.Read(m[:])
if err != nil {
t.Fatal(err)
}
exp := cipherNaive(p, m, 0)
for i := 1; i <= n; i <<= 1 {
copy(b, p)
s := n / i
for j := s; j <= n; j += s {
l, r := j-s, j
Cipher(b[l:r], m, l)
if !reflect.DeepEqual(b[l:r], exp[l:r]) {
t.Errorf("unexpected Cipher([%d:%d]) = %x; want %x", l, r, b[l:r], exp[l:r])
return
}
}
}
l := 0
copy(b, p)
for l < n {
r := rand.Intn(n-l) + l + 1
Cipher(b[l:r], m, l)
if !reflect.DeepEqual(b[l:r], exp[l:r]) {
t.Errorf("unexpected Cipher([%d:%d]):\nact:\t%v\nexp:\t%v\nact:\t%#x\nexp:\t%#x\n\n", l, r, b[l:r], exp[l:r], b[l:r], exp[l:r])
return
}
l = r
}
})
}
}
func cipherNaive(p []byte, m [4]byte, pos int) []byte {
r := make([]byte, len(p))
copy(r, p)
cipherNaiveNoCp(r, m, pos)
return r
}
func cipherNaiveNoCp(p []byte, m [4]byte, pos int) []byte {
for i := 0; i < len(p); i++ {
p[i] ^= m[(pos+i)%4]
}
return p
}
func BenchmarkCipher(b *testing.B) {
for _, bench := range []struct {
size int
offset int
}{
{
size: 7,
offset: 1,
},
{
size: 125,
},
{
size: 1024,
},
{
size: 4096,
},
{
size: 4100,
offset: 4,
},
{
size: 4099,
offset: 3,
},
{
size: (1 << 15) + 7,
offset: 49,
},
} {
bts := make([]byte, bench.size)
_, err := rand.Read(bts)
if err != nil {
b.Fatal(err)
}
var mask [4]byte
_, err = rand.Read(mask[:])
if err != nil {
b.Fatal(err)
}
//b.Run(fmt.Sprintf("naive_bytes=%d;offset=%d", bench.size, bench.offset), func(b *testing.B) {
// for i := 0; i < b.N; i++ {
// cipherNaiveNoCp(bts, mask, bench.offset)
// }
//})
b.Run(fmt.Sprintf("bytes=%d;offset=%d", bench.size, bench.offset), func(b *testing.B) {
for i := 0; i < b.N; i++ {
Cipher(bts, mask, bench.offset)
}
})
}
}