-
Notifications
You must be signed in to change notification settings - Fork 0
/
c6_test.go
85 lines (75 loc) · 1.82 KB
/
c6_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 main
import (
"encoding/base64"
"io"
"os"
"slices"
"testing"
)
func TestBreakRepeatingKeyXOR(t *testing.T) {
f, err := os.Open("./files/1_6.txt")
if err != nil {
t.Fatalf("opening file: %s", err)
}
defer f.Close()
decoder := base64.NewDecoder(base64.StdEncoding, f)
cipherText, err := io.ReadAll(decoder)
if err != nil {
t.Fatalf("reading file: %s", err)
}
var maxKeySize = 40
plainText, key, err := breakRepeatingKeyXOR(cipherText, maxKeySize)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
t.Logf("Key: %s", key)
t.Logf("Key Size: %d", len(key))
t.Logf("Plain-text:\n%s", plainText)
}
func TestHammingDistance(t *testing.T) {
var (
inputText1 = "this is a test"
inputText2 = "wokka wokka!!!"
wantDistance = 37
)
gotDistance, err := hammingDistance([]byte(inputText1), []byte(inputText2))
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if gotDistance != wantDistance {
t.Errorf("expected Hamming distance %d but got %d", wantDistance, gotDistance)
}
}
func TestTranspose(t *testing.T) {
var (
cipherText = []byte{
'a', 'b', 'c', 'd', 'e',
'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o',
}
keySize = 3
cipherTextLen = len(cipherText)
transposed = make([]byte, cipherTextLen)
nBlocks = (cipherTextLen + keySize - 1) / keySize
)
for blockIdx := 0; blockIdx < nBlocks; blockIdx++ {
for byteIdx := 0; byteIdx < keySize; byteIdx++ {
var (
index = blockIdx*keySize + byteIdx
transposedIndex = byteIdx*nBlocks + blockIdx
)
if index >= cipherTextLen {
break
}
transposed[transposedIndex] = cipherText[index]
}
}
want := []byte{
'a', 'd', 'g', 'j', 'm',
'b', 'e', 'h', 'k', 'n',
'c', 'f', 'i', 'l', 'o',
}
if !slices.Equal(transposed, want) {
t.Errorf("want: %c, but got %c", want, transposed)
}
}