Node.js Reference ยท crypto
Node.js crypto.createHash() method
Reference for crypto.createHash() in Node.js: incremental hashing with update() and digest(), encodings, copy(), XOF output lengths and which algorithms to use.
crypto.createHash(algorithm) creates a Hash object that computes a message digest. You feed it data with update() as many times as you like and finish with digest(), which returns the hash. Because data can arrive in pieces, it works for strings, buffers and whole files read as streams.
A hash is a one-way fingerprint: the same input always gives the same output, a tiny change gives a very different output, and you cannot recover the input from the digest. Hashes are for integrity checks, cache keys and content addressing. They are not enough for storing passwords; use crypto.scrypt() for that.
import { createHash } from 'node:crypto'; const hex = createHash('sha256').update('hello').digest('hex');
Try it
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.
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
crypto.createHash(algorithm[, options]) // Hash hash.update(data[, inputEncoding]) // Hash (chainable) hash.digest([encoding]) // Buffer | string hash.copy([options]) // Hash
| Name | Description |
|---|---|
algorithm |
Digest name, such as 'sha256', 'sha384', 'sha512', 'sha3-256', 'sha1', 'md5' or 'shake256'. The list depends on your OpenSSL build; see crypto.getHashes(). |
options.outputLength |
For extendable-output functions (SHAKE), the digest size in bytes. |
data |
A string, Buffer, typed array or DataView. |
inputEncoding |
Encoding of a string data, default 'utf8'. |
encoding |
'hex', 'base64', 'base64url', 'latin1' or 'binary'. Without it, digest() returns a Buffer. |
Returns: Hash objects are also writable and readable streams, so stream.pipeline(file, hash) works.
Errors: an unknown algorithm throws Error: Digest method not supported. Calling update() or digest() after digest() throws ERR_CRYPTO_HASH_FINALIZED. Invalid argument types throw ERR_INVALID_ARG_TYPE.
Usage notes
- Choose SHA-256 or SHA-512 (or SHA-3) for new code.
md5andsha1are broken against attackers who can craft collisions; they are fine only for non-security checksums and legacy formats. - Hashing is not password storage. Fast hashes are easy to brute-force. Use
scryptorpbkdf2with a salt. - A plain hash is not authentication. Anyone can recompute it. To prove knowledge of a secret use
crypto.createHmac(). - Hash files as streams.
pipeline(createReadStream(path), createHash('sha256'))and then read the digest from the stream keeps memory flat for huge files. - Strings are encoded first. The same text in
utf8andutf16lehashes differently; be explicit when interoperating. - For a single small input
crypto.hash()is shorter and faster. - Compare digests with
timingSafeEqual()when the comparison guards something secret:crypto.timingSafeEqual().