Generating hash values with Python
Coding with Python
🕑 This lesson will take about 10 minutes
Hashing is a process of converting an input (eg. plaintext data such as a password) into a fixed-size string of characters, known as a hash value. This process uses a mathematical function known as a hash function, which takes in the input data and generates a hash value.
The hash value is always a fixed length (which means it can be larger or smaller than the original input)
Hash functions when given the same input, should always produces the same output
Hashing is in irreversible process, making it extremely difficult to determine the original input from the hash value, making it suitable for protecting user data such as passwords
In cryptographic applications, the one-way nature of hashing is important for security and the reason why passwords are often stored as hashes in databases, so even if someone obtains the hash, they cannot easily recover the original password.
Hash values can also be thought of as fingerprints for files. The contents of a file (such as an image, video, text or audio file) can be processed through a hashing function, and a unique value (the hash value) is produced that identifies the contents of the file. If the contents are modified in any way, then the value of the hash will also change.
Common algorithms used to produce hash values include MD5, SHA1, SHA256, and more. Several hashing algorithms are supported by Python using the hashlib library. The supported algorithms are:
sha256
sha384
sha224
sha512
sha1
md5
Let’s check out some examples.
Generating MD5 hash values
MD5 is one type of hashing algorithm that has been replaced by more secure hashing algorithms such as SHA-256. The MD5 algorithm is vulnerable to collisions (generating the same hash for two different plaintext values) and brute-force attacks. SHA256 is an example of a newer hashing algorithm that generates longer hashes and is resistant to collisions and brute-force attacks, unlike MD5 and SHA-1 which have been exploited by hackers and researchers. However, MD5 can still be used for generating hashes where security is not important.
Generating SHA-1 hash values
SHA-1 another type of hashing algorithm that can be used. It also has been replaced by more secure hashing algorithms such as SHA-256 (which generates longer hashes and is resistant to collisions and brute-force attacks, unlike MD5 and SHA-1 which have been exploited by hackers and researchers). However, SHA-1 can still be used for generating hashes where security is not important.
Generating SHA-256 hash values
SHA-256 is a stronger hashing algorithm that generates longer hashes than MD5 and SHA-1, and is resistant to collisions and brute-force attacks, unlike MD5 and SHA-1 which have been exploited by hackers and researchers). SHA-256 and other similar hashing algorithms can be used to hash passwords before storing them in a database.