Cryptography in Python

From NovaOrdis Knowledge Base
Revision as of 23:44, 3 March 2022 by Ovidiu (talk | contribs) (Created page with "=Internal= * Python Code Examples =Organizatorium= requirements.txt: <syntaxhighlight lang='text'> cryptography == 3.4.7 </syntaxhighlight> <font color=darkkhaki>NOT TEST...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Internal

Organizatorium

requirements.txt:

cryptography == 3.4.7

NOT TESTED:

from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC

def init_fernet(password):
    key = PBKDF2HMAC(algorithm=hashes.SHA256(), length=32, salt=b'some-salt',
                     iterations=1000, backend=default_backend()).derive(password)
    return Fernet(base64.urlsafe_b64encode(key))

# encrypt
password = ...
clear_data = ...
cypher_data = init_fernet(password.encode('utf-8')).encrypt(clear_data)

# decrypt
clear_data_2 = init_fernet(password.encode('utf-8')).encrypt(cypher_data)