Secure¶
-
format_key(hsh)[source]¶ Reformat a hash (can be str or bytes)
- Parameters
hsh (
Union[str,bytes]) – hash to be reformatted- Return type
bytes- Returns
a key which can be used for decrypting the data
-
secure_key(word, salt=None)[source]¶ Get a hash from the raw text password
- Parameters
word (
str) – raw text passwordsalt (
Optional[str]) – random salt to seed the hash computation
- Return type
str- Returns
hash of the password
-
prompt_password_hash(salt=None)[source]¶ Get the hash of a password which is entered by the user in a prompt
- Parameters
salt (
Optional[str]) – random salt for hash computation- Return type
str- Returns
hash of the entered password
-
encrypt(data, hsh=None)[source]¶ Encrypt the data from a provided hash (or prompt for a password if no hash is provided)
- Parameters
data (
bytes) – bytes to be encryptedhsh (
Union[str,bytes,None]) – hash used as key to encrypt
- Return type
bytes- Returns
encrypted data bytes
-
decrypt(data, hsh=None)[source]¶ Decrypt data with provided hash (or prompt for a password to compute hash if no hash is provided)
- Parameters
data (
bytes) – encrypted bytes that should be decryptedhsh (
Union[str,bytes,None]) – hash of a password to be used as a key to decrypt (obviously, must be the same as was used to encrypt)
- Return type
bytes- Returns
the decrypted data, or a WrongKeyError if the key failed
-
secure_pack(obj, hsh=None, meta=None, include_timestamp=False)[source]¶ Pack the object and encrypt it using the provided hash (prompt user for password if none is provided)
- Parameters
obj (
NewType()(SERIALIZABLE,object)) – object to be packedhsh (
Union[str,bytes,None]) – hash used as key to encryptmeta (
Optional[Dict[str,NewType()(JSONABLE,object)]]) – meta information to store with the packed objinclude_timestamp (
bool) – include timestamp in meta info
- Return type
bytes- Returns
encrypted bytes
-
secure_unpack(data, hsh=None, return_meta=False)[source]¶ Decrypt data and unpack to recover the original object using the provided hash as a key
- Parameters
data (
bytes) – encrypted byteshsh (
Union[str,bytes,None]) – hash to be used as a key to decrypt (prompt user, if not provided)return_meta (
bool) – include meta info in output
- Return type
NewType()(SERIALIZABLE,object)- Returns
decrypted and unpacked object, possibly including the meta info