3
3
* @module
4
4
*/
5
5
/*! 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' ;
14
6
import { abytes } from './_assert.ts' ;
15
7
// export { isBytes } from './_assert.ts';
16
8
// We can't reuse isBytes from _assert, because somehow this causes huge perf issues
@@ -154,6 +146,7 @@ export async function asyncLoop(
154
146
// Global symbols in both browsers and Node.js since v11
155
147
// See https://github.com/microsoft/TypeScript/issues/31535
156
148
declare const TextEncoder : any ;
149
+ declare const TextDecoder : any ;
157
150
158
151
/**
159
152
* Convert JS string to byte array.
@@ -164,15 +157,24 @@ export function utf8ToBytes(str: string): Uint8Array {
164
157
return new Uint8Array ( new TextEncoder ( ) . encode ( str ) ) ; // https://bugzil.la/1681809
165
158
}
166
159
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
+
167
169
/** Accepted input of hash functions. Strings are converted to byte arrays. */
168
170
export type Input = Uint8Array | string ;
169
171
/**
170
172
* Normalizes (non-hex) string or Uint8Array to Uint8Array.
171
173
* Warning: when Uint8Array is passed, it would NOT get copied.
172
174
* Keep in mind for future mutable operations.
173
175
*/
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);
176
178
abytes ( data ) ;
177
179
return data ;
178
180
}
@@ -302,14 +304,15 @@ export function wrapXOFConstructorWithOpts<H extends HashXOF<H>, T extends Objec
302
304
return hashC ;
303
305
}
304
306
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.
305
311
/** Cryptographically secure PRNG. Uses internal OS-level `crypto.getRandomValues`. */
306
312
export function randomBytes ( bytesLength = 32 ) : Uint8Array {
313
+ const crypto = typeof globalThis === 'object' && 'crypto' in globalThis && globalThis . crypto ;
307
314
if ( crypto && typeof crypto . getRandomValues === 'function' ) {
308
315
return crypto . getRandomValues ( new Uint8Array ( bytesLength ) ) ;
309
316
}
310
- // Legacy Node.js compatibility
311
- if ( crypto && typeof crypto . randomBytes === 'function' ) {
312
- return crypto . randomBytes ( bytesLength ) ;
313
- }
314
317
throw new Error ( 'crypto.getRandomValues must be defined' ) ;
315
318
}
0 commit comments