Basic Cryptography With Python
Cryptographic services(hashing and key derivation)
In this present day, the digital space has become very intertwined with our lives and it has become increasingly more important, it is no longer the worry of only spies and governments to secure confidential information but every individual with a legitimate online identity has to make sure their own information and online communication is secure against adversaries, gone are the days when all it needed was a simple transposition or substitution of letters to secure a message.
Data is increasingly more valuable as the whole internet is driven by it, therefore the need to protect data has led the evolution of cryptography techniques.
Previously cryptography techniques were not as widespread as it is in this digital age, different algorithms have surfaced in the struggle to protect privacy and we would explore some of those algorithms using python.
Hashing
Hashing is a cryptographic service that uses a math function to map data into a fixed length value that will represent the data, the hash value is reproducible but cannot be reversed. It is used to verify digital integrity and a quick and secure way to search and retrieve data.
Hashing can be done in a number of ways, python has a library named hashlib that has a slew of different algorithms and function that can be used to do quick hashes ranging from md5() to sha512(), these algorithms can be applied differently some more secure than others.
Get Joseph Ita’s stories in your inbox
Join Medium for free to get updates from this writer.
Use a python interpreter to run the code:
import hashlib
m = hashlib.sha256()
m.update(b"Anything you want to hash")
m.hexdigest()Output:
‘efe1f9a9f6f3af088198c74ab8ee1e3f74527fbae08e515b8bba7ce29d663fea’The ‘b’ prefixed before the string indicates that the literal should become a bytes literal.
The output is a hex value and cannot be reversed, but the same text will always return the same hash value. This is especially important for passwords, they are stored as hashes and every time you log in the password inputted is hashed and compared with what is in the database other than password.
Key Derivation
There are more interesting ways to implement or generate secure hashes and this can be achieved by using hash functions. One of these ways is key derivation function, it derives secret keys from a main key, a password, and a passphrase(salt) using a pseudorandom function. scrypt is a strong password-based key derivation function, it is memory intensive therefore highly efficient password cracking hardware will have a hard time cracking it.
Scrypt algorithm:
key = Scrypt(password, salt, N, r, p, derived-key-len)Parameters:
N = iterations count (affects memory and CPU usage), e.g. 16384 or 2048
r = block size (affects memory and CPU usage), e.g. 8
p = parallelism factor (threads to run in parallel - affects the memory, CPU usage), usually 1
password = the input password (8-10 chars minimal length is recommended)
salt – securely-generated random bytes (64 bits minimum, 128 bits recommended)
derived-key-length - how many bytes to generate as output, e.g. 32 bytes (256 bits)
These days a lot of hashing algorithms use scrypt as it is quite strong, but in the coming years as computing power is upgraded, every other technology has to follow suit and the piece of code it relies on.
Thanks for reaching here, let’s hash things out. 🙂





