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'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'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.
# 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 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
:: Built-in, no install needed
certutil -hashfile file.txt MD5
certutil -hashfile file.txt SHA256
:: PowerShell alternative
Get-FileHash file.txt -Algorithm SHA256
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.
Compute MD5, SHA-1, SHA-256, and SHA-512 for any text, free and instant, no code required.
Open Hash Generator โ