Spaces:
Sleeping
Sleeping
Update modules/crypto_engine.py
Browse files- modules/crypto_engine.py +55 -2
modules/crypto_engine.py
CHANGED
|
@@ -1,3 +1,56 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
def __init__(self):
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# modules/crypto_engine.py
|
| 2 |
+
|
| 3 |
+
import hashlib
|
| 4 |
+
import secrets
|
| 5 |
+
from typing import Dict, Any
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class CryptoEngine:
|
| 9 |
+
|
| 10 |
def __init__(self):
|
| 11 |
+
|
| 12 |
+
print("CryptoEngine geladen")
|
| 13 |
+
|
| 14 |
+
self.algorithm = "sha256"
|
| 15 |
+
|
| 16 |
+
def hash_text(self, text: str) -> str:
|
| 17 |
+
|
| 18 |
+
return hashlib.sha256(
|
| 19 |
+
text.encode("utf-8")
|
| 20 |
+
).hexdigest()
|
| 21 |
+
|
| 22 |
+
def generate_token(
|
| 23 |
+
self,
|
| 24 |
+
length: int = 32
|
| 25 |
+
) -> str:
|
| 26 |
+
|
| 27 |
+
return secrets.token_hex(length)
|
| 28 |
+
|
| 29 |
+
def verify_hash(
|
| 30 |
+
self,
|
| 31 |
+
text: str,
|
| 32 |
+
hashed: str
|
| 33 |
+
) -> bool:
|
| 34 |
+
|
| 35 |
+
new_hash = self.hash_text(text)
|
| 36 |
+
|
| 37 |
+
return new_hash == hashed
|
| 38 |
+
|
| 39 |
+
def secure_payload(
|
| 40 |
+
self,
|
| 41 |
+
payload: Dict[str, Any]
|
| 42 |
+
) -> Dict[str, Any]:
|
| 43 |
+
|
| 44 |
+
payload_string = str(payload)
|
| 45 |
+
|
| 46 |
+
payload_hash = self.hash_text(payload_string)
|
| 47 |
+
|
| 48 |
+
return {
|
| 49 |
+
"payload": payload,
|
| 50 |
+
"hash": payload_hash,
|
| 51 |
+
"algorithm": self.algorithm
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
def create_session_key(self) -> str:
|
| 55 |
+
|
| 56 |
+
return secrets.token_urlsafe(64)
|