Hash data
Create a stable fingerprint for content, cache keys, or integrity checks.
Node.js · Module
Node.js node:crypto provides OpenSSL-backed APIs for hashing, HMAC, encryption, decryption, signing, verification, randomness, and key management. Its operation-specific objects consume data and produce a final result.
The node:crypto module exposes cryptographic operations backed by OpenSSL. Create an operation-specific object, provide data through update-style methods, and finish with a method such as digest, encrypt, sign, or verify; key-related APIs use KeyObject instances for symmetric or asymmetric keys.
import { createHash } from 'node:crypto';
const hash = createHash(algorithm);
hash.update(data);
const digest = hash.digest([encoding]);| Name | Type | Description |
|---|---|---|
algorithm | string | Hash algorithm name, such as sha256. |
data | BinaryLike | Data supplied to the hash through update(); commonly a string, Buffer, or typed-array-compatible value. |
The module exports cryptographic functions, classes, constants, and key-related types. createHash() returns a Hash instance; a Hash's digest() returns the completed digest as a Buffer or encoded string.
node:crypto; the node: prefix makes the built-in-module intent explicit.createHash(algorithm) to construct a digest operation for a supported hash algorithm.createVerify(algorithm) and then provide the signed content before checking the signature.import { createHash } from 'node:crypto';
const digest = createHash('sha256')
.update('hello')
.digest('hex');
console.log(digest);Creates a SHA-256 hash, feeds it text, and prints the final digest as hexadecimal.
Create a stable fingerprint for content, cache keys, or integrity checks.
Authenticate messages when both parties share a secret.
Prove authorship or validate signed payloads with asymmetric keys.
Generate or manage symmetric and asymmetric cryptographic keys.
The module supplies wrappers for common OpenSSL-backed operations, including hashing, HMAC, encryption, decryption, signing, and verification. It also provides key-management APIs through KeyObject instances.
Follow-up: How would you represent an asymmetric key? Use crypto.createPublicKey() or crypto.createPrivateKey() to create the corresponding KeyObject.
createHash() creates a Hash instance. Pass data with update(), then call digest() to obtain the hash output in a selected encoding or as a buffer.
Follow-up: What is a key distinction between hashing and encryption? A hash produces a digest for integrity or identification; encryption is intended to be reversed with the appropriate key.
A Verify instance can consume data through its update() method and then check a signature with verify(). The verification result indicates whether the signature matches the supplied data and key.
Follow-up: Which key is normally used to verify a public-key signature? The public key corresponding to the private key that created the signature.
HashHmacSignVerifyKeyObjectcreateHmac()createSign()createVerify()