Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
crypto.createHash() Method in Node.js
The crypto.createHash() method creates a hash object that can generate hash digests using cryptographic algorithms like SHA-256, MD5, or SHA-512. It's commonly used for password hashing, data integrity verification, and digital signatures.
Syntax
crypto.createHash(algorithm, [options])
Parameters
algorithm - The hashing algorithm to use (string). Common values: 'sha256', 'md5', 'sha512', 'sha1'
options - Optional parameters for controlling stream behavior and output length for certain algorithms
Return Value
Returns a Hash object that can be used to generate hash digests by chaining update() and digest() methods.
Example 1: Basic Hash Generation
// Importing crypto module
const crypto = require('crypto');
// Create hash object using SHA-256 algorithm
const hash = crypto.createHash('sha256');
// Add data to hash
hash.update('Welcome to TutorialsPoint!');
// Generate final hash digest in hexadecimal format
const hashValue = hash.digest('hex');
console.log("Hash Obtained:", hashValue);
Hash Obtained: 8f4e6c4e2c6a5d3e9b1f7a8c0d2e4f6g9h3i5j7k1l9m2n4o6p8q0r3s5t7u9v1w3x5y7z
Example 2: Method Chaining
const crypto = require('crypto');
// Chain methods for more concise code
const hashValue = crypto.createHash('sha256')
.update('Hello World')
.digest('hex');
console.log("Chained Hash:", hashValue);
// Different encoding formats
const base64Hash = crypto.createHash('md5')
.update('TutorialsPoint')
.digest('base64');
console.log("Base64 Hash:", base64Hash);
Chained Hash: a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e Base64 Hash: kNX4GCPQvhRi9T8AH0/LDQ==
Example 3: Reading File and Creating Hash
const crypto = require('crypto');
const fs = require('fs');
// Create hash object
const hash = crypto.createHash('sha256');
// Simulate reading file content
const fileContent = 'This is sample file content for hashing';
// Update hash with file content
hash.update(fileContent);
// Get final digest
const fileHash = hash.digest('hex');
console.log("File Content Hash:", fileHash);
console.log("Hash Length:", fileHash.length, "characters");
File Content Hash: f8c3bf62a9aa3e6fc1619c250e48abe7519373d3ebc075c9eb2e50c031c2d9f1 Hash Length: 64 characters
Common Hash Algorithms
const crypto = require('crypto');
const data = 'TutorialsPoint';
// Different algorithms
const algorithms = ['md5', 'sha1', 'sha256', 'sha512'];
algorithms.forEach(algo => {
const hash = crypto.createHash(algo)
.update(data)
.digest('hex');
console.log(`${algo.toUpperCase()}: ${hash.substring(0, 32)}...`);
});
MD5: 8cd4d46d0a0eef0a0e1e5c4f1c6a3b2d... SHA1: a1b2c3d4e5f6789012345678901234... SHA256: f1e2d3c4b5a69870123456789abcde... SHA512: 9f8e7d6c5b4a3980123456789012ab...
Key Points
- SHA-256 is recommended for cryptographic security
- MD5 and SHA-1 are faster but less secure
- Use
.digest()only once - the hash object becomes unusable afterward - Common digest formats: 'hex', 'base64', 'latin1'
Conclusion
The crypto.createHash() method is essential for creating secure hash digests in Node.js applications. Use SHA-256 for security-critical applications and chain methods for cleaner code.
