From d040480f2ceffccdc560c8385b080b3d5ac1b81b Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Tue, 5 Oct 2021 22:43:14 -0700 Subject: [PATCH] Add hash worker --- README.md | 30 ++++++++++++++++++++++++++++++ hash/index.js | 25 +++++++++++++++++++++++++ hash/package.json | 20 ++++++++++++++++++++ hash/wrangler.toml.example | 5 +++++ package.json | 3 ++- 5 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 hash/index.js create mode 100644 hash/package.json create mode 100644 hash/wrangler.toml.example diff --git a/README.md b/README.md index c8065c9..d6e8793 100644 --- a/README.md +++ b/README.md @@ -153,3 +153,33 @@ In PowerShell: (Invoke-WebRequest https://x.y.workers.dev).Content > ``` + +## 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 index 0000000..48e1cb9 --- /dev/null +++ b/hash/index.js @@ -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 index 0000000..e7780d2 --- /dev/null +++ b/hash/package.json @@ -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 index 0000000..ad03e5e --- /dev/null +++ b/hash/wrangler.toml.example @@ -0,0 +1,5 @@ +name = 'hash' +type = 'javascript' +account_id = '' +workers_dev = true +route = 'example.com/*' diff --git a/package.json b/package.json index 743ed60..a9dadcf 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "pwned", "qr", "ip", - "uuid" + "uuid", + "hash" ] } -- 2.47.3