Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Node.js · Module

crypto

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.

How it works

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.

Syntax

import { createHash } from 'node:crypto';
const hash = createHash(algorithm);
hash.update(data);
const digest = hash.digest([encoding]);

Parameters

NameTypeDescription
algorithmstringHash algorithm name, such as sha256.
dataBinaryLikeData supplied to the hash through update(); commonly a string, Buffer, or typed-array-compatible value.

Returns

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.

Key points

  • Import the current module as node:crypto; the node: prefix makes the built-in-module intent explicit.
  • Use createHash(algorithm) to construct a digest operation for a supported hash algorithm.
  • Use the key-construction helpers to turn symmetric, public, or private key material into KeyObject instances.
  • A Hash instance accumulates input before its final digest is requested.
  • Use Verify when the task is checking a digital signature against supplied data and key material.
  • Create a verifier with createVerify(algorithm) and then provide the signed content before checking the signature.

Common mistakes

  • Signature verification requires feeding the same signed data into the verifier before calling its final verification method.

Example: Hash text with SHA-256

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.

When to use it

Hash data

Create a stable fingerprint for content, cache keys, or integrity checks.

Compute an HMAC

Authenticate messages when both parties share a secret.

Sign and verify data

Prove authorship or validate signed payloads with asymmetric keys.

Work with KeyObject instances

Generate or manage symmetric and asymmetric cryptographic keys.

Interview questions

QWhat does the Node.js crypto module provide?

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.

QHow is a digest produced?

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.

QHow does signature verification work?

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.

  • Hash
  • Hmac
  • Sign
  • Verify
  • KeyObject
  • createHmac()
  • createSign()
  • createVerify()

Source: Crypto. Checked 2026-10-15.