summaryrefslogtreecommitdiff
path: root/binary
diff options
context:
space:
mode:
Diffstat (limited to 'binary')
-rw-r--r--binary/data_handling/recv_data.py26
-rw-r--r--binary/data_handling/send_data.py13
-rw-r--r--binary/encrypt_data.py26
3 files changed, 65 insertions, 0 deletions
diff --git a/binary/data_handling/recv_data.py b/binary/data_handling/recv_data.py
new file mode 100644
index 0000000..a329532
--- /dev/null
+++ b/binary/data_handling/recv_data.py
@@ -0,0 +1,26 @@
+import pickle
+import zlib
+
+
+def recv_data(conn, settings, callback=None):
+ encryption, headersize = settings
+ mode = [True, 0, b'']
+
+ while True:
+ client_msg = conn.recv(81920)
+
+ if mode[0]:
+ mode[1] = int(client_msg[:headersize])
+ mode[0] = False
+
+ mode[2] += client_msg
+
+ if len(mode[2])-headersize == mode[1]:
+ decrypted_msg = encryption.do_decrypt(mode[2][headersize:])
+ decompressed_msg = zlib.decompress(decrypted_msg)
+ client_msg = pickle.loads(decompressed_msg)
+
+ if callback:
+ callback(conn, client_msg)
+
+ return client_msg \ No newline at end of file
diff --git a/binary/data_handling/send_data.py b/binary/data_handling/send_data.py
new file mode 100644
index 0000000..4622fbb
--- /dev/null
+++ b/binary/data_handling/send_data.py
@@ -0,0 +1,13 @@
+import pickle
+import zlib
+
+
+def send_data(conn, data, settings, extra_data={}):
+ encryption, encoding, headersize = settings
+ data.update(extra_data)
+
+ pickled_msg = pickle.dumps(data)
+ compressed_msg = zlib.compress(pickled_msg, 5)
+ encrypted_msg = encryption.do_encrypt(compressed_msg)
+ final_msg = bytes(f'{len(encrypted_msg):<{headersize}}', encoding) + encrypted_msg
+ conn.send(final_msg) \ No newline at end of file
diff --git a/binary/encrypt_data.py b/binary/encrypt_data.py
new file mode 100644
index 0000000..6f88280
--- /dev/null
+++ b/binary/encrypt_data.py
@@ -0,0 +1,26 @@
+import base64
+
+from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
+from cryptography.hazmat.backends import default_backend
+from cryptography.hazmat.primitives import hashes
+from cryptography.fernet import Fernet
+
+
+class Encryption:
+ def __init__(self, password='ksxgyRuBRJLKxjFeHD4nmxbE', salt=b'v4CuHZFzmTedBY2EBGrLRXsm'):
+ self.password = password
+ self.salt = salt
+ self.key = Fernet(self.generate_key())
+
+
+ def generate_key(self):
+ kdf = PBKDF2HMAC(algorithm=hashes.SHA256(), length=32, salt=self.salt, iterations=100000, backend=default_backend())
+ return base64.urlsafe_b64encode(kdf.derive(self.password.encode()))
+
+
+ def do_encrypt(self, message):
+ return self.key.encrypt(message)
+
+
+ def do_decrypt(self, ciphertext):
+ return self.key.decrypt(ciphertext) \ No newline at end of file