/** * Ed25519 and SHA-256 on every runtime (RFC-0046 ยง11a): WebCrypto for Ed25519 and one-shot * digests; for the streamed tarball, Workers' native `crypto.DigestStream` where it exists and * otherwise `@noble/hashes` (audited, zero dependencies), because WebCrypto's `digest` cannot * stream. test/crypto.test.ts checks both paths against the NIST vectors and `node:crypto`. */ import { sha256 as nobleSha256 } from "@noble/hashes/sha2.js"; const toHex = (bytes: Uint8Array): string => { let s = ""; for (const b of bytes) s += b.toString(16).padStart(2, "0"); return s; }; /** * `bytes` as WebCrypto's `BufferSource` under every consumer's lib settings: the same view when it * is backed by an `ArrayBuffer`, else a copy (a `SharedArrayBuffer` is never handed to WebCrypto). */ function buffer(bytes: Uint8Array): Uint8Array { return bytes.buffer instanceof ArrayBuffer ? (bytes as Uint8Array) : new Uint8Array(bytes); } /** Pure Ed25519 over the exact `message` bytes. */ export async function verifyEd25519( publicKey: Uint8Array, message: Uint8Array, signature: Uint8Array, ): Promise { try { const key = await crypto.subtle.importKey( "raw", buffer(publicKey), { name: "Ed25519" }, false, ["verify"], ); return await crypto.subtle.verify({ name: "Ed25519" }, key, buffer(signature), buffer(message)); } catch { // A key that is not a valid Ed25519 point verifies nothing. return false; } } /** SHA-256 of `bytes`, lowercase hex. */ export async function sha256Hex(bytes: Uint8Array): Promise { return toHex(new Uint8Array(await crypto.subtle.digest("SHA-256", buffer(bytes)))); } export interface IncrementalSha256 { update(chunk: Uint8Array): Promise; /** Lowercase hex; call once, after the last `update`. */ digest(): Promise; } /** Workers' `crypto.DigestStream` (a WritableStream whose `digest` resolves on close). */ export type DigestStreamConstructor = new ( algorithm: string, ) => WritableStream & { readonly digest: Promise }; const nativeDigestStream = (): DigestStreamConstructor | undefined => (globalThis as { crypto?: { DigestStream?: DigestStreamConstructor } }).crypto?.DigestStream; /** An incremental SHA-256: `crypto.DigestStream` when the runtime has one, else @noble/hashes. */ export function incrementalSha256( DigestStream: DigestStreamConstructor | undefined = nativeDigestStream(), ): IncrementalSha256 { if (DigestStream) { const stream = new DigestStream("SHA-256"); const writer = stream.getWriter(); return { update: (chunk) => writer.write(chunk), async digest() { await writer.close(); return toHex(new Uint8Array(await stream.digest)); }, }; } const h = nobleSha256.create(); return { update: async (chunk) => { h.update(chunk); }, digest: async () => toHex(h.digest()), }; }