-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper_functions.py
68 lines (57 loc) · 1.83 KB
/
helper_functions.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Helper Functions
"""
import random
import string
def get_byte_list_from(data_string):
"""
Args:
data_string: a string to convert to bytes
length should be a multiple of 4
Return:
A list of lists with bytes corresponding to the data_string
"""
return [list(bytes(data_string[i:i+4], 'utf-8')) for i in range(0,len(data_string), 4)]
def generate_random_key(key_size):
"""
Generates a random key from uppercase characters and digits.
Args:
key_size: an integer; the desired list length
Returns:
A list of bytes corresponding to the randomly generated key.
"""
rand_key = bytes(random.randint(0,255) for _ in range(key_size))
return list(rand_key)
def initialization_vector(block_size=16):
"""
Generate an initialization vector for AES Cipher Block Chaining Mode
See aes_test.py test_cbc_on_file function for example of use.
block_size should be the same size as the key_size.
Args:
block_size: an integer
Returns:
4 by block_size/4 sized matrix of integers.
"""
byte_list = generate_random_key(block_size)
return byte_list
def convert_to_state_matrix(lst):
return [lst[i:i + 4] for i in range(0, len(lst), 4)]
def convert_hex_to_bytes(hex_str):
"""
Take hex string and convert it to list of bytes
Args:
hex_str: hex string
Returns:
list of bytes
"""
hex_str = hex_str[2:] if hex_str[:2] == "0x" else hex_str
return list(bytes([int(hex_str[i:i+2],16) for i in range(0, len(hex_str), 2)]))
def convert_bytes_to_hex(bytes_list):
"""
Take list of bytes and convert to hex string representation
Args:
byte_str: list(byte)
Returns:
hex string representation of the bstr
"""
return ''.join(map("{:02x}".format, bytes_list))