Node.js Tutorial

Node.js Crypto Module

Node.js Crypto Module
Node.js Crypto Module

The Node.js crypto module provides cryptographic functionality through OpenSSL. It supports important security operations such as hashing, HMAC, encryption, decryption, digital signing, verification, and secure random data generation.

What is Hash?

Complete Advance AI Topics: Click Here
SQL Tutorial:
Click Here

A hash is a fixed-length value generated from input data using a hashing algorithm. The same input always produces the same hash, while even a small change in the input can produce a completely different result.

Hashing is commonly used for data integrity checks and password-related security. For password storage, dedicated password-hashing algorithms such as scrypt or bcrypt should be preferred over plain SHA-256.

What is HMAC?

HMAC (Hash-based Message Authentication Code) combines a cryptographic hash function with a secret key. It helps verify both the integrity and authenticity of data.

Because the secret key is required to generate the HMAC, someone without the key cannot produce a valid HMAC for the same message.

Example 1: HMAC with SHA-256

The following example creates an HMAC using the SHA-256 hashing algorithm and a secret key:

// crypto_example1.js
const crypto = require('crypto');

const secret = 'abcdefg';

const hash = crypto.createHmac('sha256', secret)
  .update('Welcome to UpdateGadh')
  .digest('hex');

console.log(hash);

Example 2: Modern AES Encryption

Node.js provides modern encryption APIs through createCipheriv(). The older createCipher() API is deprecated, so applications should use an algorithm, key, and initialization vector (IV) explicitly.

const crypto = require('crypto');

const algorithm = 'aes-192-cbc';
const password = 'a password';

const key = crypto.scryptSync(password, 'salt', 24);
const iv = Buffer.alloc(16, 0);

const cipher = crypto.createCipheriv(algorithm, key, iv);

let encrypted = cipher.update(
  'Hello UpdateGadh',
  'utf8',
  'hex'
);

encrypted += cipher.final('hex');

console.log(encrypted);

Example 3: Decryption

The encrypted data can be converted back to its original form using createDecipheriv() with the same algorithm, key, and IV.

const decipher = crypto.createDecipheriv(algorithm, key, iv);

let decrypted = decipher.update(
  encrypted,
  'hex',
  'utf8'
);

decrypted += decipher.final('utf8');

console.log(decrypted);

Common Use Cases of Node.js Crypto Module

  • Password Hashing: Use dedicated password-hashing algorithms such as scrypt or bcrypt instead of plain SHA hashing.
  • API Token Signing: HMAC can be used to verify the authenticity and integrity of API-related data.
  • Data Encryption: Encrypt sensitive information before storing it at rest.
  • Secure Random Tokens: Use crypto.randomBytes() to generate cryptographically secure random values.
  • Digital Signatures: The crypto module supports signing and verification for secure communication and data validation.

Download New Real Time Projects:- Click here

Conclusion

The Node.js crypto module provides production-ready cryptographic features for securing applications. It can be used for hashing, HMAC authentication, encryption, decryption, digital signatures, and secure random data generation.

For modern Node.js applications, prefer current IV-based encryption APIs such as createCipheriv() and use dedicated password-hashing functions for storing passwords securely.

Keywords

Node.js Crypto Module, Node.js crypto, Node.js encryption, Node.js decryption, Node.js hashing, Node.js HMAC, Node.js SHA-256, Node.js AES encryption, Node.js createCipheriv, Node.js createDecipheriv, Node.js randomBytes, Node.js cryptography

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us