So my question reduced to: "How can I encrypt data against a password in Python". I gave up on pysodium due to the lack of documentation. I used cryptography and argon2 packages to write my own encryption algorithm (it's not my own crypto algorithm, I know Rule No. 1 in crypto; it's just the procedure to utilize what's already there). So here are my functions:
import cryptography.fernet import argon2 import base64 def encrypt_data(data_bytes, password, salt): password_hash = argon2.argon2_hash(password=password, salt=salt) encoded_hash = base64.urlsafe_b64encode(password_hash[:32]) encryptor = cryptography.fernet.Fernet(encoded_hash) return encryptor.encrypt(data_bytes) def decrypt_data(cipher_bytes, password, salt): password_hash = argon2.argon2_hash(password=password, salt=salt) encoded_hash = base64.urlsafe_b64encode(password_hash[:32]) decryptor = cryptography.fernet.Fernet(encoded_hash) return decryptor.decrypt(cipher_bytes)
And here's an example on how to use them:
cipher = encrypt_data("Hi Dude, Don't tell anyone I said Hi!".encode(), "SecretPassword", "SaltySaltySalt") decrypted = decrypt_data(cipher, "SecretPassword", "SaltySaltySalt") print(cipher) print(decrypted.decode())
Remember that encryption is for bytes only; not for strings. This is why I'm using encode/decode.
Why argon2? Because it's a memory hard algorithm that's very hard to break with GPUs and ASICs (yes, I'm a cryptocurrency fan).
Why Fernet? Because it uses AES CBC, which seems to be secure enough; besides, it's really easy to use (which is exactly what I need... I'm not a cryptographer, so I need a black-box to use).
Disclaimer: Please be aware that I'm not a cryptographer. I'm just a programmer. Please feel free to critique my way of encrypting and decrypting, and please feel free to add your contribution to make this better.
crypto_pwhash... no idea why or how and the lack of documentation is really irritating. I'm doing exactly the opposite of what I wanted to do... I was hoping I could find some one-liner to encrypt my data and a one-liner to decrypt it, but for some reason that doesn't exist. Now I'm tackling the cryptography package of python... maybe that'll do it.