-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
33 lines (27 loc) · 938 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
26
27
28
29
30
31
32
33
import operator
from sage.crypto.util import ascii_to_bin
# converts list of chars to string
def to_str(h):
if (len(h) < 1):
return ""
return reduce(operator.add, h)
little_endian = lambda x: int(to_str(list(reversed(split_every(format(x, "064b"), 8)))), 2)
word_to_int = lambda x: int(str(ascii_to_bin(x)), 2)
# split list for lists of lengths of n
def split_every (list, n):
return [list[i:i+n] for i in range(0,len(list), n)]
# in bytes
def pad_word(word, size):
ext_len = size-len(word)%size
if (ext_len == 1):
word += chr(0x86)
return word
word += to_str([chr(0x06)] + [chr(0x0)]*(ext_len-2) + [chr(0x80)]*(ext_len-abs(ext_len-2)-1))
return word
def split_at(word, n):
return [word[:n], word[n:]]
# Bitwise rotation of W with length of w by r bits to left
def rot(W, r, w):
r = r % w
b = split_at(format(W, "0"+str(w)+"b"), r)
return int(b[1] + b[0], base=2)