4 def base64_from_hex(_hex):
5 return codecs.encode(bytes.fromhex(_hex), 'base64').decode('utf-8')
7 def hex_xor(hex0, hex1):
8 assert len(hex0) == len(hex1)
9 bytes0 = bytes.fromhex(hex0)
10 bytes1 = bytes.fromhex(hex1)
11 xored_bytes = bytes(byte0 ^ byte1 for byte0, byte1 in zip(bytes0, bytes1))
12 return codecs.encode(xored_bytes, 'hex').decode('utf-8')
14 class Set1Challenge1Tests(unittest.TestCase):
15 def test_converts_hex_to_base64(self):
16 expected = 'SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t\n'
17 actual = base64_from_hex('49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d')
18 self.assertEqual(expected, actual)
20 class Set1Challenge2Tests(unittest.TestCase):
21 def test_xors_hex_strings(self):
22 hex0 = '1c0111001f010100061a024b53535009181c'
23 hex1 = '686974207468652062756c6c277320657965'
25 expected = '746865206b696420646f6e277420706c6179'
26 actual = hex_xor(hex0, hex1)
28 self.assertEqual(expected, actual)
30 if __name__ == '__main__':