An invalid crypto digest algorithm was used.
Production Risk
Low. This is typically a configuration or startup error that is caught during development. If it occurs in production, it's likely due to a misconfiguration.
This error is thrown by the `crypto` module when you try to use a digest algorithm (a hash function) that is not supported by the version of OpenSSL that your Node.js binary was compiled with. For example, using a misspelled algorithm name or an obsolete one like 'md2' will trigger this error.
- 1A typo in the algorithm name (e.g., 'sha265' instead of 'sha256').
- 2Attempting to use an algorithm that is not supported or has been deprecated for security reasons.
- 3Dynamically generating an algorithm name that results in an invalid string.
This error occurs when `crypto.createHash()`, `crypto.createHmac()`, or similar methods are called with an algorithm name that is not found in the list of available digests.
const crypto = require('crypto');
try {
// 'sha3-512' is a valid algorithm, but 'sha3' is not.
crypto.createHash('sha3');
} catch (err) {
console.error(err.message);
}expected output
Digest method not supported
Fix 1
Use a Supported Algorithm
WHEN Creating a hash, HMAC, or other cryptographic primitive that requires a digest.
const crypto = require('crypto');
// Use a valid and secure algorithm like 'sha256'.
const hash = crypto.createHash('sha256');
hash.update('some data');
console.log(hash.digest('hex'));Why this works
Use a known, valid digest algorithm. You can get a list of all available algorithms by calling `crypto.getHashes()`.
Fix 2
Validate Dynamic Algorithm Names
WHEN The algorithm name is provided by user input or configuration.
const crypto = require('crypto');
const supportedAlgs = crypto.getHashes();
function createHashFromConfig(alg) {
if (!supportedAlgs.includes(alg)) {
throw new Error('Unsupported hash algorithm: ' + alg);
}
return crypto.createHash(alg);
}Why this works
If the algorithm name is not a fixed string, validate it against the array of supported hashes returned by `crypto.getHashes()` before using it.
const crypto = require('crypto');
try {
// 'sha3-512' is a valid algorithm, but 'sha3' is not.
crypto.createHash('sha3');
} catch (err) {
console.error(err.message); // this triggers ERR_CRYPTO_INVALID_DIGESTtry {
// operation that may throw ERR_CRYPTO_INVALID_DIGEST
riskyOperation()
} catch (err) {
if (err.code === 'ERR_CRYPTO_INVALID_DIGEST') {
console.error('ERR_CRYPTO_INVALID_DIGEST:', err.message)
} else {
throw err
}
}const { getHashes } = require('crypto')
function createHash(alg) {
if (!getHashes().includes(alg)) throw new Error(`Unsupported hash: ${alg}`)
return require('crypto').createHash(alg)
}✕
https://github.com/nodejs/node/blob/main/src/crypto/crypto_util.cc
More information ↗Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev