Python Modules of Cryptography



In this chapter, you will learn in detail about various modules of cryptography in Python.

Cryptography Module

It includes all the recipes and primitives, and provides a high level interface of coding in Python. You can install cryptography module using the following command −

pip3 install cryptography
Collecting cryptography
  Downloading cryptography-46.0.3-cp311-abi3-win_amd64.whl.metadata (5.7 kB)
Collecting cffi>=2.0.0 (from cryptography)
  Downloading cffi-2.0.0-cp314-cp314-win_amd64.whl.metadata (2.6 kB)
Collecting pycparser (from cffi>=2.0.0->cryptography)
  Downloading pycparser-2.23-py3-none-any.whl.metadata (993 bytes)
Downloading cryptography-46.0.3-cp311-abi3-win_amd64.whl (3.5 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.5/3.5 MB 6.6 MB/s  0:00:00
Downloading cffi-2.0.0-cp314-cp314-win_amd64.whl (185 kB)
Downloading pycparser-2.23-py3-none-any.whl (118 kB)
Installing collected packages: pycparser, cffi, cryptography
Successfully installed cffi-2.0.0 cryptography-46.0.3 pycparser-2.23

Example - Usage of Cryptography Module

You can use the following code to implement the cryptography module −

main.py

from cryptography.fernet import Fernet

key = Fernet.generate_key()
cipher_suite = Fernet(key)
text = "This example is used to demonstrate cryptography module";

cipher_text = cipher_suite.encrypt(text.encode())

plain_text = cipher_suite.decrypt(cipher_text)

print(plain_text.decode())

Output

The code given above produces the following output −

This example is used to demonstrate cryptography module

Example - Verifying a Password

The code given here is used to verify the password and creating its hash. It also includes logic for verifying the password for authentication purpose.

main.py

import uuid
import hashlib

def hash_password(password):
   # uuid is used to generate a random number of the specified password
   salt = uuid.uuid4().hex
   return hashlib.sha256(salt.encode() + password.encode()).hexdigest() + ':' + salt

def check_password(hashed_password, user_password):
   password, salt = hashed_password.split(':')
   return password == hashlib.sha256(salt.encode() + user_password.encode()).hexdigest()

new_pass = input('Please enter a password: ')
hashed_password = hash_password(new_pass)
print('The string to store in the db is: ' + hashed_password)
old_pass = input('Now please enter the password again to check: ')

if check_password(hashed_password, old_pass):
   print('You entered the right password')
else:
   print('Passwords do not match')

Output

Scenario 1 − If you have entered a correct password, you can find the following output −

Please enter a password: abcd
The string to store in the db is: e3b340e06393a071a1685251fe59523b21b77d5d1bf1092bb270a1875b09250c:0f2f13c9312f42fc92a68350d0139ee4
Now please enter the password again to check: abcd
You entered the right password

Scenario 2 − If we enter wrong password, you can find the following output −

Please enter a password: abcd
The string to store in the db is: e375a2bd46f0cf0c63a96fc0a5e7cd8884e0ebbccff6d7196929538a8019b909:f500e1821203492ca88c53256ee947e6
Now please enter the password again to check: abc
Passwords do not match

Explanation

Hashlib package is used for storing passwords in a database. In this program, salt is used which adds a random sequence to the password string before implementing the hash function.

Advertisements