]> git.ardis.lu - cloudflare-workers/commitdiff
Allow directly hashing hex byte array
authorArdis Lu <ardisglu@gmail.com>
Sun, 31 Mar 2024 08:08:05 +0000 (01:08 -0700)
committerArdis Lu <ardisglu@gmail.com>
Sun, 31 Mar 2024 08:08:05 +0000 (01:08 -0700)
README.md
hash/index.js

index b994ae3dae6e1ba0f3b218e71add09ca7b41a0f1..450d77d4b8d75d0a5c8322df5178d98197ff4f4b 100644 (file)
--- a/README.md
+++ b/README.md
@@ -238,21 +238,31 @@ PS> (Invoke-WebRequest https://x.y.workers.dev).Content
 
 ## hash
 
-Returns the hash of a string using the
+Returns the hash of a string or a hexadecimal byte array using the
 [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest).
 
 In bash:
 
 ```bash
-$ curl https://x.y.workers.dev?algorithm=SHA-256&query=Hello%2C%20world%21
+# SHA-256 hash of the string "Hello, world!"
+$ curl "https://x.y.workers.dev?algorithm=SHA-256&query=Hello%2C%20world%21"
 315F5BDB76D078C43B8AC0064E4A0164612B1FCE77C869345BFC94C75894EDD3
+
+# MD5 hash of the literal byte array [00, AA, BB, CC]
+$ curl "https://x.y.workers.dev?algorithm=MD5&hex=00AABBCC"
+CD7409B51FDE80807E2E1532B7432A00
 ```
 
 In PowerShell:
 
 ```powershell
-PS> (Invoke-WebRequest https://x.y.workers.dev?algorithm=SHA-256&query=Hello%2C%20world%21).Content
+# SHA-256 hash of the string "Hello, world!"
+PS> (Invoke-WebRequest "https://x.y.workers.dev?algorithm=SHA-256&query=Hello%2C%20world%21").Content
 315F5BDB76D078C43B8AC0064E4A0164612B1FCE77C869345BFC94C75894EDD3
+
+# MD5 hash of the literal byte array [00, AA, BB, CC]
+PS> (Invoke-WebRequest "https://x.y.workers.dev?algorithm=MD5&hex=00AABBCC").Content
+CD7409B51FDE80807E2E1532B7432A00
 ```
 
 ### `algorithm` (alias `a`)
@@ -268,3 +278,10 @@ Supported values are:
 ### `query` (alias `q`)
 
 The string you want to get the hash of.
+
+### `hex` (alias `h`)
+
+The hexadecimal byte array you want to get the hash of. An optional "0x" prefix
+may be given, the prefix is ignored.
+
+If both `query` and `hex` are provided, `query` will be used.
index 1fa697bbcb8a26871f390f145f925732fe5090ac..7fb5390a775ce04fe341fa0048116c3ddc479c9b 100644 (file)
@@ -3,6 +3,7 @@ export default {
     const url = new URL(request.url);
     let algorithm;
     let query;
+    let hex;
 
     // Parse URLSearchParams
     for (const [key, value] of url.searchParams) {
@@ -18,18 +19,31 @@ export default {
       else if ((normalizedKey === 'q' || normalizedKey === 'query') && value) { // Intentionally fail for falsy values because a null or empty string is also invalid
         query = value;
       }
+      else if ((normalizedKey === 'h' || normalizedKey === 'hex') && value) {
+        hex = value;
+      }
     }
 
     // Handling bad params
     if (algorithm === undefined) {
       return new Response('Invalid algorithm provided. Provide one of the following values for the parameter "a" or "algorithm": MD5, SHA-1, SHA-256, SHA-384, or SHA-512.', { status: 400 });
     }
-    else if (query === undefined) {
-      return new Response('Invalid query provided. Provide a value for the parameter "q" or "query".', { status: 400 });
+    if (query === undefined && hex === undefined) {
+      return new Response('No input provided. Provide a string value for the parameter "q" or "query", or a hex value for the parameter "h" or "hex".', { status: 400 });
+    }
+    if (hex !== undefined) {
+      if (hex.length % 2 === 1 || !/^(0x)?[a-fA-F0-9]+$/.test(hex)) {
+        return new Response('Invalid hexadecimal input provided.', { status: 400 });
+      }
     }
 
-    // Convert query string to byte array and hash it, then convert hashed byte array to hex string
-    const inputArray = new TextEncoder().encode(query);
+    let inputArray;
+    if (query) {
+      inputArray = new TextEncoder().encode(query);
+    }
+    else {
+      inputArray = Uint8Array.from(hex.replace('0x', '').match(/.{2}/g), v => parseInt(v, 16));
+    }
     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();
This page took 0.041828 seconds and 4 git commands to generate.