-
Notifications
You must be signed in to change notification settings - Fork 0
/
chal6_test.go
55 lines (46 loc) · 1.13 KB
/
chal6_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
package set1_test
import (
"cryptopals/internal/common"
"cryptopals/pkg/xor"
"encoding/base64"
"os"
"testing"
)
func TestHammingDistance(t *testing.T) {
s1 := []byte("this is a test")
s2 := []byte("wokka wokka!!!")
dist, err := common.HammingDistance(s1, s2)
if err != nil {
t.Fatal(err)
}
expectedDist := 37
if dist != expectedDist {
t.Fatalf("Wrong output.\nHave: %d\nNeed: %d\n", dist, expectedDist)
}
}
func TestChal6_BreakRepeatingKeyXOR(t *testing.T) {
// t.Skip("skip: test is a bit long")
// read file (base64 encoded)
fileb64, err := os.ReadFile("../../res/set1/6.txt")
if err != nil {
t.Fatal(err)
}
// decode
ct, err := base64.StdEncoding.DecodeString(string(fileb64))
if err != nil {
t.Fatal(err)
}
// decipher
pt, key, err := xor.RepeatingKeyXORDecipher(ct)
if err != nil {
t.Fatal(err)
}
t.Log(string(pt), "\n")
expectedKey := "Terminator X: Bring the noise"
// there are two answers if you include capital letters too!
// key: teRmINaTORx:brINGthENOISE
// key: Terminator X: Bring the noise
if string(key) != expectedKey {
t.Fatalf("Wrong output.\nHave: %s\nNeed: %s\n", key, expectedKey)
}
}