-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset1_test.go
More file actions
90 lines (81 loc) · 2.91 KB
/
set1_test.go
File metadata and controls
90 lines (81 loc) · 2.91 KB
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
package cryptopals
import (
"bufio"
xbytes "bytes"
"os"
"testing"
"github.com/jjshanks/cryptopals/internal/pkg/base64"
"github.com/jjshanks/cryptopals/internal/pkg/bitwise"
"github.com/jjshanks/cryptopals/internal/pkg/cryptanalysis"
"github.com/jjshanks/cryptopals/internal/pkg/hex"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestChallenge1(t *testing.T) {
input := "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"
byteInput, err := hex.Decode(input)
require.NoError(t, err)
expectedOutput := "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"
assert.Equal(t, expectedOutput, base64.Encode(byteInput))
}
func TestChallenge2(t *testing.T) {
left := "1c0111001f010100061a024b53535009181c"
leftBytes, err := hex.Decode(left)
leftBuffer := xbytes.NewBuffer(leftBytes)
require.NoError(t, err)
right := "686974207468652062756c6c277320657965"
rightBytes, err := hex.Decode(right)
rightBuffer := xbytes.NewBuffer(rightBytes)
require.NoError(t, err)
expected := "746865206b696420646f6e277420706c6179"
expectedBytes, err := hex.Decode(expected)
require.NoError(t, err)
actual, err := bitwise.FixedXOR(leftBuffer, rightBuffer)
require.NoError(t, err)
assert.Equal(t, expectedBytes, actual)
}
func TestChallenge3(t *testing.T) {
input := "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736"
inputBytes, err := hex.Decode(input)
require.NoError(t, err)
solution, err := cryptanalysis.SingleCharXOR(inputBytes)
require.NoError(t, err)
assert.Equal(t, "Cooking MC's like a pound of bacon", solution.Text)
}
func TestChallenge4(t *testing.T) {
file, err := os.Open("data/challenge4.txt")
require.NoError(t, err)
defer file.Close()
bestSolution := cryptanalysis.XORCryptanalysisSolution{
Score: 0,
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
inputBytes, err := hex.Decode(line)
require.NoError(t, err)
solution, err := cryptanalysis.SingleCharXOR(inputBytes)
require.NoError(t, err)
if bestSolution.Score < solution.Score {
bestSolution = solution
}
}
require.NoError(t, scanner.Err())
assert.Equal(t, "Now that the party is jumping\n", bestSolution.Text)
}
func TestChallenge5(t *testing.T) {
input := "Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal"
key := "ICE"
actual, err := bitwise.RepeatingXOR([]byte(input), []byte(key))
require.NoError(t, err)
expected, err := hex.Decode("0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f")
require.NoError(t, err)
assert.Equal(t, expected, actual)
}
func TestChallenge6(t *testing.T) {
file, err := os.Open("data/challenge4.txt")
require.NoError(t, err)
defer file.Close()
bestKeySize, err := cryptanalysis.RepeatingXORKeySize(file, 2, 40, 1)
assert.Equal(t, []int{7}, bestKeySize)
}