Spaces:
Running
Running
| # modules/crypto_engine.py | |
| import hashlib | |
| import secrets | |
| from typing import Dict, Any | |
| class CryptoEngine: | |
| def __init__(self): | |
| print("CryptoEngine geladen") | |
| self.algorithm = "sha256" | |
| def hash_text(self, text: str) -> str: | |
| return hashlib.sha256( | |
| text.encode("utf-8") | |
| ).hexdigest() | |
| def generate_token( | |
| self, | |
| length: int = 32 | |
| ) -> str: | |
| return secrets.token_hex(length) | |
| def verify_hash( | |
| self, | |
| text: str, | |
| hashed: str | |
| ) -> bool: | |
| new_hash = self.hash_text(text) | |
| return new_hash == hashed | |
| def secure_payload( | |
| self, | |
| payload: Dict[str, Any] | |
| ) -> Dict[str, Any]: | |
| payload_string = str(payload) | |
| payload_hash = self.hash_text(payload_string) | |
| return { | |
| "payload": payload, | |
| "hash": payload_hash, | |
| "algorithm": self.algorithm | |
| } | |
| def create_session_key(self) -> str: | |
| return secrets.token_urlsafe(64) |