Python Tutorial (46) - hashlib module

Time: Column:Python views:258

The Python hashlib module is primarily used for performing hash operations.

Hashing is an algorithm that maps input data of any length to fixed-length output data. It is commonly used in scenarios such as verifying data integrity and securely storing passwords.

The output of a hash function is usually a string of seemingly random letters and numbers.

The hashlib module provides implementations of common hash algorithms like MD5, SHA-1, SHA-256, and more.

Importing the hashlib Module

To use the functions from the hashlib module, you must first import it:

import hashlib

Exploring the hashlib Module

To view the contents of the hashlib module:

>>> import hashlib
>>> dir(hashlib)
['__all__', '__block_openssl_constructor', '__builtin_constructor_cache', '__builtins__', '__cached__', '__doc__', '__file__', '__get_builtin_constructor', '__loader__', '__name__', '__package__', '__spec__', '_hashlib', 'algorithms_available', 'algorithms_guaranteed', 'blake2b', 'blake2s', 'md5', 'new', 'pbkdf2_hmac', 'scrypt', 'sha1', 'sha224', 'sha256', 'sha384', 'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512', 'sha512', 'shake_128', 'shake_256']

Common Methods and Hash Algorithms

Here are some of the common methods in the hashlib module and an introduction to different hash algorithms:

Common Methods

  • hashlib.new(name, data=None): Creates a new hash object. The name parameter specifies the hash algorithm, and the data parameter is the data to be hashed.

Example:

import hashlib

sha256_hash = hashlib.new('sha256')
sha256_hash.update(b'PMeve')
print(sha256_hash.hexdigest())

Output:

673dc967d03201db7fe47b7eabd56c47ca5bc694222de303106a5504e5d0daa8
  • hashlib.md5() / hashlib.sha1() / hashlib.sha256() / ...: Directly create a hash object using a specific hash algorithm.

Example:

import hashlib

md5_hash = hashlib.md5(b'PMeve')
print(md5_hash.hexdigest())

Output:

18fa661e2a4a7dd6471cc1407290cf6e

Hash Object Methods

  • update(data): Updates the hash object with additional data.

Example:

import hashlib

sha256_hash = hashlib.sha256()
sha256_hash.update(b'Hello, ')
sha256_hash.update(b'PMeve!')
print(sha256_hash.hexdigest())

Output:

1b56561022276e9a5a8e1cda72e1b39fca6f6074326a74d39f6dfd9540c8ecd7
  • hexdigest(): Retrieves the hexadecimal representation of the hash value.

Example:

import hashlib

md5_hash = hashlib.md5(b'PMeve')
print(md5_hash.hexdigest())

Output:

18fa661e2a4a7dd6471cc1407290cf6e
  • digest(): Retrieves the binary representation of the hash value.

Example:

import hashlib

sha1_hash = hashlib.sha1(b'PMeve')
print(sha1_hash.digest())

Output:

b'4\x17\t\xd0\xdb\xc2f3/\x1c\xbc\xd8\xc2_\xd4\xa0T\x12\xb7\xd4'

Common Hash Algorithms

MD5

Example:

import hashlib

md5_hash = hashlib.md5(b'PMeve')
print(md5_hash.hexdigest())

Output:

18fa661e2a4a7dd6471cc1407290cf6e

SHA-1

Example:

import hashlib

sha1_hash = hashlib.sha1(b'PMeve')
print(sha1_hash.hexdigest())

Output:

341709d0dbc266332f1cbcd8c25fd4a05412b7d4

SHA-256

Example:

import hashlib

sha256_hash = hashlib.sha256(b'PMeve')
print(sha256_hash.hexdigest())

Output:

673dc967d03201db7fe47b7eabd56c47ca5bc694222de303106a5504e5d0daa8

SHA-512

Example:

import hashlib

sha512_hash = hashlib.sha512(b'PMeve')
print(sha512_hash.hexdigest())

Output:

7cfe50493eebd48ee7330c797459c2d0d5ca943bd1c84ad7a0b6783b11cd49d06b4a1dc84ee9ea5e20d0bfedbdb67e716500a20e5870abecea3f32dc8484a811

Choosing the Right Hash Algorithm

The choice of a suitable hash algorithm depends on the specific requirements. It is important to note that MD5 and SHA-1 are now considered insecure, especially in security-related fields. It is recommended to use stronger algorithms like SHA-256 or SHA-512.

Common Hash Algorithms in Python hashlib Module:

AlgorithmDigest Length (bits)Output Length (bytes)SecurityUsage
MD512816InsecureData integrity verification, password storage, etc.
SHA-116020InsecureData integrity verification, password storage, etc.
SHA-22422428LowData integrity verification, digital signatures, etc.
SHA-25625632MediumData integrity verification, digital signatures, etc.
SHA-38438448HighDigital signatures, encryption, etc.
SHA-51251264HighDigital signatures, encryption, etc.
SHA3-22422428HighFuture SHA-3 family standard, suitable for digital signatures, etc.
SHA3-25625632HighFuture SHA-3 family standard, suitable for digital signatures, etc.
SHA3-38438448HighFuture SHA-3 family standard, suitable for digital signatures, etc.
SHA3-51251264HighFuture SHA-3 family standard, suitable for digital signatures, etc.
SHAKE-128VariableVariableHighSHAKE series is a variable-length version of SHA-3, suitable for various applications
SHAKE-256VariableVariableHighSHAKE series is a variable-length version of SHA-3, suitable for various applications

Note:

  • Digest Length (bits): Represents the length of the hash output in bits.

  • Output Length (bytes): Represents the length of the hash output in bytes.

  • Security: Indicates the general security level of the algorithm, categorized as "Insecure", "Low", "Medium", or "High".