Hashing data is security-sensitive. It has led in the past to the following vulnerabilities:
Cryptographic hash functions are used to uniquely identify information without storing their original form. When not done properly, an attacker can
steal the original information by guessing it (ex: with a rainbow table), or replace the
original data with another one having the same hash.
This rule flags code that initiates hashing.
Ask Yourself Whether
- the hashed value is used in a security context.
- the hashing algorithm you are using is known to have vulnerabilities.
- salts are not automatically generated and applied by the hashing function.
- any generated salts are cryptographically weak or not credential-specific.
You are at risk if you answered yes to the first question and any of the following ones.
Recommended Secure Coding Practices
- for security related purposes, use only hashing algorithms which are currently known to be strong. Avoid using algorithms like MD5 and SHA1
completely in security contexts.
- do not define your own hashing- or salt algorithms as they will most probably have flaws.
- do not use algorithms that compute too quickly, like SHA256, as it must remain beyond modern hardware capabilities to perform brute force and
dictionary based attacks.
- use a hashing algorithm that generate its own salts as part of the hashing. If you generate your own salts, make sure that a cryptographically
strong salt algorithm is used, that generated salts are credential-specific, and finally, that the salt is applied correctly before the hashing.
- save both the salt and the hashed value in the relevant database record; during future validation operations, the salt and hash can then be
retrieved from the database. The hash is recalculated with the stored salt and the value being validated, and the result compared to the stored
hash.
- the strength of hashing algorithms often decreases over time as hardware capabilities increase. Check regularly that the algorithms you are
using are still considered secure. If needed, rehash your data using a stronger algorithm.
Questionable Code Example
// === Server side ===
const crypto = require("crypto");
const hash = crypto.createHash('sha1'); // Questionable regardless of algorithm used
crypto.scrypt(secret, salt, keylen, (err, derivedKey) => {}); // Questionable
const derivedKey = crypto.scryptSync(secret, salt, keylen); // Questionable
// === Client side ===
crypto.subtle.digest("SHA-256", buffer) // Questionable regardless of algorithm used
.then(function (hash) {});
See