-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
53 lines (39 loc) · 1.03 KB
/
utilities.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
50
51
52
def create_block(text, start):
matrix = []
index = start
for i in range(4):
vector = []
for j in range(4):
vector.append((text[index]))
index += 1
matrix.append(vector)
return matrix
def block_to_text(block):
text = ""
for i in range(4):
text += "".join(map(chr, block[i]))
return text
def blocks_list_to_text(blocks):
text = ""
for i in range(len(blocks)):
text += block_to_text(blocks[i])
return text
def add_padding(text):
text_len = len(text)
if text_len % 16 != 0:
text.append(128)
padding_number = 16 - (text_len % 16)
for i in range(1, padding_number):
text.append(0)
# add dummy padding
else:
text.append(128)
for i in range(15):
text.append(0)
return text
def remove_padding(text):
padding_start = 0
for i in range(len(text)-1, 0, -1):
if text[i] == chr(128):
padding_start = i
return text[0: padding_start]