How to Create Hash Generators

Learn how to create hash generators in JavaScript. Step-by-step tutorial for MD5, SHA-1, SHA-256, and SHA-512 with Web Crypto API and libraries.

How to Create Hash Generators

Creating a hash generator in JavaScript is straightforward with modern APIs. This tutorial shows you how to generate hashes using the built-in Web Crypto API, popular libraries, and how to build a complete hash generator utility.

Method 1: Web Crypto API (Recommended)

Modern browsers and Node.js 19+ include the Web Crypto API:

async function hash(message, algorithm = 'SHA-256') {
  const encoder = new TextEncoder();
  const data = encoder.encode(message);
  const hashBuffer = await crypto.subtle.digest(algorithm, data);
  const hashArray = Array.from(new Uint8Array(hashBuffer));
  const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
  return hashHex;
}

// Usage const sha256 = await hash('Hello, World!', 'SHA-256'); console.log(sha256); // "a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e"

Supported Algorithms

// Available algorithms
const algorithms = ['SHA-1', 'SHA-256', 'SHA-384', 'SHA-512'];

// MD5 is NOT supported by Web Crypto API (use a library instead)

Browser Support

| Browser | Version | |---------|---------| | Chrome | 37+ | | Firefox | 34+ | | Safari | 11+ | | Edge | 79+ | | Node.js | 19+ (15+ with --experimental-global-webcrypto) |

Method 2: Using crypto-js Library

For broader algorithm support including MD5:

npm install crypto-js
const CryptoJS = require('crypto-js');

// MD5 const md5 = CryptoJS.MD5('Hello, World!').toString(); console.log(md5); // "65a8e27d8879283831b664bd8b7f0ad4"

// SHA-1 const sha1 = CryptoJS.SHA1('Hello, World!').toString();

// SHA-256 const sha256 = CryptoJS.SHA256('Hello, World!').toString();

// SHA-512 const sha512 = CryptoJS.SHA512('Hello, World!').toString();

Browser Usage

import CryptoJS from 'crypto-js';

const hash = CryptoJS.SHA256('Hello, World!').toString(); console.log(hash);

Method 3: Node.js Built-in crypto Module

For server-side JavaScript:

const crypto = require('crypto');

function hash(message, algorithm = 'sha256') { return crypto.createHash(algorithm).update(message).digest('hex'); }

// Usage console.log(hash('Hello, World!', 'md5')); console.log(hash('Hello, World!', 'sha256')); console.log(hash('Hello, World!', 'sha512'));

ES Modules

import { createHash } from 'crypto';

const hash = (message, algorithm = 'sha256') => { return createHash(algorithm).update(message).digest('hex'); };

Complete Hash Generator Class

class HashGenerator {
  // Hash using Web Crypto API (browser/Node 19+)
  static async hashWebCrypto(message, algorithm = 'SHA-256') {
    const encoder = new TextEncoder();
    const data = encoder.encode(message);
    const hashBuffer = await crypto.subtle.digest(algorithm, data);
    const hashArray = Array.from(new Uint8Array(hashBuffer));
    return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
  }

// Hash using Node.js crypto static hashNode(message, algorithm = 'sha256') { const crypto = require('crypto'); return crypto.createHash(algorithm).update(message).digest('hex'); }

// Auto-detect and use available method static async hash(message, algorithm = 'SHA-256') { // Try Web Crypto first if (typeof crypto !== 'undefined' && crypto.subtle) { return this.hashWebCrypto(message, algorithm); } // Fall back to Node.js crypto return this.hashNode(message, algorithm.toLowerCase()); }

// Generate multiple hashes at once static async hashAll(message) { const algorithms = ['SHA-1', 'SHA-256', 'SHA-384', 'SHA-512']; const results = {};

for (const algo of algorithms) { results[algo] = await this.hash(message, algo); }

return results; }

// Hash a file (browser) static async hashFile(file, algorithm = 'SHA-256') { const arrayBuffer = await file.arrayBuffer(); const hashBuffer = await crypto.subtle.digest(algorithm, arrayBuffer); const hashArray = Array.from(new Uint8Array(hashBuffer)); return hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); } }

// Usage const sha256 = await HashGenerator.hash('Hello, World!'); const allHashes = await HashGenerator.hashAll('Hello, World!');

Hashing Files

Browser File Hashing

async function hashFile(file, algorithm = 'SHA-256') {
  const arrayBuffer = await file.arrayBuffer();
  const hashBuffer = await crypto.subtle.digest(algorithm, arrayBuffer);
  const hashArray = Array.from(new Uint8Array(hashBuffer));
  return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
}

// Usage with file input const input = document.querySelector('input[type="file"]'); input.addEventListener('change', async (e) => { const file = e.target.files[0]; const hash = await hashFile(file, 'SHA-256'); console.log(SHA-256: ${hash}); });

Node.js File Hashing (Large Files)

const crypto = require('crypto');
const fs = require('fs');

function hashLargeFile(filePath, algorithm = 'sha256') { return new Promise((resolve, reject) => { const hash = crypto.createHash(algorithm); const stream = fs.createReadStream(filePath);

stream.on('data', chunk => hash.update(chunk)); stream.on('end', () => resolve(hash.digest('hex'))); stream.on('error', reject); }); }

// Usage const hash = await hashLargeFile('large-file.iso', 'sha256'); console.log(hash);

HMAC (Hash with Secret Key)

For authenticated hashing:

// Web Crypto API
async function hmac(message, secret, algorithm = 'SHA-256') {
  const encoder = new TextEncoder();
  const keyData = encoder.encode(secret);
  const messageData = encoder.encode(message);

const key = await crypto.subtle.importKey( 'raw', keyData, { name: 'HMAC', hash: algorithm }, false, ['sign'] );

const signature = await crypto.subtle.sign('HMAC', key, messageData); const hashArray = Array.from(new Uint8Array(signature)); return hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); }

// Node.js const crypto = require('crypto'); function hmacNode(message, secret, algorithm = 'sha256') { return crypto.createHmac(algorithm, secret).update(message).digest('hex'); }

Hash Comparison Table

| Algorithm | Output Length | Security | Speed | Use Case | |-----------|---------------|----------|-------|----------| | MD5 | 32 chars | ❌ Broken | Fast | Checksums only | | SHA-1 | 40 chars | ❌ Deprecated | Fast | Legacy systems | | SHA-256 | 64 chars | ✅ Secure | Fast | General purpose | | SHA-384 | 96 chars | ✅ Secure | Fast | High security | | SHA-512 | 128 chars | ✅ Secure | Fast | Maximum security |

Complete Hash Generator App




  Hash Generator


  
  

💻 Get Full Source Code + Offline Version

Download all 82 tools as standalone HTML files. No internet? No problem. One-time purchase, lifetime access, modify as you like.

🔓 Full source code included. Use anywhere, modify freely.

Try It Online

Want to generate hashes without coding? Use our Hash Generator for instant hashing with multiple algorithms and file support.

Conclusion

The Web Crypto API is the modern, built-in way to generate hashes in JavaScript. For MD5 support or older browsers, use the crypto-js library. For Node.js, the built-in crypto module works great.

For related tutorials, see How to Generate UUIDs in JavaScript for generating unique identifiers.

---

Related Tools:

🚀 Deploy Your Own Tools — Recommended Hosting

Want to self-host or build your own developer tools? These are the platforms we use:

🌐
Hostinger
Web Hosting from $2.99/mo
💧
DigitalOcean
$200 Free Credit for New Users
🔑
Namecheap
Domains from $0.99/yr

* Affiliate links — we may earn a commission at no extra cost to you.

💻 Get Full Source Code + Offline Version

Download all 82 tools as standalone HTML files. No internet? No problem. One-time purchase, lifetime access, modify as you like.

🛠️

DevKits Offline Pack

82 个开发者工具离线版

👥 50+ developers downloaded

$9 One-time Get Source →
📝

SEO Content Pack

100 篇开发者文章模板

👥 30+ developers downloaded

$19 One-time Get Source →
💎

Airdrop Hunter Guide

2026 空投狩猎完整指南

👥 25+ developers downloaded

$5 One-time Get Source →

🔓 Full source code included. Use anywhere, modify freely.