-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhexmessage_3.py
37 lines (27 loc) · 978 Bytes
/
hexmessage_3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import binascii
def hex_from(s):
return binascii.hexlify(bytes(s, encoding='utf-8'))
def str_from(h):
return str(binascii.unhexlify(h), encoding='utf-8')
class HexMessage:
def __init__(self, msg):
if type(msg) == bytes:
# assumed already in hex
self.msg = msg
else:
# assumed string
self.msg = hex_from(msg)
def __str__(self):
return '"{}"'.format(str_from(self.msg))
def __repr__(self):
return repr(self.msg)
def __xor__(self, other):
a = binascii.unhexlify(self.msg)
if type(other) == HexMessage:
b = binascii.unhexlify(other.msg)
elif type(other) == bytes:
b = binascii.unhexlify(other)
else:
# assumed string
b = bytes(other, encoding='utf-8')
return HexMessage(binascii.hexlify(bytes(ai^bi for ai, bi in zip(a,b))))