-
Notifications
You must be signed in to change notification settings - Fork 1
/
Common.py
49 lines (38 loc) · 1.01 KB
/
Common.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
38
39
40
41
42
43
44
45
46
47
48
49
import binascii
from Crypto.Hash import *
# TLS 1.2
TLS_VERSION = b'\x03\x03'
sha256WithRSAEncryption = b'\x2A\x86\x48\x86\xF7\x0D\x01\x01\x0B'
sha384WithRSAEncryption = b'\x2A\x86\x48\x86\xF7\x0D\x01\x01\x0C'
sha1WithRSAEncryption = b'\x2A\x86\x48\x86\xF7\x0D\x01\x01\x05'
def bytes_to_int(s):
# In Python 3 one might receive an int
if type(s) == int:
return s
return int(binascii.hexlify(s), 16)
def bytes_to_hex(s):
return bytes_to_str(binascii.hexlify(s))
def nb_to_bytes(n):
h = '%x' % n
s = binascii.unhexlify('0'*(len(h) % 2) + h)
return s
def nb_to_n_bytes(number, size):
h = '%x' % number
s = binascii.unhexlify('0'*(size*2 - len(h)) + h)
return s
def hex_to_bytes(s):
return binascii.unhexlify(s)
def str_to_bytes(s):
try:
# Python 3
return bytes(s, 'utf-8')
except:
# Python 2
return s
def bytes_to_str(b):
try:
# Python 3
return str(b, 'utf-8')
except:
# Python 2
return b