Python hashlib Module
Python hashlib module is mainly used for hashing (hash) operations.
A hash is an algorithm that maps input data of arbitrary length to output data of fixed length.
Hashes are commonly used for verifying data integrity, securely storing passwords, and similar scenarios.
The output of a hash function is usually a seemingly random string of letters and numbers.
The hashlib module provides implementations of common hash algorithms, such as MD5, SHA-1, SHA-256, etc.
To use hashlib function 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 are some common methods and a brief introduction to the hash algorithms in the hashlib module:
// 2022-01-01 00:00:00 GMT
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
'RUNOOB'
)
(
sha256_hash.
hexdigest
(
)
)
The output 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
'RUNOOB'
)
(
md5_hash.
hexdigest
(
)
)
The output 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
'Runoob!'
)
(
sha256_hash.
hexdigest
(
)
)
The output is:
1b56561022276e9a5a8e1cda72e1b39fca6f6074326a74d39f6dfd9540c8ecd7
hexdigest(): Get the hash value in hexadecimal representation.
Example
import
hashlib
md5_hash
=
hashlib.
md5
(
b
'RUNOOB'
)
(
md5_hash.
hexdigest
(
)
)
The output is:
18fa661e2a4a7dd6471cc1407290cf6e
digest(): Get the hash value in binary representation.
Example
import
hashlib
sha1_hash
=
hashlib.
sha1
(
b
'RUNOOB'
)
(
sha1_hash.
digest
(
)
)
The output 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
'RUNOOB'
)
(
md5_hash.
hexdigest
(
)
)
The output is:
18fa661e2a4a7dd6471cc1407290cf6e
SHA-1
Example
import
hashlib
sha1_hash
=
hashlib.
sha1
(
b
'RUNOOB'
)
(
sha1_hash.
hexdigest
(
)
)
The output is:
341709d0dbc266332f1cbcd8c25fd4a05412b7d4
SHA-256
Example
import
hashlib
sha256_hash
=
hashlib.
sha256
(
b
'RUNOOB'
)
(
sha256_hash.
hexdigest
(
)
)
The output is:
673dc967d03201db7fe47b7eabd56c47ca5bc694222de303106a5504e5d0daa8
SHA-512
Example
import
hashlib
sha512_hash
=
hashlib.
sha512
(
b
'RUNOOB'
)
(
sha512_hash.
hexdigest
(
)
)
The output is:
7cfe50493eebd48ee7330c797459c2d0d5ca943bd1c84ad7a0b6783b11cd49d06b4a1dc84ee9ea5e20d0bfedbdb67e716500a20e5870abecea3f32dc8484a811
In practical applications, choosing an appropriate hash algorithm depends on specific needs. Note that MD5 and SHA-1 are considered insecure, especially in security contexts. 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 name | Digest length (bits) | Output length (bytes) | Safety | Purpose |
|---|---|---|---|---|
| md5 | 128 | 16 | Unsafe | Data integrity verification, password storage, etc. |
| sha1 | 160 | 20 | Unsafe | 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 | A member of the future standard SHA-3 family, suitable for digital signatures, etc. |
| sha3_256 | 256 | 32 | High | A member of the future standard SHA-3 family, suitable for digital signatures, etc. |
| sha3_384 | 384 | 48 | High | A member of the future standard SHA-3 family, suitable for digital signatures, etc. |
| sha3_512 | 512 | 64 | High | A member of the future standard SHA-3 family, suitable for digital signatures, etc. |
| shake_128 | Mutable | Mutable | High | The SHAKE series is the variable-length version of the SHA-3 family, suitable for various applications |
| shake_256 | Mutable | Mutable | High | The SHAKE series is the variable-length version of the SHA-3 family, suitable for various applications |
Explanation:
- Digest length (bits): indicates the digest length of the hash algorithm's output, in bits.
- Output length (bytes): indicates the digest length of the hash algorithm's output, in bytes.
- Security: indicates the security level of the hash algorithm, including "insecure", "low", "medium", and "high". This is a general classification; the actual security also depends on the algorithm's purpose and specific attack scenarios.