Update app/utils/crypto.py
Browse files- app/utils/crypto.py +56 -2
app/utils/crypto.py
CHANGED
|
@@ -26,13 +26,64 @@ def derive_session_id(password: str) -> str:
|
|
| 26 |
return hashlib.sha256(f"secforge::{password}".encode()).hexdigest()[:16]
|
| 27 |
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
def encrypt(data: dict, password: str) -> str:
|
| 30 |
"""Encrypt a dict to a base64 string using AES-GCM."""
|
|
|
|
|
|
|
|
|
|
| 31 |
salt = os.urandom(16)
|
| 32 |
key = _derive_key(password, salt)
|
| 33 |
aesgcm = AESGCM(key)
|
| 34 |
nonce = os.urandom(12)
|
| 35 |
-
plaintext = json.dumps(
|
| 36 |
ciphertext = aesgcm.encrypt(nonce, plaintext, None)
|
| 37 |
# Pack: salt(16) + nonce(12) + ciphertext
|
| 38 |
packed = salt + nonce + ciphertext
|
|
@@ -49,6 +100,9 @@ def decrypt(token: str, password: str) -> dict:
|
|
| 49 |
key = _derive_key(password, salt)
|
| 50 |
aesgcm = AESGCM(key)
|
| 51 |
plaintext = aesgcm.decrypt(nonce, ciphertext, None)
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
except Exception:
|
| 54 |
raise ValueError("Wrong password or corrupted data.")
|
|
|
|
| 26 |
return hashlib.sha256(f"secforge::{password}".encode()).hexdigest()[:16]
|
| 27 |
|
| 28 |
|
| 29 |
+
def _serialize_data(data: dict) -> dict:
|
| 30 |
+
"""Convert non-JSON-serializable types (like bytes) to JSON-safe formats."""
|
| 31 |
+
serialized = {}
|
| 32 |
+
for key, value in data.items():
|
| 33 |
+
if key == "file_contents":
|
| 34 |
+
# file_contents is dict of {filename: (content, language, is_image)}
|
| 35 |
+
serialized[key] = {}
|
| 36 |
+
for fname, file_data in value.items():
|
| 37 |
+
if isinstance(file_data, tuple) and len(file_data) >= 2:
|
| 38 |
+
content, language = file_data[0], file_data[1]
|
| 39 |
+
is_image = language == "image"
|
| 40 |
+
|
| 41 |
+
# Convert bytes to base64 for images
|
| 42 |
+
if is_image and isinstance(content, bytes):
|
| 43 |
+
content_safe = base64.b64encode(content).decode("utf-8")
|
| 44 |
+
serialized[key][fname] = [content_safe, language, True] # True = was bytes
|
| 45 |
+
else:
|
| 46 |
+
serialized[key][fname] = [content, language, False]
|
| 47 |
+
else:
|
| 48 |
+
serialized[key][fname] = file_data
|
| 49 |
+
else:
|
| 50 |
+
serialized[key] = value
|
| 51 |
+
return serialized
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def _deserialize_data(data: dict) -> dict:
|
| 55 |
+
"""Restore base64-encoded bytes back to bytes for images."""
|
| 56 |
+
deserialized = {}
|
| 57 |
+
for key, value in data.items():
|
| 58 |
+
if key == "file_contents":
|
| 59 |
+
deserialized[key] = {}
|
| 60 |
+
for fname, file_data in value.items():
|
| 61 |
+
if isinstance(file_data, list) and len(file_data) >= 3:
|
| 62 |
+
content, language, was_bytes = file_data[0], file_data[1], file_data[2]
|
| 63 |
+
|
| 64 |
+
# Restore bytes from base64 for images
|
| 65 |
+
if was_bytes and language == "image" and isinstance(content, str):
|
| 66 |
+
content_restored = base64.b64decode(content.encode("utf-8"))
|
| 67 |
+
deserialized[key][fname] = (content_restored, language)
|
| 68 |
+
else:
|
| 69 |
+
deserialized[key][fname] = (content, language)
|
| 70 |
+
else:
|
| 71 |
+
deserialized[key][fname] = file_data
|
| 72 |
+
else:
|
| 73 |
+
deserialized[key] = value
|
| 74 |
+
return deserialized
|
| 75 |
+
|
| 76 |
+
|
| 77 |
def encrypt(data: dict, password: str) -> str:
|
| 78 |
"""Encrypt a dict to a base64 string using AES-GCM."""
|
| 79 |
+
# Serialize bytes to base64 before JSON
|
| 80 |
+
data_safe = _serialize_data(data)
|
| 81 |
+
|
| 82 |
salt = os.urandom(16)
|
| 83 |
key = _derive_key(password, salt)
|
| 84 |
aesgcm = AESGCM(key)
|
| 85 |
nonce = os.urandom(12)
|
| 86 |
+
plaintext = json.dumps(data_safe).encode("utf-8")
|
| 87 |
ciphertext = aesgcm.encrypt(nonce, plaintext, None)
|
| 88 |
# Pack: salt(16) + nonce(12) + ciphertext
|
| 89 |
packed = salt + nonce + ciphertext
|
|
|
|
| 100 |
key = _derive_key(password, salt)
|
| 101 |
aesgcm = AESGCM(key)
|
| 102 |
plaintext = aesgcm.decrypt(nonce, ciphertext, None)
|
| 103 |
+
data = json.loads(plaintext.decode("utf-8"))
|
| 104 |
+
# Restore bytes from base64
|
| 105 |
+
data = _deserialize_data(data)
|
| 106 |
+
return data
|
| 107 |
except Exception:
|
| 108 |
raise ValueError("Wrong password or corrupted data.")
|