Solve set 1 challenge 4, and add forgotten sample text file
[sandbox] / cryptopals-python / cryptopals.py
1 import codecs
2 import unittest
3
4 def base64_from_hex(_hex):
5     return codecs.encode(bytes.fromhex(_hex), 'base64').decode('utf-8')
6
7 def xor_bytes(bytes0, bytes1):
8     return bytes(b0 ^ b1 for b0, b1 in zip(bytes0, bytes1))
9
10 def xor_hex(hex0, hex1):
11     assert len(hex0) == len(hex1)
12     bytes0 = bytes.fromhex(hex0)
13     bytes1 = bytes.fromhex(hex1)
14     return codecs.encode(xor_bytes(bytes0, bytes1), 'hex').decode('utf-8')
15
16 def get_character_frequencies(source):
17     frequencies = {}
18
19     for source_character in source:
20         frequencies[source_character] = frequencies.get(source_character, 0) + 1
21
22     return frequencies
23
24 def compare_frequency_deviation(base_frequency, comparison_frequency):
25     return sum(
26         abs(frequency - comparison_frequency.get(character, 0))
27         for character, frequency in base_frequency.items()
28     ) / len(base_frequency)
29
30 with open('sample.txt','r') as sample_file:
31     sample_text = sample_file.read()
32
33 SAMPLE_FREQUENCIES = get_character_frequencies(sample_text)
34
35 class Set1Challenge1Tests(unittest.TestCase):
36     def test_converts_hex_to_base64(self):
37         expected = 'SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t\n'
38         actual = base64_from_hex('49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d')
39         self.assertEqual(expected, actual)
40
41 class Set1Challenge2Tests(unittest.TestCase):
42     def test_xors_hex_strings(self):
43         hex0 = '1c0111001f010100061a024b53535009181c'
44         hex1 = '686974207468652062756c6c277320657965'
45
46         expected = '746865206b696420646f6e277420706c6179'
47         actual = xor_hex(hex0, hex1)
48
49         self.assertEqual(expected, actual)
50
51 class Set1Challenge3Tests(unittest.TestCase):
52     def test_gets_message(self):
53         xored_string = bytes.fromhex('1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736')
54
55         lowest_frequency_deviation_string = None
56         lowest_frequency_deviation = None
57
58         for i in range(128):
59             key_char = bytes([i]).decode('utf-8')
60             key = bytes([i]) * len(xored_string)
61             try_string = xor_bytes(xored_string, key).decode('utf-8')
62             try_string_frequency_deviation = compare_frequency_deviation(
63                 SAMPLE_FREQUENCIES,
64                 get_character_frequencies(try_string),
65             )
66
67             if lowest_frequency_deviation is None or try_string_frequency_deviation < lowest_frequency_deviation:
68                 lowest_frequency_deviation_string = try_string
69                 lowest_frequency_deviation = try_string_frequency_deviation
70
71
72         expected = "Cooking MC's like a pound of bacon"
73         actual = lowest_frequency_deviation_string
74
75         self.assertEqual(expected, actual)
76
77 class Set1Challenge4Tests(unittest.TestCase):
78     def test_gets_message(self):
79         with open('set1challenge4.txt','r') as f:
80             lines = f.readlines()
81
82
83         lowest_frequency_deviation_string = None
84         lowest_frequency_deviation = None
85
86         for line in lines:
87             line_bytes = bytes.fromhex(line)
88
89             for i in range(128):
90                 key_char = bytes([i]).decode('utf-8')
91                 key = bytes([i]) * len(line_bytes)
92
93                 try:
94                     try_string = xor_bytes(line_bytes, key).decode('utf-8')
95                     try_string_frequency_deviation = compare_frequency_deviation(
96                         SAMPLE_FREQUENCIES,
97                         get_character_frequencies(try_string),
98                     )
99
100                     if lowest_frequency_deviation is None or try_string_frequency_deviation < lowest_frequency_deviation:
101                         lowest_frequency_deviation_string = try_string
102                         lowest_frequency_deviation = try_string_frequency_deviation
103                 except:
104                     pass
105
106         expected = 'Now that the party is jumping\n'
107         actual = lowest_frequency_deviation_string
108
109         self.assertEqual(expected, actual)
110
111 if __name__ == '__main__':
112     unittest.main()