crypto.getHashes() Method in Node.js

The crypto.getHashes() method returns an array containing the names of all supported hash algorithms available in Node.js. This is useful for discovering what hashing options are available in your current Node.js environment.

Syntax

crypto.getHashes()

Parameters

This method takes no parameters and returns an array of supported hash algorithm names.

Return Value

Returns an array of strings, where each string is the name of a supported hash algorithm.

Example

// Importing the crypto module
const crypto = require('crypto');

// Get all supported hash algorithms
const hashAlgorithms = crypto.getHashes();

// Display the first few algorithms
console.log('Total hash algorithms available:', hashAlgorithms.length);
console.log('First 10 algorithms:');
hashAlgorithms.slice(0, 10).forEach((algorithm, index) => {
    console.log(`${index + 1}. ${algorithm}`);
});

// Check if specific algorithms are available
console.log('\nChecking for common algorithms:');
console.log('SHA256 available:', hashAlgorithms.includes('sha256'));
console.log('MD5 available:', hashAlgorithms.includes('md5'));
console.log('SHA512 available:', hashAlgorithms.includes('sha512'));
Total hash algorithms available: 55
First 10 algorithms:
1. RSA-MD4
2. RSA-MD5
3. RSA-RIPEMD160
4. RSA-SHA1
5. RSA-SHA1-2
6. RSA-SHA224
7. RSA-SHA256
8. RSA-SHA3-224
9. RSA-SHA3-256
10. RSA-SHA3-384

Checking for common algorithms:
SHA256 available: true
MD5 available: true
SHA512 available: true

Common Hash Algorithms

Some of the most commonly used hash algorithms include:

Algorithm Output Size Security Level Common Use
MD5 128-bit Weak File integrity (legacy)
SHA1 160-bit Weak Git commits (legacy)
SHA256 256-bit Strong Modern applications
SHA512 512-bit Very Strong High-security applications

Practical Usage

const crypto = require('crypto');

// Get available algorithms and filter for SHA variants
const algorithms = crypto.getHashes();
const shaAlgorithms = algorithms.filter(algo => algo.includes('sha'));

console.log('Available SHA algorithms:');
shaAlgorithms.forEach(algo => console.log(`- ${algo}`));

// Use one of the available algorithms to create a hash
const data = 'Hello, World!';
const hash = crypto.createHash('sha256').update(data).digest('hex');
console.log(`\nSHA256 hash of "${data}":`, hash);
Available SHA algorithms:
- sha1
- sha224
- sha256
- sha3-224
- sha3-256
- sha3-384
- sha3-512
- sha384
- sha512
- sha512-224
- sha512-256

SHA256 hash of "Hello, World!": dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f

Conclusion

The crypto.getHashes() method is essential for discovering available hash algorithms in your Node.js environment. Use SHA256 or SHA512 for modern applications, avoiding older algorithms like MD5 and SHA1 for security-sensitive operations.

Updated on: 2026-03-15T23:19:00+05:30

295 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements