Skip to content

Commit

Permalink
Do not check for crypto.subtle when sync is used.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Jan 7, 2025
1 parent 3506e40 commit e4b9906
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
17 changes: 9 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ const verifyAsync = async (s, m, p, opts = dvo) => hashFinish(true, _verify(s, m
/** Verifies signature on message and public key. To use, set `etc.sha512Sync` first. */
const verify = (s, m, p, opts = dvo) => hashFinish(false, _verify(s, m, p, opts));
const cr = () => // We support: 1) browsers 2) node.js 19+
typeof globalThis === 'object' && 'crypto' in globalThis && 'subtle' in globalThis.crypto ? globalThis.crypto : undefined;
typeof globalThis === 'object' && 'crypto' in globalThis ? globalThis.crypto : undefined;
/** Math, hex, byte helpers. Not in `utils` because utils share API with noble-curves. */
const etc = {
bytesToHex: b2h,
Expand All @@ -343,19 +343,20 @@ const etc = {
mod: M,
invert: invert,
randomBytes: (len = 32) => {
const crypto = cr(); // Can be shimmed in node.js <= 18 to prevent error:
const c = cr(); // Can be shimmed in node.js <= 18 to prevent error:
// import { webcrypto } from 'node:crypto';
// if (!globalThis.crypto) globalThis.crypto = webcrypto;
if (!crypto || !crypto.getRandomValues)
if (!c || !c.getRandomValues)
err('crypto.getRandomValues must be defined');
return crypto.getRandomValues(u8n(len));
return c.getRandomValues(u8n(len));
},
sha512Async: async (...messages) => {
const crypto = cr();
if (!crypto || !crypto.subtle)
err('crypto.subtle or etc.sha512Async must be defined');
const c = cr();
const s = c && c.subtle;
if (!s)
err('etc.sha512Async or crypto.subtle must be defined');
const m = concatB(...messages);
return u8n(await crypto.subtle.digest('SHA-512', m.buffer));
return u8n(await s.digest('SHA-512', m.buffer));
},
sha512Sync: undefined, // Actual logic below
};
Expand Down
15 changes: 8 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ const verify = (s: Hex, m: Hex, p: Hex, opts: DVO = dvo): boolean =>
hashFinish(false, _verify(s, m, p, opts));
declare const globalThis: Record<string, any> | undefined; // Typescript symbol present in browsers
const cr = () => // We support: 1) browsers 2) node.js 19+
typeof globalThis === 'object' && 'crypto' in globalThis && 'subtle' in globalThis.crypto ? globalThis.crypto : undefined;
typeof globalThis === 'object' && 'crypto' in globalThis ? globalThis.crypto : undefined;
/** Math, hex, byte helpers. Not in `utils` because utils share API with noble-curves. */
const etc = {
bytesToHex: b2h satisfies (b: Bytes) => string as (b: Bytes) => string,
Expand All @@ -314,17 +314,18 @@ const etc = {
mod: M satisfies (a: bigint, b?: bigint) => bigint as (a: bigint, b?: bigint) => bigint,
invert: invert as (num: bigint, md: bigint) => bigint,
randomBytes: (len = 32): Bytes => { // CSPRNG (random number generator)
const crypto = cr(); // Can be shimmed in node.js <= 18 to prevent error:
const c = cr(); // Can be shimmed in node.js <= 18 to prevent error:
// import { webcrypto } from 'node:crypto';
// if (!globalThis.crypto) globalThis.crypto = webcrypto;
if (!crypto || !crypto.getRandomValues) err('crypto.getRandomValues must be defined');
return crypto.getRandomValues(u8n(len));
if (!c || !c.getRandomValues) err('crypto.getRandomValues must be defined');
return c.getRandomValues(u8n(len));
},
sha512Async: async (...messages: Bytes[]): Promise<Bytes> => {
const crypto = cr();
if (!crypto || !crypto.subtle) err('crypto.subtle or etc.sha512Async must be defined');
const c = cr();
const s = c && c.subtle
if (!s) err('etc.sha512Async or crypto.subtle must be defined');
const m = concatB(...messages);
return u8n(await crypto.subtle.digest('SHA-512', m.buffer));
return u8n(await s.digest('SHA-512', m.buffer));
},
sha512Sync: undefined as Sha512FnSync, // Actual logic below
};
Expand Down

0 comments on commit e4b9906

Please sign in to comment.