Python hashlib Module

Python hashlibThis module is mainly used for hash operations.

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

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

To usehashlibfunctions must be imported first:

import hashlib 

View the contents of the hashlib module:

Example

>>> 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:

Common Methods

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

The name parameter is the name of 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'EXAMPLE')
print(sha256_hash.hexdigest())

The output result is:

673dc967d03201db7fe47b7eabd56c47ca5bc694222de303106a5504e5d0daa8

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

Example

import hashlib

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

The output result is:

18fa661e2a4a7dd6471cc1407290cf6e

Hash Object Methods

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

Example

import hashlib

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

The output result is:

1b56561022276e9a5a8e1cda72e1b39fca6f6074326a74d39f6dfd9540c8ecd7

hexdigest(): Get the hash value in hexadecimal representation.

Example

import hashlib

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

The output result is:

18fa661e2a4a7dd6471cc1407290cf6e

digest(): Get the hash value in binary representation.

Example

import hashlib

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

The output result is:

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'EXAMPLE')
print(md5_hash.hexdigest())

The output result is:

18fa661e2a4a7dd6471cc1407290cf6e

SHA-1

Example

import hashlib

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

The output result is:

341709d0dbc266332f1cbcd8c25fd4a05412b7d4

SHA-256

Example

import hashlib

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

The output result is:

673dc967d03201db7fe47b7eabd56c47ca5bc694222de303106a5504e5d0daa8

SHA-512

Example

import hashlib

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

The output result is:

7cfe50493eebd48ee7330c797459c2d0d5ca943bd1c84ad7a0b6783b11cd49d06b4a1dc84ee9ea5e20d0bfedbdb67e716500a20e5870abecea3f32dc8484a811

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

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

Algorithm NameDigest Length (bits)Output Length (bytes)SecurityUsage
md512816InsecureData integrity verification, password storage, etc.
sha116020InsecureData integrity verification, password storage, etc.
sha22422428LowData integrity verification, digital signatures, etc.
sha25625632MediumData integrity verification, digital signatures, etc.
sha38438448HighDigital signatures, encryption algorithms, etc.
sha51251264HighDigital signatures, encryption algorithms, etc.
sha3_22422428HighA member of the future standard SHA-3 family, suitable for digital signatures, etc.
sha3_25625632HighA member of the future standard SHA-3 family, suitable for digital signatures, etc.
sha3_38438448HighA member of the future standard SHA-3 family, suitable for digital signatures, etc.
sha3_51251264HighA member of the future standard SHA-3 family, suitable for digital signatures, etc.
shake_128VariableVariableHighThe SHAKE series is a variable-length version of the SHA-3 family, suitable for various applications.
shake_256VariableVariableHighThe SHAKE series is a variable-length version of the SHA-3 family, suitable for various applications.

Notes:

  • Digest length (bits):Indicates the digest length output by the hash algorithm, in bits.
  • Output length (bytes):Indicates the digest length output by the hash algorithm, in bytes.
  • Security:Indicates the security level of the hash algorithm, including "insecure", "low", "medium", and "high". This is a general classification; the specific security also depends on the algorithm's usage and specific attack scenarios.
Other Extensions