In cryptography, a "salt" is an extra piece of data which is included when hashing a password. This makes rainbow-table attacks
more
difficult. Using a cryptographic hash function without an unpredictable salt increases the likelihood that an attacker could successfully find the
hash value in databases of precomputed hashes (called rainbow-tables
).
This rule raises an issue when a hashing function which has been specifically designed for hashing passwords, such as PBKDF2
, is used
with a non-random, reused or too short salt value. It does not raise an issue on base hashing algorithms such as sha1
or md5
as they should not be used to hash passwords.
Recommended Secure Coding Practices
- Use hashing functions generating their own secure salt or generate a secure random value of at least 16 bytes.
- The salt should be unique by user password.
Noncompliant Code Example
hashlib
import crypt
from hashlib import pbkdf2_hmac
hash = pbkdf2_hmac('sha256', password, b'D8VxSmTZt2E2YV454mkqAY5e', 100000) # Noncompliant: salt is hardcoded
crypt
hash = crypt.crypt(password) # Noncompliant: salt is not provided
Compliant Solution
hashlib
import crypt
from hashlib import pbkdf2_hmac
salt = os.urandom(32)
hash = pbkdf2_hmac('sha256', password, salt, 100000) # Compliant
crypt
salt = crypt.mksalt(crypt.METHOD_SHA256)
hash = crypt.crypt(password, salt) # Compliant
See