/
S1C4.go
59 lines (42 loc) · 972 Bytes
/
S1C4.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
package main
import (
"bufio"
"fmt"
"github.com/scripterio/go-cryptopals/bitwise"
"github.com/scripterio/go-cryptopals/hexstring"
"os"
)
// readLines reads a whole file into memory
// and returns a slice of its lines.
func readLines(path string) []string {
file, _ := os.Open(path)
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines
}
func main() {
lines := readLines("./data/4.txt")
topscore := 0
var topoutput []byte
for _, line := range lines {
raw := hexstring.Decode(line)
bruteout := bitwise.BRUTEXOR(raw)
score, key := hexstring.ScoreEnglish(bruteout)
if score >= topscore {
topscore = score
topoutput = bruteout[key]
}
}
output := string(topoutput)
//Test
fmt.Printf("Set 1 Challenge 4 \n")
if output == "one day this nation will rise\n" {
fmt.Printf("PASS!!\n\n")
} else {
fmt.Printf("##FAIL##\n\n")
}
}