Reversible One Way Hash in PyCrypto
Module
The Python module pycrypto
provides a number of functions for hashing data.
However, some of the hash algorithms supported by pycrypto
are insecure and
should not be used. These insecure hash algorithms include MD2
, MD4
, MD5
,
RIPEMD
and SHA
.
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 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 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
from Crypto.Hash import MD2
h = MD2.new()
h.update(b'Hello')
print h.hexdigest()
Remediation
The recommendation is to swap the insecure hashing method to one of the more
secure alternatives, SHA256
, SHA384
, or SHA512
.
from Crypto.Hash import SHA256
h = SHA256.new()
h.update(b'Hello')
print h.hexdigest()
False Positives
In the case of a false positive the rule can be suppressed. Simply add a
trailing or preceding comment line with either the rule ID (PY514
) or
rule category name (reversible_one_way_hash
).
- Using rule ID
- Using category name
from Crypto.Hash import MD2
# suppress: PY514
h = MD2.new()
h.update(b'Hello')
print h.hexdigest()
from Crypto.Hash import MD2
# suppress: reversible_one_way_hash
h = MD2.new()
h.update(b'Hello')
print h.hexdigest()