rng-with-intention 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/crypto-polyfill.js +14 -8
package/package.json
CHANGED
package/src/crypto-polyfill.js
CHANGED
|
@@ -8,20 +8,25 @@ const isNode = typeof process !== 'undefined' &&
|
|
|
8
8
|
process.versions != null &&
|
|
9
9
|
process.versions.node != null;
|
|
10
10
|
|
|
11
|
+
// Lazy-load Node crypto only when needed
|
|
11
12
|
let nodeCrypto = null;
|
|
12
|
-
|
|
13
|
-
nodeCrypto
|
|
13
|
+
async function getNodeCrypto() {
|
|
14
|
+
if (!nodeCrypto && isNode) {
|
|
15
|
+
nodeCrypto = await import('crypto');
|
|
16
|
+
}
|
|
17
|
+
return nodeCrypto;
|
|
14
18
|
}
|
|
15
19
|
|
|
16
20
|
/**
|
|
17
21
|
* Get random bytes - works in both Node.js and browser
|
|
18
22
|
* @param {number} size - Number of bytes to generate
|
|
19
|
-
* @returns {Uint8Array} Random bytes
|
|
23
|
+
* @returns {Promise<Uint8Array>} Random bytes
|
|
20
24
|
*/
|
|
21
|
-
export function randomBytes(size) {
|
|
22
|
-
if (isNode
|
|
25
|
+
export async function randomBytes(size) {
|
|
26
|
+
if (isNode) {
|
|
23
27
|
// Node.js / Electron environment
|
|
24
|
-
|
|
28
|
+
const crypto = await getNodeCrypto();
|
|
29
|
+
return crypto.randomBytes(size);
|
|
25
30
|
} else {
|
|
26
31
|
// Browser environment (mobile)
|
|
27
32
|
const bytes = new Uint8Array(size);
|
|
@@ -36,9 +41,10 @@ export function randomBytes(size) {
|
|
|
36
41
|
* @returns {Promise<Uint8Array>} Hash bytes
|
|
37
42
|
*/
|
|
38
43
|
export async function sha256(data) {
|
|
39
|
-
if (isNode
|
|
44
|
+
if (isNode) {
|
|
40
45
|
// Node.js / Electron environment
|
|
41
|
-
|
|
46
|
+
const crypto = await getNodeCrypto();
|
|
47
|
+
return crypto.createHash('sha256').update(data).digest();
|
|
42
48
|
} else {
|
|
43
49
|
// Browser environment (mobile)
|
|
44
50
|
const encoder = new TextEncoder();
|