The hashlib
module in Python provides a common interface to many secure hash and message digest algorithms, such as SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, and MD5. These algorithms are used to create cryptographic hashes, which are fixed-size strings or numbers that uniquely represent data.
Table of Contents
- Introduction
- Key Functions and Methods
hashlib.new
hashlib.md5
hashlib.sha1
hashlib.sha224
hashlib.sha256
hashlib.sha384
hashlib.sha512
hashlib.blake2b
hashlib.blake2s
- Examples
- Creating Hashes
- Updating Hashes
- Hashing Files
- Real-World Use Case
- Conclusion
- References
Introduction
The hashlib
module provides a secure way to create cryptographic hashes. These hashes can be used to verify the integrity of data, ensure the authenticity of messages, and for other security-related purposes. The module supports a variety of hashing algorithms, allowing you to choose the one that best fits your needs.
Key Functions and Methods
hashlib.new
Creates a new hash object using the specified algorithm.
import hashlib
hash_obj = hashlib.new('sha256')
hash_obj.update(b'Hello, World!')
print(hash_obj.hexdigest()) # Outputs the SHA-256 hash
hashlib.md5
Creates a new MD5 hash object.
import hashlib
hash_obj = hashlib.md5()
hash_obj.update(b'Hello, World!')
print(hash_obj.hexdigest()) # Outputs the MD5 hash
hashlib.sha1
Creates a new SHA-1 hash object.
import hashlib
hash_obj = hashlib.sha1()
hash_obj.update(b'Hello, World!')
print(hash_obj.hexdigest()) # Outputs the SHA-1 hash
hashlib.sha224
Creates a new SHA-224 hash object.
import hashlib
hash_obj = hashlib.sha224()
hash_obj.update(b'Hello, World!')
print(hash_obj.hexdigest()) # Outputs the SHA-224 hash
hashlib.sha256
Creates a new SHA-256 hash object.
import hashlib
hash_obj = hashlib.sha256()
hash_obj.update(b'Hello, World!')
print(hash_obj.hexdigest()) # Outputs the SHA-256 hash
hashlib.sha384
Creates a new SHA-384 hash object.
import hashlib
hash_obj = hashlib.sha384()
hash_obj.update(b'Hello, World!')
print(hash_obj.hexdigest()) # Outputs the SHA-384 hash
hashlib.sha512
Creates a new SHA-512 hash object.
import hashlib
hash_obj = hashlib.sha512()
hash_obj.update(b'Hello, World!')
print(hash_obj.hexdigest()) # Outputs the SHA-512 hash
hashlib.blake2b
Creates a new BLAKE2b hash object.
import hashlib
hash_obj = hashlib.blake2b()
hash_obj.update(b'Hello, World!')
print(hash_obj.hexdigest()) # Outputs the BLAKE2b hash
hashlib.blake2s
Creates a new BLAKE2s hash object.
import hashlib
hash_obj = hashlib.blake2s()
hash_obj.update(b'Hello, World!')
print(hash_obj.hexdigest()) # Outputs the BLAKE2s hash
Examples
Creating Hashes
import hashlib
data = b'Hello, World!'
# MD5
md5_hash = hashlib.md5(data).hexdigest()
print(f'MD5: {md5_hash}')
# SHA-1
sha1_hash = hashlib.sha1(data).hexdigest()
print(f'SHA-1: {sha1_hash}')
# SHA-256
sha256_hash = hashlib.sha256(data).hexdigest()
print(f'SHA-256: {sha256_hash}')
Updating Hashes
import hashlib
hash_obj = hashlib.sha256()
hash_obj.update(b'Hello, ')
hash_obj.update(b'World!')
print(hash_obj.hexdigest()) # Outputs the SHA-256 hash for 'Hello, World!'
Hashing Files
import hashlib
def hash_file(filename, algorithm='sha256'):
hash_obj = hashlib.new(algorithm)
with open(filename, 'rb') as f:
while chunk := f.read(8192):
hash_obj.update(chunk)
return hash_obj.hexdigest()
file_hash = hash_file('example.txt')
print(f'SHA-256 hash of the file: {file_hash}')
Real-World Use Case
Verifying File Integrity
When downloading files from the internet, you can use hash functions to verify that the file has not been tampered with. The file provider will often provide a hash value, which you can compare with the hash of the downloaded file.
import hashlib
def verify_file_integrity(file_path, expected_hash, algorithm='sha256'):
hash_obj = hashlib.new(algorithm)
with open(file_path, 'rb') as f:
while chunk := f.read(8192):
hash_obj.update(chunk)
return hash_obj.hexdigest() == expected_hash
# Example usage
expected_hash = 'expected_sha256_hash_of_the_file'
file_path = 'example.txt'
if verify_file_integrity(file_path, expected_hash):
print('The file is intact.')
else:
print('The file has been tampered with.')
Conclusion
The hashlib
module in Python provides a secure and easy way to create cryptographic hashes. It supports a variety of hashing algorithms, making it suitable for different applications, such as verifying file integrity, ensuring data authenticity, and more.