-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.py
More file actions
39 lines (32 loc) · 1.12 KB
/
crypto.py
File metadata and controls
39 lines (32 loc) · 1.12 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
import os
import json # for username feature
from cryptography.hazmat.primitives.ciphers.aead import ChaCha20Poly1305
from hashlib import sha256
# Packet structure:
def send_packet(sock, p_type, user, content, key):
"""Wraps data in JSON, encrypts, and sends"""
packet = {
"type": p_type,
"user": user,
"content": content
}
encrypted_data = encrypt(key, json.dumps(packet).encode())
sock.send(encrypted_data)
def receive_packet(sock, key):
"""Receives, decrypts, and converts JSON back to dict"""
data = sock.recv(4096)
if not data:
return None
decrypted_msg = decrypt(key, data).decode()
return json.loads(decrypted_msg)
def derive_key(shared_password: str) -> bytes:
return sha256(shared_password.encode()).digest()
def encrypt(key: bytes, message: bytes) -> bytes:
nonce = os.urandom(12)
cipher = ChaCha20Poly1305(key)
return nonce + cipher.encrypt(nonce, message, None)
def decrypt(key: bytes, data: bytes) -> bytes:
nonce = data[:12]
ciphertext = data[12:]
cipher = ChaCha20Poly1305(key)
return cipher.decrypt(nonce, ciphertext, None)