diff --git a/packages/crypto/src/pbkdf2.spec.ts b/packages/crypto/src/pbkdf2.spec.ts index d76b48c4..a4d969b8 100644 --- a/packages/crypto/src/pbkdf2.spec.ts +++ b/packages/crypto/src/pbkdf2.spec.ts @@ -1,11 +1,11 @@ import { fromHex, toAscii, toUtf8 } from "@cosmjs/encoding"; import { - getCryptoModule, + getNodeCrypto, getSubtle, pbkdf2Sha512, - pbkdf2Sha512Crypto, pbkdf2Sha512Noble, + pbkdf2Sha512NodeCrypto, pbkdf2Sha512Subtle, } from "./pbkdf2"; @@ -146,20 +146,20 @@ describe("pbkdf2", () => { }); }); - describe("pbkdf2Sha512Crypto", () => { + describe("pbkdf2Sha512NodeCrypto", () => { it("works", async () => { - const crypto = await getCryptoModule(); - if (!crypto) pending("The crypto module is not available in this environment"); + const nodeCrypto = await getNodeCrypto(); + if (!nodeCrypto) pending("The crypto module is not available in this environment"); { const { secret, salt, iterations, keylen, expected } = botanTest; - const hash = await pbkdf2Sha512Crypto(crypto, secret, salt, iterations, keylen); + const hash = await pbkdf2Sha512NodeCrypto(nodeCrypto, secret, salt, iterations, keylen); expect(hash).toEqual(expected); } for (const [index, test] of brycxTests.entries()) { const { secret, salt, iterations, keylen, expected } = test; - const hash = await pbkdf2Sha512Crypto(crypto, secret, salt, iterations, keylen); + const hash = await pbkdf2Sha512NodeCrypto(nodeCrypto, secret, salt, iterations, keylen); expect(hash).withContext(`brycx tests index ${index}`).toEqual(expected); } }); diff --git a/packages/crypto/src/pbkdf2.ts b/packages/crypto/src/pbkdf2.ts index 38ad1ab3..5176346d 100644 --- a/packages/crypto/src/pbkdf2.ts +++ b/packages/crypto/src/pbkdf2.ts @@ -9,15 +9,15 @@ import { sha512 as nobleSha512 } from "@noble/hashes/sha512"; * Detects an unimplemented fallback module from Webpack 5 and returns * `undefined` in that case. */ -export async function getCryptoModule(): Promise { +export async function getNodeCrypto(): Promise { try { - const crypto = await import("crypto"); + const nodeCrypto = await import("crypto"); // We get `Object{default: Object{}}` as a fallback when using // `crypto: false` in Webpack 5, which we interprete as unavailable. - if (typeof crypto === "object" && Object.keys(crypto).length <= 1) { + if (typeof nodeCrypto === "object" && Object.keys(nodeCrypto).length <= 1) { return undefined; } - return crypto; + return nodeCrypto; } catch { return undefined; } @@ -68,20 +68,24 @@ export async function pbkdf2Sha512Subtle( ); } -export async function pbkdf2Sha512Crypto( +/** + * Implements pbkdf2-sha512 using the Node.js crypro module (`import "crypto"`). + * This does not use subtle from [Crypto](https://developer.mozilla.org/en-US/docs/Web/API/Crypto). + */ +export async function pbkdf2Sha512NodeCrypto( // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types - crypto: any, + nodeCrypto: any, secret: Uint8Array, salt: Uint8Array, iterations: number, keylen: number, ): Promise { - assert(crypto, "Argument crypto is falsy"); - assert(typeof crypto === "object", "Argument crypto is not of type object"); - assert(typeof crypto.pbkdf2 === "function", "crypto.pbkdf2 is not a function"); + assert(nodeCrypto, "Argument nodeCrypto is falsy"); + assert(typeof nodeCrypto === "object", "Argument nodeCrypto is not of type object"); + assert(typeof nodeCrypto.pbkdf2 === "function", "nodeCrypto.pbkdf2 is not a function"); return new Promise((resolve, reject) => { - crypto.pbkdf2(secret, salt, iterations, keylen, "sha512", (error: any, result: any) => { + nodeCrypto.pbkdf2(secret, salt, iterations, keylen, "sha512", (error: any, result: any) => { if (error) { reject(error); } else { @@ -113,9 +117,9 @@ export async function pbkdf2Sha512( if (subtle) { return pbkdf2Sha512Subtle(subtle, secret, salt, iterations, keylen); } else { - const crypto = await getCryptoModule(); - if (crypto) { - return pbkdf2Sha512Crypto(crypto, secret, salt, iterations, keylen); + const nodeCrypto = await getNodeCrypto(); + if (nodeCrypto) { + return pbkdf2Sha512NodeCrypto(nodeCrypto, secret, salt, iterations, keylen); } else { return pbkdf2Sha512Noble(secret, salt, iterations, keylen); }