forked from Psychotropos/sercomm_fwutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
25 lines (21 loc) · 770 Bytes
/
utils.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
def nullpad_str(s, length):
return s + ('\x00' * (length - len(s)))
def unnullpad_str(s):
if '\x00' not in s:
return s
return s.split('\x00')[0]
def pkcs7_pad(s):
pad_length = 16 - (len(s) % 16)
return s + (chr(pad_length) * pad_length)
def sercomm_hexdigest(s):
# Replicates a really odd behaviour in the Sercomm libfwutil implementation of hex-digest to hex-string
# Hexadecimal digits starting prefixed with a '0' have their leading zero removed and are followed by a trailing null byte.
hex_s = ''
for c in s:
c_hex = c.encode('hex')
if c_hex.startswith('0'):
hex_s += c_hex[1:]
hex_s += '\x00'
else:
hex_s += c_hex
return hex_s