Improve readability crypro -> nodeCrypto

This commit is contained in:
Simon Warta 2022-12-20 14:13:38 +01:00
parent 29afc855ef
commit f5e9bb13f2
2 changed files with 24 additions and 20 deletions

View File

@ -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);
}
});

View File

@ -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<any | undefined> {
export async function getNodeCrypto(): Promise<any | undefined> {
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<Uint8Array> {
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);
}