Skip to content

Commit dca1d4f

Browse files
committed
Re-use noble-hashes crypto module
1 parent 0652c61 commit dca1d4f

File tree

3 files changed

+6
-12
lines changed

3 files changed

+6
-12
lines changed

src/aes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { concatBytes, crypto, equalsBytes } from "./utils";
1+
import { crypto } from "@noble/hashes/crypto";
2+
import { concatBytes, equalsBytes } from "./utils";
23

34
function validateOpt(key: Uint8Array, iv: Uint8Array, mode: string) {
45
if (!mode.startsWith("aes-")) {

src/bip39/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { pbkdf2, pbkdf2Async } from "@noble/hashes/pbkdf2";
22
import { sha256 } from "@noble/hashes/sha256";
33
import { sha512 } from "@noble/hashes/sha512";
4-
import { assertBytes, assertNumber } from "@noble/hashes/utils";
4+
import { assertBytes, assertNumber, randomBytes } from "@noble/hashes/utils";
55
import { utils as baseUtils } from "micro-base";
6-
import { getRandomBytesSync } from "../random";
76

87
const isJapanese = (wordlist: string[]) =>
98
wordlist[0] === "\u3042\u3044\u3053\u304f\u3057\u3093"; // Japanese wordlist
@@ -27,7 +26,7 @@ export function generateMnemonic(
2726
if (strength % 32 !== 0) {
2827
throw new TypeError("Invalid entropy");
2928
}
30-
return entropyToMnemonic(getRandomBytesSync(strength / 8), wordlist);
29+
return entropyToMnemonic(randomBytes(strength / 8), wordlist);
3130
}
3231

3332
const checksum = (entropy: Uint8Array) => {

src/random.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
import { crypto } from "./utils";
1+
import { randomBytes } from "@noble/hashes/utils";
22

33
export function getRandomBytesSync(bytes: number): Uint8Array {
4-
if (crypto.web) {
5-
return crypto.web.getRandomValues(new Uint8Array(bytes));
6-
} else if (crypto.node) {
7-
return new Uint8Array(crypto.node.randomBytes(bytes).buffer);
8-
} else {
9-
throw new Error("The environment doesn't have randomBytes function");
10-
}
4+
return randomBytes(bytes);
115
}
126

137
export async function getRandomBytes(bytes: number): Promise<Uint8Array> {

0 commit comments

Comments
 (0)