Code Guide

How to Generate MD5/SHA-256 Hashes in JavaScript, Python, and the Command Line

๐Ÿ“… September 2026โฑ๏ธ 5 min read
Every modern language and OS has built-in hashing support. Here's the copy-paste snippet for generating MD5 and SHA-256 digests in browser JavaScript, Node.js, Python, and the command line on Linux, Mac, and Windows.

JavaScript (Browser)

Browsers expose the Web Crypto SubtleCrypto API for SHA-family hashes. It's asynchronous (returns a Promise) and does not support MD5:

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

sha256('hello world').then(hash => console.log(hash));
// b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde

Swap 'SHA-256' for 'SHA-1' or 'SHA-512' to compute other SHA-family digests โ€” the algorithm name is the only thing that changes. Because SubtleCrypto has no MD5 support, browser MD5 requires a small pure-JS implementation (or a library like blueimp-md5/ crypto-js).

Node.js

Node's built-in crypto module supports MD5, SHA-1, SHA-256, SHA-512, and more, synchronously โ€” no external package required:

const crypto = require('crypto');

const sha256Hash = crypto.createHash('sha256').update('hello world').digest('hex');
console.log(sha256Hash);
// b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde

const md5Hash = crypto.createHash('md5').update('hello world').digest('hex');
console.log(md5Hash);
// 5eb63bbbe01eeed093cb22bb8f5acdc3

Python

Python's standard library hashlib module supports all common hash algorithms out of the box:

import hashlib

sha256_hash = hashlib.sha256("hello world".encode()).hexdigest()
print(sha256_hash)
# b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde

md5_hash = hashlib.md5("hello world".encode()).hexdigest()
print(md5_hash)
# 5eb63bbbe01eeed093cb22bb8f5acdc3

sha512_hash = hashlib.sha512("hello world".encode()).hexdigest()
print(sha512_hash)

Always call .encode() first โ€” hashlib hashes bytes, not Python strings, and this also controls the character encoding (UTF-8 by default) used before hashing.

Command Line

Linux

# Hash a file
md5sum file.txt
sha256sum file.txt

# Hash a string piped in
echo -n "hello world" | md5sum
echo -n "hello world" | sha256sum

macOS

# macOS uses shasum instead of sha256sum
shasum -a 256 file.txt
md5 file.txt

# Hash a string piped in
echo -n "hello world" | shasum -a 256
echo -n "hello world" | md5

Windows

:: Built-in, no install needed
certutil -hashfile file.txt MD5
certutil -hashfile file.txt SHA256

:: PowerShell alternative
Get-FileHash file.txt -Algorithm SHA256
๐Ÿ’ก Just need one hash right now?

If you just need to quickly check a hash without opening a terminal or editor, skip the code โ€” paste your text into the browser tool instead and get MD5, SHA-1, SHA-256, and SHA-512 all at once.

Generate a Hash Now

Compute MD5, SHA-1, SHA-256, and SHA-512 for any text, free and instant, no code required.

Open Hash Generator โ†’

Frequently Asked Questions

Why does the browser's SubtleCrypto API not support MD5?
The Web Crypto API standard deliberately omits MD5 because it's cryptographically broken and the spec authors didn't want to encourage its use for security purposes. If you need MD5 in the browser (e.g. for legacy checksum compatibility, not security), you'll need a small JavaScript implementation or a library โ€” the native API only covers SHA-1/SHA-256/SHA-384/SHA-512.
Why do Node.js and Python give a different hash than the browser for the same string?
Usually this comes down to text encoding, not the algorithm. Hash functions operate on bytes, not characters, so if one environment encodes a string as UTF-8 and another uses UTF-16 or Latin-1, you'll get a different byte sequence and therefore a different hash. Make sure every environment explicitly encodes as UTF-8 before hashing (TextEncoder in JS, .encode() in Python, which defaults to UTF-8) to get matching results.
Can I hash a file directly instead of a string?
Yes โ€” all of the CLI examples above (md5sum, sha256sum, shasum, certutil, Get-FileHash) hash files directly. In Node.js and Python, read the file as a buffer/bytes object first (fs.readFileSync in Node, open(path,'rb').read() in Python) and pass that to the hash function instead of a string.