bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/Node.js/Core Modules
Node.js•Core Modules

Node.js Crypto Module

Concept visual

Node.js Crypto Module

Graph traversalgraph
ABCDE
current
queued
1
4

Start from A

What is the Crypto Module?

Formula

The Crypto module is a built - in Node.js module that provides cryptographic functionality including:
Hash functions (SHA - 256, SHA - 512, etc.)
HMAC (Hash - based Message Authentication Code)

Symmetric encryption (AES, DES, etc.) Asymmetric encryption (RSA, ECDSA, etc.)

Digital signatures and verification

Secure random number generation

The Crypto module is essential for applications that need to handle sensitive information securely.

Formula

The Crypto module wraps the OpenSSL library, providing access to well - established and tested cryptographic algorithms.

This module is often used to handle sensitive data, such as:

User authentication and password storage

Secure data transmission

File encryption and decryption

Secure communication channels

Getting Started with Crypto

Here's a quick example of using the Crypto module to hash a string:

Basic Hashing Example

const crypto = require('crypto');
// Create a SHA-256 hash of a string const hash = crypto.createHash('sha256').update('Hello, Node.js!').digest('hex');
console.log('SHA-256 Hash:', hash);

Installing the Crypto Module

The Crypto module is included in Node.js by default. You can use it by requiring it in your script:

const crypto = require('crypto');

Hash Functions

Formula

Hashing is a one - way transformation of data into a fixed - length string of characters.

Hash functions have several important properties:

Deterministic:

Same input always produces the same output

Fixed Length:

Output is always the same size regardless of input size

One-Way:

Extremely difficult to reverse the process

Avalanche Effect:

Small changes in input produce significant changes in output

Common use cases include:

Password storage

Data integrity verification

Digital signatures

Content addressing (e.g., Git, IPFS)

Creating a Hash const crypto = require('crypto');
// Create a hash object const hash = crypto.createHash('sha256');
// Update the hash with data hash.update('Hello, World!');
// Get the digest in hexadecimal format const digest = hash.digest('hex');
console.log(digest);

In this example:

createHash() creates a hash object with the specified algorithm update() updates the hash content with the given data digest() calculates the digest and outputs it in the specified format

Common Hash Algorithms const crypto = require('crypto');
const data = 'Hello, World!';

Formula

// MD5 (not recommended for security - critical applications)
const md5 = crypto.createHash('md5').update(data).digest('hex');
console.log('MD5:', md5);

Formula

// SHA - 1 (not recommended for security - critical applications)
const sha1 = crypto.createHash('sha1').update(data).digest('hex');
console.log('SHA-1:', sha1);
// SHA-256 const sha256 = crypto.createHash('sha256').update(data).digest('hex');
console.log('SHA-256:', sha256);
// SHA-512 const sha512 = crypto.createHash('sha512').update(data).digest('hex');
console.log('SHA-512:', sha512);

Warning:

Formula

MD5 and SHA - 1 are considered cryptographically weak and should not be used for security - critical applications.
Use SHA - 256, SHA - 384, or SHA - 512 instead.

Password Security

When handling passwords, it's crucial to use specialized password hashing functions that are designed to be computationally expensive to prevent brute-force attacks. Here's why simple hashes are insufficient:

Formula

Never store passwords in plain text or with simple hashes like MD5 or SHA - 1.
These can be easily cracked using rainbow tables or brute - force attacks.

Previous

Node.js Buffer Module

Next

Node.js Timers Module