+ actual = xor_hex(hex0, hex1)
+
+ self.assertEqual(expected, actual)
+
+class Set1Challenge3Tests(unittest.TestCase):
+ def test_gets_message(self):
+ with open('sample.txt','r') as sample_file:
+ sample_text = sample_file.read()
+
+ sample_frequencies = get_character_frequencies(sample_text)
+
+ xored_string = bytes.fromhex('1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736')
+
+ lowest_frequency_deviation_string = None
+ lowest_frequency_deviation = None
+
+ for i in range(128):
+ key_char = bytes([i]).decode('utf-8')
+ key = bytes([i]) * len(xored_string)
+ try_string = xor_bytes(xored_string, key).decode('utf-8')
+ try_string_frequency_deviation = compare_frequency_deviation(
+ sample_frequencies,
+ get_character_frequencies(try_string),
+ )
+
+ if lowest_frequency_deviation is None or try_string_frequency_deviation < lowest_frequency_deviation:
+ lowest_frequency_deviation_string = try_string
+ lowest_frequency_deviation = try_string_frequency_deviation
+
+
+ expected = "Cooking MC's like a pound of bacon"
+ actual = lowest_frequency_deviation_string