(Invoke-WebRequest https://x.y.workers.dev).Content
> <random v4 UUID>
```
+
+## hash
+
+Returns the hash of a string using the [crypto web API](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest).
+
+### Usage
+
+In bash:
+```
+curl https://x.y.workers.dev?algorithm=SHA-256&query=Hello%2C%20world%21
+> 315F5BDB76D078C43B8AC0064E4A0164612B1FCE77C869345BFC94C75894EDD3
+```
+
+In PowerShell:
+```
+(Invoke-WebRequest https://x.y.workers.dev?algorithm=SHA-256&query=Hello%2C%20world%21).Content
+> 315F5BDB76D078C43B8AC0064E4A0164612B1FCE77C869345BFC94C75894EDD3
+```
+
+### `algorithm`
+
+Supported values are:
+- `SHA-1`
+- `SHA-256`
+- `SHA-384`
+- `SHA-512`
+
+### `query` (alias `q`)
+
+The string you want to get the hash of.
--- /dev/null
+async function handleRequest(request) {
+ const url = new URL(request.url);
+ let algorithm = 'SHA-256';
+ let query = 'Example string';
+ for (const [key, value] of url.searchParams) {
+ if (key.toLowerCase() === 'algorithm' && ['SHA-1', 'SHA-256', 'SHA-384', 'SHA-512'].includes(value.toUpperCase())) {
+ algorithm = value;
+ }
+ else if ((key.toLowerCase() === 'q' || key.toLowerCase() === 'query') && value) { // Intentionally excluding falsy values and not just null because empty strings are no good
+ query = value;
+ }
+ }
+
+ // Convert query string to byte array and hash it, then convert hashed byte array to hex string
+ const inputArray = new TextEncoder().encode(query);
+ const digest = await crypto.subtle.digest(algorithm, inputArray);
+ const outputArray = [...new Uint8Array(digest)]; // Need to spread typed array into a non-typed array to allow .map() to output strings
+ const hash = outputArray.map(v => v.toString(16).padStart(2, '0')).join('').toUpperCase();
+
+ return new Response(hash);
+}
+
+addEventListener('fetch', event => {
+ event.respondWith(handleRequest(event.request));
+})
--- /dev/null
+{
+ "name": "hash",
+ "version": "0.1.0",
+ "description": "Return the hash of the input query.",
+ "private": true,
+ "main": "index.js",
+ "scripts": {
+ "login": "wrangler login",
+ "dev": "wrangler dev",
+ "preview": "wrangler preview",
+ "build": "wrangler build",
+ "publish": "wrangler publish"
+ },
+ "author": "Ardis Lu",
+ "license": "ISC",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/ardislu/cloudflare-workers.git"
+ }
+}