Skip to content

Python hashlib Module

Python hashlib module is mainly used for hash operations.

Hash is an algorithm that maps input data of arbitrary length to fixed-length output data.

Hashing is typically used for verifying data integrity, securely storing passwords, and other scenarios.

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, such as MD5, SHA-1, SHA-256, etc.

To use hashlib functions, you must first import:

import hashlib 

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']

The following is an introduction to some common methods and hash algorithms in the hashlib module:

hashlib.new(name, data=None): Creates a hash object.

The name parameter is the name of the hash algorithm, and the data parameter is the data to be hashed.

import hashlib

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

Output result:

673dc967d03201db7fe47b7eabd56c47ca5bc694222de303106a5504e5d0daa8

hashlib.md5() / hashlib.sha1() / hashlib.sha256() / …: Directly use a specific hash algorithm to create a hash object.

import hashlib

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

Output result:

18fa661e2a4a7dd6471cc1407290cf6e

update(data): Updates the message content of the hash object.

import hashlib

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

Output result:

1b56561022276e9a5a8e1cda72e1b39fca6f6074326a74d39f6dfd9540c8ecd7

hexdigest(): Gets the hexadecimal representation of the hash value.

import hashlib

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

Output result:

18fa661e2a4a7dd6471cc1407290cf6e

digest(): Gets the binary representation of the hash value.

import hashlib

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

Output result:

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

MD5

import hashlib

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

Output result:

18fa661e2a4a7dd6471cc1407290cf6e

SHA-1

import hashlib

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

Output result:

341709d0dbc266332f1cbcd8c25fd4a05412b7d4

SHA-256

import hashlib

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

Output result:

673dc967d03201db7fe47b7eabd56c47ca5bc694222de303106a5504e5d0daa8

SHA-512

import hashlib

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

Output result:

7cfe50493eebd48ee7330c797459c2d0d5ca943bd1c84ad7a0b6783b11cd49d06b4a1dc84ee9ea5e20d0bfedbdb67e716500a20e5870abecea3f32dc8484a811

In practical applications, choosing the appropriate hash algorithm depends on specific requirements. It should be noted that MD5 and SHA-1 are already considered insecure, especially in security-related fields. It is recommended to use stronger algorithms, such as SHA-256 or SHA-512.

Common hash algorithms and their meanings in the Python hashlib module:

Algorithm Name Digest Length (bits) Output Length (bytes) Security Usage
md5 128 16 Insecure Data integrity verification, password storage, etc.
sha1 160 20 Insecure Data integrity verification, password storage, etc.
sha224 224 28 Low Data integrity verification, digital signatures, etc.
sha256 256 32 Medium Data integrity verification, digital signatures, etc.
sha384 384 48 High Digital signatures, encryption algorithms, etc.
sha512 512 64 High Digital signatures, encryption algorithms, etc.
sha3_224 224 28 High SHA-3 family member for future standards, suitable for digital signatures, etc.
sha3_256 256 32 High SHA-3 family member for future standards, suitable for digital signatures, etc.
sha3_384 384 48 High SHA-3 family member for future standards, suitable for digital signatures, etc.
sha3_512 512 64 High SHA-3 family member for future standards, suitable for digital signatures, etc.
shake_128 Variable Variable High SHAKE series is a variable-length version of the SHA-3 family, suitable for various applications.
shake_256 Variable Variable High SHAKE series is a variable-length version of the SHA-3 family, suitable for various applications.

Description:

  • Digest Length (bits): Represents the digest length output by the hash algorithm, in bits.
  • Output Length (bytes): Represents the digest length output by the hash algorithm, in bytes.
  • Security: Represents the security level of the hash algorithm, including “Insecure”, “Low”, “Medium”, “High”. This is a general classification; specific security also depends on the algorithm’s usage and specific attack scenarios.