Skip to content

hashlib — weak hash

Reversible One Way Hash in hashlib Module

The Python module hashlib provides a number of functions for hashing data. However, some of the hash algorithms supported by hashlib are insecure and should not be used. These insecure hash algorithms include MD4, MD5, RIPEMD-160 and SHA-1.

The MD4 hash algorithm is a cryptographic hash function that was designed in the late 1980s. MD4 is no longer considered secure, and passwords hashed with MD4 can be easily cracked by attackers.

The MD5 hash algorithm is a cryptographic hash function that was designed in the early 1990s. MD5 is no longer considered secure, and passwords hashed with MD5 can be easily cracked by attackers.

RIPEMD-160 is a cryptographic hash function that was designed in 1996. It is considered to be a secure hash function, but it is not as secure as SHA-256, SHA-384, or SHA-512. In 2017, a collision attack was found for RIPEMD-160. This means that it is possible to find two different messages that have the same RIPEMD-160 hash. While this does not mean that RIPEMD-160 is completely insecure, it does mean that it is not as secure as it once was.

The SHA-1 hash algorithm is also a cryptographic hash function that was designed in the early 1990s. SHA-1 is no longer considered secure, and passwords hashed with SHA-1 can be easily cracked by attackers.

Example

hashlib_md5.py
1
2
3
4
import hashlib


hashlib.md5()
Example Output
> precli tests/unit/rules/python/stdlib/hashlib/examples/hashlib_md5.py
⛔️ Error on line 4 in tests/unit/rules/python/stdlib/hashlib/examples/hashlib_md5.py
PY004: Use of Weak Hash
The hash function 'hashlib.md5' is vulnerable to collision and pre-image attacks.

Remediation

The recommendation is to swap the insecure hashing method to one of the more secure alternatives, SHA256 or SHA512.

hashlib_md5.py
1
2
3
4
5
import hashlib


hash = hashlib.sha256(b"Nobody inspects the spammish repetition")
hash.hexdigest()

If an insecure hash such as MD5 must be used and not in within a security context, then set the keyword-only argument usedforsecurity in the hashes constructor.

1
2
3
4
5
import hashlib


hash = hashlib.md5(b"Non-security related text", usedforsecurity=False)
hash.hexdigest()

Default Configuration

enabled = true
level = "error"
weak_hashes = [
  "md4",
  "md5",
  "md5-sha1",
  "ripemd160",
  "sha",
  "sha1",
]

See also

New in version 0.1.0

Changed in version 0.4.1: Added md5-sha1