The crypto module provides cryptographic functionalities to help you perform encryption, hashing, and other security-related operations.

Example:

const crypto = require('crypto');

// Hashing data
const hash = crypto.createHash('sha256').update('Node.js').digest('hex');
console.log(hash); // Output: Hashed value of 'Node.js'

This line generates a SHA-256 hash for the string "Node.js" in hexadecimal format using Node.js’s crypto module:

const hash = crypto.createHash('sha256').update('Node.js').digest('hex');

Explanation:

  • crypto.createHash('sha256'): Creates a SHA-256 hash object.
  • .update('Node.js'): Adds the string "Node.js" to the hash calculation.
  • .digest('hex'): Completes the hash process and outputs the result in hexadecimal format.

The final hash variable contains the SHA-256 hash of "Node.js" as a hex string.

crypto.createHash('sha256').update('Node.js').digest('base64'); // Base64 encoding
crypto.createHash('sha256').update('Node.js').digest('latin1'); // Latin-1 encoding

The crypto module provides cryptographic functionalities to help you perform encryption, hashing, and other security-related operations.

Example:

const crypto = require('crypto');

// Hashing data
const hash = crypto.createHash('sha256').update('Node.js').digest('hex');
console.log(hash); // Output: Hashed value of 'Node.js'

This line generates a SHA-256 hash for the string "Node.js" in hexadecimal format using Node.js’s crypto module:

const hash = crypto.createHash('sha256').update('Node.js').digest('hex');

Explanation:

  • crypto.createHash('sha256'): Creates a SHA-256 hash object.
  • .update('Node.js'): Adds the string "Node.js" to the hash calculation.
  • .digest('hex'): Completes the hash process and outputs the result in hexadecimal format.

The final hash variable contains the SHA-256 hash of "Node.js" as a hex string.

crypto.createHash('sha256').update('Node.js').digest('base64'); // Base64 encoding
crypto.createHash('sha256').update('Node.js').digest('latin1'); // Latin-1 encoding