Node.js’s crypto
module provides a range of cryptographic functionalities including set of wrappers for OpenSSL’s hash, HMAC, cipher, decipher, sign, and verify functions. It’s an essential tool for securing data and communications in Node.js applications, whether it’s encrypting passwords, creating secure tokens, or verifying data integrity through digital signatures.
Key components and functions:
- Hashes: Creating a hash of data (like passwords) allows you to store a fixed-size representation. Itβs a one-way process, meaning you can’t recover the original data from the hash.
crypto.createHash(algorithm)
: Creates and returns a hash object that can be used to generate hash digests using the specified algorithm.
- Cryptographic Ciphers: Ciphers allow for data to be encrypted and decrypted.
crypto.createCipheriv(algorithm, key, iv)
: Creates a Cipher object using the specific algorithm, key, and an initialization vector (iv).crypto.createDecipheriv(algorithm, key, iv)
: Counterpart for decryption.
- HMAC: HMAC (Hash-based Message Authentication Code) is used to verify data integrity and authenticate messages.
crypto.createHmac(algorithm, key)
: Creates and returns an Hmac object that uses the specified algorithm and key.
- Digital Signatures: These are used to authenticate the source and integrity of data.
crypto.createSign(algorithm)
: Used to create signatures.crypto.createVerify(algorithm)
: Used to verify signatures.
Encrypting and decrypting data
Below is a practical example of how to encrypt and decrypt data using the crypto
module. This code can be executed in any standard Node.js environment, including online compilers.
const crypto = require('crypto');
// Change these constants to try different encryption algorithms and key/iv sizes.
const ALGORITHM = 'aes-256-cbc'; // Encryption algorithm
const KEY = crypto.randomBytes(32); // Secret key (32 bytes for aes-256)
const IV = crypto.randomBytes(16); // Initialization vector (16 bytes for aes-256)
// Function to encrypt the data
function encrypt(text) {
const cipher = crypto.createCipheriv(ALGORITHM, KEY, IV);
let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
}
// Function to decrypt the data
function decrypt(encrypted) {
const decipher = crypto.createDecipheriv(ALGORITHM, KEY, IV);
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
// Test the functions
const originalText = 'plaintext data';
const encryptedText = encrypt(originalText);
const decryptedText = decrypt(encryptedText);
console.log('Original:', originalText);
console.log('Encrypted:', encryptedText);
console.log('Decrypted:', decryptedText);
This script starts by requiring the crypto
module and setting up constants for the encryption algorithm and keys. The encrypt
function takes a text input and returns an encrypted version, while the decrypt
function does the opposite. We test these functions with a string, displaying the original, encrypted, and decrypted versions of the string in the console.
Get more articles on Java Script
Read more on Shell and Linux related articles
Refer more on python here : Python