Cryptography in Python: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * Python Code Examples =Organizatorium= requirements.txt: <syntaxhighlight lang='text'> cryptography == 3.4.7 </syntaxhighlight> <font color=darkkhaki>NOT TEST...")
 
Line 2: Line 2:
* [[Python Code Examples]]
* [[Python Code Examples]]
=Organizatorium=
=Organizatorium=
==Symmetric Cryptography==
requirements.txt:
requirements.txt:
<syntaxhighlight lang='text'>
<syntaxhighlight lang='text'>
Line 26: Line 27:


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

Revision as of 23:44, 3 March 2022

Internal

Organizatorium

Symmetric Cryptography

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')).decrypt(cypher_data)