forked from yuhangjiang22/ZeroShotRE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
159 lines (126 loc) · 4.44 KB
/
Copy pathutils.py
File metadata and controls
159 lines (126 loc) · 4.44 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# %% libraries
import os
from collections import Counter
import importlib
import re
import pickle
import json
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives.asymmetric import padding as rsa_padding
# %% body
def recursive_lowercase(data):
if isinstance(data, str):
return data.lower()
elif isinstance(data, list):
return [recursive_lowercase(el) for el in data]
elif isinstance(data, set):
return {recursive_lowercase(el) for el in data}
elif isinstance(data, dict):
return {key: recursive_lowercase(value) for key, value in data.items()}
else:
return data
def recursive_convert(data, converter):
if isinstance(data, str):
return converter[data]
elif isinstance(data, list):
return [recursive_convert(el) for el in data]
elif isinstance(data, set):
return {recursive_convert(el) for el in data}
elif isinstance(data, dict):
return {key: recursive_convert(value) for key, value in data.items()}
else:
return data
def unique_dicts(dict_list):
unique = []
for el in dict_list:
if el not in dict_list:
unique.append(el)
return unique
def save_json(data, filename):
with open(filename, "w") as outfile:
json.dump(data, outfile)
def uniquify(path):
filename, extension = os.path.splitext(path)
counter = 1
while os.path.exists(path):
path = filename + " (" + str(counter) + ")" + extension
counter += 1
return path
def unlist(nested_list):
unlisted = [subel for el in nested_list for subel in el]
return unlisted
def pickle_save(data, filename):
with open(filename, "wb") as f:
pickle.dump(data, f)
def pickle_load(filename):
with open(filename, "rb") as f:
data = pickle.load(f)
return data
def make_dir(dir_path):
if not os.path.exists(dir_path):
os.makedirs(dir_path)
print(f"Directory created at: {dir_path}")
else:
print(f"Directory already exists at: {dir_path}")
def pickle_save_encrypted(data, filename, public_key_path):
# Serialize dataset (can be large)
serialized_data = pickle.dumps(data)
# Generate random AES key and IV
aes_key = os.urandom(32) # AES-256
iv = os.urandom(16)
# Encrypt the serialized data with AES
cipher = Cipher(algorithms.AES(aes_key), modes.CBC(iv))
encryptor = cipher.encryptor()
# PKCS7 padding
padding_len = 16 - (len(serialized_data) % 16)
padded_data = serialized_data + bytes([padding_len] * padding_len)
encrypted_data = encryptor.update(padded_data) + encryptor.finalize()
# Load RSA public key
with open(public_key_path, "rb") as key_file:
public_key = serialization.load_pem_public_key(key_file.read())
# Encrypt AES key with RSA
encrypted_aes_key = public_key.encrypt(
aes_key,
rsa_padding.OAEP(
mgf=rsa_padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
# Save to file: [2-byte key length][encrypted AES key][IV][encrypted data]
with open(filename, "wb") as f:
f.write(len(encrypted_aes_key).to_bytes(2, "big"))
f.write(encrypted_aes_key)
f.write(iv)
f.write(encrypted_data)
def pickle_load_encrypted(filename, private_key_path, password=None):
# Load private RSA key
with open(private_key_path, "rb") as key_file:
private_key = serialization.load_pem_private_key(
key_file.read(),
password=password.encode() if password else None,
)
with open(filename, "rb") as f:
key_len = int.from_bytes(f.read(2), "big")
encrypted_key = f.read(key_len)
iv = f.read(16)
encrypted_data = f.read()
# Decrypt AES key
aes_key = private_key.decrypt(
encrypted_key,
rsa_padding.OAEP(
mgf=rsa_padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
# Decrypt data
cipher = Cipher(algorithms.AES(aes_key), modes.CBC(iv))
decryptor = cipher.decryptor()
padded_data = decryptor.update(encrypted_data) + decryptor.finalize()
# Remove PKCS7 padding
padding_len = padded_data[-1]
data = padded_data[:-padding_len]
return pickle.loads(data)