]> git.ardis.lu - cloudflare-workers/commitdiff
Add MD5 to hash worker
authorArdis Lu <ardisglu@gmail.com>
Thu, 6 Jul 2023 20:29:54 +0000 (13:29 -0700)
committerArdis Lu <ardisglu@gmail.com>
Thu, 6 Jul 2023 20:29:54 +0000 (13:29 -0700)
README.md
hash/index.js

index ced9e0d6d7440d7702c86929e9227bf9be5a1635..3e644308775476f79ba61733f4e92c61664c8123 100644 (file)
--- a/README.md
+++ b/README.md
@@ -253,6 +253,7 @@ PS> (Invoke-WebRequest https://x.y.workers.dev?algorithm=SHA-256&query=Hello%2C%
 
 Supported values are:
 
+- `MD5` (not part of the Web Crypto API but supported by Cloudflare Workers)
 - `SHA-1`
 - `SHA-256`
 - `SHA-384`
index 78b241f5bb5c7267792a3712fcf536fa8ceb5030..1fa697bbcb8a26871f390f145f925732fe5090ac 100644 (file)
@@ -7,8 +7,13 @@ export default {
     // Parse URLSearchParams
     for (const [key, value] of url.searchParams) {
       const normalizedKey = key.toLowerCase();
-      if ((normalizedKey === 'a' || normalizedKey === 'algorithm') && /^SHA-?(1|256|384|512)$/i.test(value)) {
-        algorithm = value.includes('-') ? value : `SHA-${value.slice(3)}`; // Insert a '-' character if it's not present; crypto.subtle.digest() requires it.
+      if ((normalizedKey === 'a' || normalizedKey === 'algorithm')) {
+        if (/^SHA-?(1|256|384|512)$/i.test(value)) {
+          algorithm = value.includes('-') ? value : `SHA-${value.slice(3)}`; // Insert a '-' character if it's not present; crypto.subtle.digest() requires it.
+        }
+        else if (value.toUpperCase() === 'MD5') {
+          algorithm = 'MD5';
+        }
       }
       else if ((normalizedKey === 'q' || normalizedKey === 'query') && value) { // Intentionally fail for falsy values because a null or empty string is also invalid
         query = value;
@@ -17,7 +22,7 @@ export default {
 
     // Handling bad params
     if (algorithm === undefined) {
-      return new Response('Invalid algorithm provided. Provide one of the following values for the parameter "a" or "algorithm": SHA-1, SHA-256, SHA-384, or SHA-512.', { status: 400 });
+      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 });
This page took 0.040603 seconds and 4 git commands to generate.