Download shield_agents/utils/crypto.py from Slfgod/shield-agents: direct link, hf CLI and curl.
- Browser
- Download file 1.24 kB
-
https://huggingface.co/Slfgod/shield-agents/resolve/main/shield_agents/utils/crypto.py
- Command line
-
hf download hf://Slfgod/shield-agents/shield_agents/utils/crypto.py
-
curl -L -o crypto.py https://huggingface.co/Slfgod/shield-agents/resolve/main/shield_agents/utils/crypto.py
1.24 kB
| """Cryptographic utility functions for Shield Agents.""" | |
| import hashlib | |
| import os | |
| from typing import Optional | |
| def hash_file(file_path: str, algorithm: str = "sha256") -> Optional[str]: | |
| """Compute hash of a file for caching/incremental scanning. | |
| Args: | |
| file_path: Path to the file. | |
| algorithm: Hash algorithm (sha256, md5, sha1). | |
| Returns: | |
| Hex digest string, or None if file cannot be read. | |
| """ | |
| try: | |
| hasher = hashlib.new(algorithm) | |
| with open(file_path, "rb") as f: | |
| for chunk in iter(lambda: f.read(8192), b""): | |
| hasher.update(chunk) | |
| return hasher.hexdigest() | |
| except (OSError, IOError, ValueError): | |
| return None | |
| def hash_content(content: str, algorithm: str = "sha256") -> str: | |
| """Compute hash of string content. | |
| Args: | |
| content: String content to hash. | |
| algorithm: Hash algorithm. | |
| Returns: | |
| Hex digest string. | |
| """ | |
| hasher = hashlib.new(algorithm) | |
| hasher.update(content.encode("utf-8")) | |
| return hasher.hexdigest() | |
| def generate_id() -> str: | |
| """Generate a unique identifier. | |
| Returns: | |
| A random hex string. | |
| """ | |
| return os.urandom(16).hex() | |