Edge Platform
Crypto Hash
Compute SHA-256/384/512 digests with the Web Crypto API on Cloudflare Workers - pasteable edge hashing utility
Compute SHA-256, SHA-384, or SHA-512 digests for any text input using the Web Crypto API in Workers.
API Reference
GET /hash
Returns a hex-encoded hash for the provided text.
Prop
Type
Example Request
curl "https://your-worker.workers.dev/hash?text=hello&algorithm=SHA-256"Success Response
algorithm string
The algorithm used for the digest
hash string
Lowercase hex-encoded digest
inputLength number
Length of the input text in characters
{
"algorithm": "SHA-256",
"hash": "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824",
"inputLength": 5
}Error Response
{
"error": "Missing or invalid query parameter: text",
"code": "INVALID_TEXT"
}Error Codes
400- Missing or invalidtext(INVALID_TEXT)400- Unsupportedalgorithm(INVALID_ALGORITHM)
Use Cases
- Hash strings at the edge without shipping crypto libraries
- Verify content fingerprints in API workflows
- Learn Web Crypto (
crypto.subtle.digest) in Workers - Compare digest sizes across SHA-256, SHA-384, and SHA-512
Limitations
- Hashes plain text only; no file upload or streaming input
- No HMAC, salting, or keyed hashing in this experiment
- Input size is limited by Worker memory and request limits
Use in your project
Copy these files into an existing Worker. Prefer Deployment to try the full experiment first. Source: apps/experiments/crypto-hash.
DependenciesNoneBindingsNonePlatformWeb Crypto (`crypto.subtle`)
import { DEFAULT_ALGORITHM, MAX_TEXT_LENGTH, SUPPORTED_ALGORITHMS, type HashAlgorithm,} from "../constants/algorithms";export function validateText(input: string | undefined): string | null { if (!input || typeof input !== "string") return null; const trimmed = input.trim(); if (!trimmed) return null; if (trimmed.length > MAX_TEXT_LENGTH) return null; return trimmed;}export function normalizeAlgorithm(input: string | undefined): HashAlgorithm | null { const raw = (input ?? DEFAULT_ALGORITHM).trim().toUpperCase().replace(/_/g, "-"); const normalized = raw.startsWith("SHA") && !raw.includes("-") ? raw.replace("SHA", "SHA-") : raw; if (SUPPORTED_ALGORITHMS.includes(normalized as HashAlgorithm)) { return normalized as HashAlgorithm; } return null;}export async function hashText(text: string, algorithm: HashAlgorithm): Promise<string> { const data = new TextEncoder().encode(text); const digest = await crypto.subtle.digest(algorithm, data); return bufferToHex(digest);}function bufferToHex(buffer: ArrayBuffer): string { return [...new Uint8Array(buffer)].map((byte) => byte.toString(16).padStart(2, "0")).join("");}Deployment
Deploy
No additional configuration required.
Test your deployment
curl "https://your-worker.workers.dev/hash?text=hello"Local Development
cd apps/experiments/crypto-hash
npm install
npm run devTest locally:
curl "http://localhost:8787/hash?text=hello"Cloudflare Features Used
- Workers - Edge compute runtime
- Web Crypto API -
crypto.subtle.digestfor SHA hashing