Set 1, challenge 3
[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 class Set1Challenge1Tests(unittest.TestCase):
31     def test_converts_hex_to_base64(self):
32         expected = 'SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t\n'
33         actual = base64_from_hex('49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d')
34         self.assertEqual(expected, actual)
35
36 class Set1Challenge2Tests(unittest.TestCase):
37     def test_xors_hex_strings(self):
38         hex0 = '1c0111001f010100061a024b53535009181c'
39         hex1 = '686974207468652062756c6c277320657965'
40
41         expected = '746865206b696420646f6e277420706c6179'
42         actual = xor_hex(hex0, hex1)
43
44         self.assertEqual(expected, actual)
45
46 class Set1Challenge3Tests(unittest.TestCase):
47     def test_gets_message(self):
48         with open('sample.txt','r') as sample_file:
49             sample_text = sample_file.read()
50
51         sample_frequencies = get_character_frequencies(sample_text)
52
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 if __name__ == '__main__':
78     unittest.main()