def decode_salt(salt: str) -> bytes: """Decodes the salt string to raw salt. Args: salt (str): The Base64-encoded string of the raw salt. Returns: The raw salt """ TypeCheck.ensure_str(salt, "salt") return base64.b64decode(bytes(salt, "utf-8"))
def make_decrypter(salt: str, key: str) -> Cryptographer: """Makes a Fernet decrypter for salt and key. Args: salt (str): The Base64-encoded string of the raw salt. key (str): The authentication key. Returns: A `Cryptographer` instance, which can be used to decrypt data. """ TypeCheck.ensure_str(salt, "salt") TypeCheck.ensure_str(key, "key") return Cryptographer(Cryptographer.decode_salt(salt), key.encode(), False)
def test_ensure_str(): assert TypeCheck.ensure_str("") is None assert TypeCheck.ensure_str(r"1asm") is None with pytest.raises(TypeError): TypeCheck.ensure_str(8) with pytest.raises(TypeError): TypeCheck.ensure_str(["string"])