]> git.ardis.lu - cloudflare-workers/commitdiff
Add hash worker
authorArdis Lu <ardisglu@gmail.com>
Wed, 6 Oct 2021 05:43:14 +0000 (22:43 -0700)
committerArdis Lu <ardisglu@gmail.com>
Wed, 6 Oct 2021 06:55:49 +0000 (23:55 -0700)
README.md
hash/index.js [new file with mode: 0644]
hash/package.json [new file with mode: 0644]
hash/wrangler.toml.example [new file with mode: 0644]
package.json

index c8065c96b9f193822c9842c022879164e8b126ee..d6e8793eed0470fafab39a450a18a31a1fef8dfd 100644 (file)
--- a/README.md
+++ b/README.md
@@ -153,3 +153,33 @@ In PowerShell:
 (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.
diff --git a/hash/index.js b/hash/index.js
new file mode 100644 (file)
index 0000000..48e1cb9
--- /dev/null
@@ -0,0 +1,25 @@
+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));
+})
diff --git a/hash/package.json b/hash/package.json
new file mode 100644 (file)
index 0000000..e7780d2
--- /dev/null
@@ -0,0 +1,20 @@
+{
+  "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"
+  }
+}
diff --git a/hash/wrangler.toml.example b/hash/wrangler.toml.example
new file mode 100644 (file)
index 0000000..ad03e5e
--- /dev/null
@@ -0,0 +1,5 @@
+name = 'hash'
+type = 'javascript'
+account_id = ''
+workers_dev = true
+route = 'example.com/*'
index 743ed60ffd0707b5ff619ff7244efbf50bc1b43b..a9dadcfcb621c9147bdf99d27ca7868964c59686 100644 (file)
@@ -26,6 +26,7 @@
     "pwned",
     "qr",
     "ip",
-    "uuid"
+    "uuid",
+    "hash"
   ]
 }
This page took 0.050818 seconds and 4 git commands to generate.