Commit my random junk
[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 def encrypt_with_repeating_xor(plaintext, key):
36     plaintext_bytes = plaintext.encode('utf-8')
37     key_bytes = key.encode('utf-8')
38
39     return xor_bytes(
40         plaintext_bytes,
41         (key_bytes * ((len(plaintext_bytes) // len(key_bytes)) + 1))[:len(plaintext_bytes)],
42     )
43
44 def hamming_weight(_bytes):
45     def hamming_weight_of_byte(b):
46         count = 0
47         while b > 0:
48             count += 1
49             b &= b - 1
50         return count
51
52     return sum(hamming_weight_of_byte(b) for b in _bytes)
53
54 def hamming_distance(bytes0, bytes1):
55     return hamming_weight(xor_bytes(bytes0, bytes1))
56
57 class Set1Challenge1Tests(unittest.TestCase):
58     def test_converts_hex_to_base64(self):
59         expected = 'SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t\n'
60         actual = base64_from_hex('49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d')
61         self.assertEqual(expected, actual)
62
63 class Set1Challenge2Tests(unittest.TestCase):
64     def test_xors_hex_strings(self):
65         hex0 = '1c0111001f010100061a024b53535009181c'
66         hex1 = '686974207468652062756c6c277320657965'
67
68         expected = '746865206b696420646f6e277420706c6179'
69         actual = xor_hex(hex0, hex1)
70
71         self.assertEqual(expected, actual)
72
73 class Set1Challenge3Tests(unittest.TestCase):
74     def test_gets_message(self):
75         xored_string = bytes.fromhex('1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736')
76
77         lowest_frequency_deviation_string = None
78         lowest_frequency_deviation = None
79
80         for i in range(128):
81             key_char = bytes([i]).decode('utf-8')
82             key = bytes([i]) * len(xored_string)
83             try_string = xor_bytes(xored_string, key).decode('utf-8')
84             try_string_frequency_deviation = compare_frequency_deviation(
85                 SAMPLE_FREQUENCIES,
86                 get_character_frequencies(try_string),
87             )
88
89             if lowest_frequency_deviation is None or try_string_frequency_deviation < lowest_frequency_deviation:
90                 lowest_frequency_deviation_string = try_string
91                 lowest_frequency_deviation = try_string_frequency_deviation
92
93
94         expected = "Cooking MC's like a pound of bacon"
95         actual = lowest_frequency_deviation_string
96
97         self.assertEqual(expected, actual)
98
99 class Set1Challenge4Tests(unittest.TestCase):
100     def test_gets_message(self):
101         with open('set1challenge4.txt','r') as f:
102             lines = f.readlines()
103
104
105         lowest_frequency_deviation_string = None
106         lowest_frequency_deviation = None
107
108         for line in lines:
109             line_bytes = bytes.fromhex(line)
110
111             for i in range(128):
112                 key_char = bytes([i]).decode('utf-8')
113                 key = bytes([i]) * len(line_bytes)
114
115                 try:
116                     try_string = xor_bytes(line_bytes, key).decode('utf-8')
117                     try_string_frequency_deviation = compare_frequency_deviation(
118                         SAMPLE_FREQUENCIES,
119                         get_character_frequencies(try_string),
120                     )
121
122                     if lowest_frequency_deviation is None or try_string_frequency_deviation < lowest_frequency_deviation:
123                         lowest_frequency_deviation_string = try_string
124                         lowest_frequency_deviation = try_string_frequency_deviation
125                 except:
126                     pass
127
128         expected = 'Now that the party is jumping\n'
129         actual = lowest_frequency_deviation_string
130
131         self.assertEqual(expected, actual)
132
133 class Set1Challenge5Tests(unittest.TestCase):
134     def test_encrypts_with_repeating_xor(self):
135         plaintext = "Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal"
136         key = 'ICE'
137
138         expected = '0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f'
139         actual = encrypt_with_repeating_xor(plaintext, key).hex()
140
141         self.assertEqual(expected, actual)
142
143 with open('set1challenge6.txt','r') as f:
144     set1challenge6text = f.read()
145
146 class Set1Challenge6Tests(unittest.TestCase):
147     def test_hamming_distance(self):
148         expected = 37
149         actual = hamming_distance(b'this is a test', b'wokka wokka!!!')
150         self.assertEqual(expected, actual)
151
152     def test_find_repeated_xor_keysize(self):
153         expected = 0
154         actual = find_repeated_xor_keysize(set1challenge6text)
155         self.assertEqual(expected, actual)
156
157 if __name__ == '__main__':
158     unittest.main()