Member-only story
Node.js provides built-in support for cryptographic operations through the crypto
module, which allows you to perform various encryption, decryption, hashing, signing, and key generation tasks. This module is based on OpenSSL and offers a wide range of cryptographic functionalities that are essential for secure communication, data integrity, and password management.
Not a Medium member? Read this article here
Here’s an overview of how Node.js uses cryptography along with practical examples:
1. Importing the crypto
Module
You need to require the crypto
module to use its features:
const crypto = require('crypto');
2. Hashing Data
Hashing is a one-way function that takes an input and produces a fixed-size string, typically used for passwords and data integrity checks.
Example: Hashing a Password with SHA-256
const hash = crypto.createHash('sha256');
hash.update('myPassword123');
const hashedPassword = hash.digest('hex');
console.log('Hashed Password:', hashedPassword);
You can replace 'sha256'
with other algorithms like 'md5'
, 'sha512'
, etc., but avoid MD5 and SHA-1 for security reasons in production.