Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท crypto

Node.js crypto.getHashes() and getCiphers()

Reference for crypto.getHashes(), getCiphers(), getCurves() and getCipherInfo() in Node.js: list the algorithms your build supports and check a name before using it.

Node.js delegates its cryptography to OpenSSL, and the set of algorithms you can use depends on the OpenSSL build behind your Node binary. crypto.getHashes(), crypto.getCiphers() and crypto.getCurves() each return an array of names, so you can check what is available instead of guessing. crypto.getCipherInfo() goes one step further and tells you the key length, IV length and mode of a single cipher.

These functions are mostly useful for diagnostics, for feature detection in libraries and for validating a configurable algorithm name before it reaches createHash() or createCipheriv().

NODE
import { getHashes, getCiphers, getCurves, getCipherInfo } from 'node:crypto';

console.log(getHashes().includes('sha256'));
console.log(getCipherInfo('aes-256-gcm').keyLength);

Try it

Node.js Checking for algorithms

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Describing a cipher

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
crypto.getHashes(): string[]
crypto.getCiphers(): string[]
crypto.getCurves(): string[]
crypto.getCipherInfo(nameOrNid: string | number, options?: { keyLength?: number, ivLength?: number }): object | undefined
Name Description
nameOrNid A cipher name such as 'aes-256-gcm', or its numeric OpenSSL identifier (NID).
options.keyLength A key length in bytes to test. If the cipher does not accept it, the result is undefined.
options.ivLength An IV length in bytes to test. Only meaningful for ciphers with a variable IV, such as GCM.

Returns: the three get* functions return a new array of lowercase strings. getCipherInfo() returns an object with name, nid, blockSize (absent for stream ciphers), ivLength (absent for ciphers without an IV), keyLength and mode ('cbc', 'ccm', 'cfb', 'ctr', 'ecb', 'gcm', 'ocb', 'ofb', 'stream', 'wrap' or 'xts'), or undefined if the cipher is unknown or the options do not fit.

Edge cases: the lists differ between Node builds, between operating systems and when OpenSSL providers (such as the legacy provider) are enabled. Treat them as runtime facts, not constants. Names can also appear in several spellings, for example RSA-SHA256 next to sha256WithRSAEncryption; the arrays hold what OpenSSL reports.

Usage notes

  • Never hard-code the length of these arrays or compare them with a stored list. Feature-detect the one algorithm you need with includes().
  • A name that appears in getHashes() is accepted by createHash(), createHmac() and createSign(), but hashes with variable output (SHAKE) also need the outputLength option.
  • Appearing in the list does not mean an algorithm is a good choice. md5 and sha1 are listed for compatibility only; use SHA-256 or stronger for new designs.
  • getCipherInfo() is the safe way to read key and IV sizes. A GCM cipher reports an ivLength of 12 (the recommended size) but accepts others; pass options.ivLength to test one.
  • getCiphers() includes both authenticated modes (GCM, CCM, OCB, ChaCha20-Poly1305) and unauthenticated ones (CBC, CTR). Prefer the authenticated ones.
  • In FIPS mode the lists shrink to the approved algorithms. crypto.getFips() tells you whether the mode is on.
  • Calling the functions is cheap enough for start-up checks, but cache the result in a Set if you test many names.