summaryrefslogtreecommitdiffstats
path: root/lib/brute.go
blob: 5210df1e0cf14aac686056a25585a5decfb2b6dd (plain) (blame)
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
// Copyright © 2020 rsiddharth <s@ricketyspace.net>
// SPDX-License-Identifier: ISC

package lib

// Average Word Length (English).
const awl float64 = 4.7

// 'hs' must be a hex encoded string.
func XORCrackSingleKey(hs string) (byte, string, float64) {
	l := len(hs) / 2

	var k byte = 0
	var ds string = ""
	var scr float64 = 100.0

	i := byte(0)
	as := []byte(HexStrToAsciiStr(hs))
	for i < 255 {
		ks := FillBytes(i, l)
		xs := FixedXORBytes(as, ks)

		s := phraseScore(string(xs))
		if s < scr {
			k = i
			ds = string(xs)
			scr = s
		}
		i += 1
	}
	return k, ds, scr
}

func phraseScore(phrase string) float64 {
	pl := len(phrase)

	// Expected number of words.
	ew := float64(pl) / awl

	// Number of words in phrase.
	ws := 0.0

	for i := 0; i < pl; i++ {
		if phrase[i] == ' ' {
			ws += 1.0
		}
	}
	ws += 1.0

	// Compute score.
	score := 1.0 - (ws / ew)
	if score < 0 {
		score *= -1
	}
	return score
}