Encryption Key Generator -
/// Generate a 256-bit AES key. pub fn aes_key_256() -> [u8; 32] let mut key = [0u8; 32]; OsRng.fill_bytes(&mut key); key
@staticmethod def aes_key(bits: int = 256) -> bytes: """Generate a key suitable for AES (128, 192, or 256 bits).""" if bits not in (128, 192, 256): raise ValueError("AES key size must be 128, 192, or 256 bits") return os.urandom(bits // 8) encryption key generator
@staticmethod def to_base64(key: bytes) -> str: """Base64 representation for storage/transmission.""" import base64 return base64.b64encode(key).decode('ascii') use rand::RngCore; use rand::rngs::OsRng; /// Generate a cryptographically secure random key of size_bytes . pub fn generate_key(size_bytes: usize) -> Vec<u8> let mut key = vec![0u8; size_bytes]; OsRng.fill_bytes(&mut key); key /// Generate a 256-bit AES key
@staticmethod def to_hex(key: bytes) -> str: """Human‑readable hex representation.""" return key.hex() For new systems, prefer Argon2id via `argon2-cffi`
@staticmethod def random_bytes(length: int) -> bytes: """Return `length` cryptographically random bytes.""" return os.urandom(length)
@staticmethod def from_password(password: str, salt: bytes, length: int = 32) -> bytes: """ Derive a key from a password using PBKDF2-HMAC-SHA256. For new systems, prefer Argon2id via `argon2-cffi`. """ kdf = PBKDF2HMAC( algorithm=hashes.SHA256(), length=length, salt=salt, iterations=600000, # OWASP recommended (2023) ) return kdf.derive(password.encode('utf-8'))