To hash data in Node.js, import the built-in node:crypto module, call crypto.createHash("sha256"), feed it data with update(), and read the result with digest("hex"). The Node.js crypto hash functions are one-way: the same input always gives the same fixed-length output, and you cannot get the input back from the digest.
Hashes are used for checksums, cache keys, content-addressed storage and integrity checks. They are not the right tool for storing passwords on their own, and later sections show what to use instead.
createHash(algorithm) returns a Hash object. You can call update() several times, then digest() exactly once.
Splitting the input over two update() calls gives the same result as one call with "hello world". After digest() the object is finished, and calling update() or digest() again throws an error. Create a new Hash for each value.
The argument to digest() is the output encoding: "hex", "base64", "base64url", or none to get a Buffer.
The list of available algorithms depends on the OpenSSL build. You can print it with crypto.getHashes().
| Algorithm | Output size | Use it for |
|---|---|---|
sha256 | 32 bytes | General purpose checksums, ETags, cache keys |
sha512 | 64 bytes | Larger digests, often faster on 64-bit CPUs |
sha3-256 | 32 bytes | When you want a different design from SHA-2 |
md5 | 16 bytes | Only for legacy compatibility, not for security |
sha1 | 20 bytes | Only for legacy compatibility, such as Git object IDs |
MD5 and SHA-1 are broken against deliberate collisions, so never use them where an attacker could benefit from forging a match. sha256 is a safe default for new code.
Do not read a large file into memory just to hash it. A Hash is a stream, so you can pipe a file into it.
pipeline() handles errors and closes the stream for you. Memory use stays small even for multi-gigabyte files, because the file is processed in chunks.
Node.js 21.7 and 20.12 added the shortcut crypto.hash(algorithm, data, outputEncoding). It is faster than the streaming API for small inputs that are already in memory.
If your code must run on older Node.js versions, use createHash() instead. It works everywhere.
A plain hash proves nothing about who created the data, because anyone can compute it. An HMAC mixes in a secret key, so only someone with the key can produce or verify the value. Use it for signing webhooks or cookies.
Always compare signatures with timingSafeEqual(), not ===. It takes the same time whatever the input, which stops attackers from guessing a signature byte by byte through timing differences. Both buffers must have the same length, so check that first.
SHA-256 is designed to be fast, so an attacker can try billions of guesses per second against a leaked database. Password storage needs a slow, salted, memory-hard function. Node.js includes crypto.scrypt() for this.
A fresh random salt means two users with the same password get different stored values. Many teams use a maintained library for Argon2 or bcrypt instead, which is also a good choice.
| Operation | Reversible | Needs a key | Example |
|---|---|---|---|
| Encoding | Yes | No | Base64, hex |
| Encryption | Yes, with the key | Yes | AES-256-GCM |
| Hashing | No | No | SHA-256 |
| HMAC | No | Yes | HMAC-SHA-256 |
Base64 is not protection, and a hash cannot be "decrypted". Pick the operation that matches your goal.
Use sha256 for checksums, cache keys and integrity checks. Use HMAC-SHA-256 when you need to prove a message came from someone with a shared secret.
No. A hash is one-way. Weak inputs such as short passwords can be guessed by trying candidates, which is why password storage uses slow salted functions.
The Hash object is finalised by the first digest() call. Create a new Hash, or use hash.copy() before digesting if you need an intermediate result.
They are different APIs. Node.js also exposes Web Crypto as crypto.subtle for use with the same code as in browsers, but createHash is simpler for most server tasks.
Import node:crypto, create a hash with createHash("sha256"), call update() as needed and finish with digest("hex"). Stream files through a Hash to keep memory low, use HMAC with timingSafeEqual() for signatures, and use scrypt or Argon2 rather than SHA-256 for passwords. Avoid MD5 and SHA-1 for anything security related.