PeculiarVentures / webcrypto

A WebCrypto Polyfill for NodeJS
MIT License
185 stars 23 forks source link

Is there a way to bundle only subtle.digest() "SHA-1" support? #74

Closed guest271314 closed 6 months ago

guest271314 commented 6 months ago

I'm implementing a JavaScript runtime agnostic WebSocket server. txiki.js does not support subtle. The only capability I need is Web Cryptography API subtle for digest() function, e.g.,

// https://stackoverflow.com/a/77398427
async function digest(message, algo = "SHA-1") {
  const { promise, resolve } = Promise.withResolvers();
  const reader = new FileReader();
  reader.onload = () => resolve(reader.result);
  reader.readAsDataURL(
    new Blob([
      new Uint8Array(
        await crypto.subtle.digest(
          algo,
          new TextEncoder().encode(
            `${message}258EAFA5-E914-47DA-95CA-C5AB0DC85B11`,
          ),
        ),
      ),
    ]),
  );
  const result = await promise;
  return result.split(",").pop();
}

I've fetched the repository and tried to bundle with

bun build ./src/index.ts --outfile=webcrypto.js --target=browser 

yet have to substitute every Buffer import for

import * as Buffer from "node:buffer";

where I'm not going to be using Node.js' Buffer at all, rather I'll be using Uint8Array and/or ArrayBuffer and DataView.

guest271314 commented 6 months ago

This works https://github.com/kawanet/sha1-uint8array.