Skip to content

Commit 1250016

Browse files
committed
v2: init
1 parent 380de41 commit 1250016

File tree

4 files changed

+18
-39
lines changed

4 files changed

+18
-39
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"typescript": "5.8.2"
4343
},
4444
"engines": {
45-
"node": "^14.21.3 || >=16"
45+
"node": "^18.20.7"
4646
},
4747
"exports": {
4848
".": {

src/crypto.ts

-9
This file was deleted.

src/cryptoNode.ts

-15
This file was deleted.

src/utils.ts

+17-14
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@
33
* @module
44
*/
55
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
6-
7-
// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
8-
// node.js versions earlier than v19 don't declare it in global scope.
9-
// For node.js, package.json#exports field mapping rewrites import
10-
// from `crypto` to `cryptoNode`, which imports native module.
11-
// Makes the utils un-importable in browsers without a bundler.
12-
// Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
13-
import { crypto } from '@noble/hashes/crypto';
146
import { abytes } from './_assert.ts';
157
// export { isBytes } from './_assert.ts';
168
// We can't reuse isBytes from _assert, because somehow this causes huge perf issues
@@ -154,6 +146,7 @@ export async function asyncLoop(
154146
// Global symbols in both browsers and Node.js since v11
155147
// See https://github.com/microsoft/TypeScript/issues/31535
156148
declare const TextEncoder: any;
149+
declare const TextDecoder: any;
157150

158151
/**
159152
* Convert JS string to byte array.
@@ -164,15 +157,24 @@ export function utf8ToBytes(str: string): Uint8Array {
164157
return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
165158
}
166159

160+
/**
161+
* Convert JS byte array to string.
162+
* @example bytesToUtf8(new Uint8Array([97, 98, 99])) // 'abc'
163+
*/
164+
export function bytesToUtf8(bytes: Uint8Array): string {
165+
abytes(bytes);
166+
return new TextDecoder().decode(bytes);
167+
}
168+
167169
/** Accepted input of hash functions. Strings are converted to byte arrays. */
168170
export type Input = Uint8Array | string;
169171
/**
170172
* Normalizes (non-hex) string or Uint8Array to Uint8Array.
171173
* Warning: when Uint8Array is passed, it would NOT get copied.
172174
* Keep in mind for future mutable operations.
173175
*/
174-
export function toBytes(data: Input): Uint8Array {
175-
if (typeof data === 'string') data = utf8ToBytes(data);
176+
export function toBytes(data: Uint8Array): Uint8Array {
177+
// if (typeof data === 'string') data = utf8ToBytes(data);
176178
abytes(data);
177179
return data;
178180
}
@@ -302,14 +304,15 @@ export function wrapXOFConstructorWithOpts<H extends HashXOF<H>, T extends Objec
302304
return hashC;
303305
}
304306

307+
declare const globalThis: any;
308+
309+
// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
310+
// node.js versions earlier than v19 don't declare it in global scope.
305311
/** Cryptographically secure PRNG. Uses internal OS-level `crypto.getRandomValues`. */
306312
export function randomBytes(bytesLength = 32): Uint8Array {
313+
const crypto = typeof globalThis === 'object' && 'crypto' in globalThis && globalThis.crypto;
307314
if (crypto && typeof crypto.getRandomValues === 'function') {
308315
return crypto.getRandomValues(new Uint8Array(bytesLength));
309316
}
310-
// Legacy Node.js compatibility
311-
if (crypto && typeof crypto.randomBytes === 'function') {
312-
return crypto.randomBytes(bytesLength);
313-
}
314317
throw new Error('crypto.getRandomValues must be defined');
315318
}

0 commit comments

Comments
 (0)