rwadurian/tools/mnemonic-test/node_modules/@cosmjs/crypto/build/pbkdf2.js

55 lines
2.4 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSubtle = getSubtle;
exports.pbkdf2Sha512Subtle = pbkdf2Sha512Subtle;
exports.pbkdf2Sha512Noble = pbkdf2Sha512Noble;
exports.pbkdf2Sha512 = pbkdf2Sha512;
const utils_1 = require("@cosmjs/utils");
const pbkdf2_js_1 = require("@noble/hashes/pbkdf2.js");
const sha2_js_1 = require("@noble/hashes/sha2.js");
/**
* Returns the SubtleCrypto API for this environment if present.
*
* Right now (CosmJS 0.35), all supported environments we are aware of have
* SubtleCrypto. However, we keep the optional return type just in case as we can
* use the pure-JS fallback.
*/
async function getSubtle() {
// From Node.js 15 onwards, webcrypto is available in globalThis.
// In version 15 and 16 this was stored under the webcrypto key.
// With Node.js 17 it was moved to the same locations where browsers
// make it available.
// Loading `require("crypto")` here seems unnecessary since it only
// causes issues with bundlers and does not increase compatibility.
return globalThis?.crypto?.subtle;
}
async function pbkdf2Sha512Subtle(subtle, secret, salt, iterations, keylen) {
(0, utils_1.assert)(subtle, "Argument subtle is falsy");
(0, utils_1.assert)(typeof subtle === "object", "Argument subtle is not of type object");
(0, utils_1.assert)(typeof subtle.importKey === "function", "subtle.importKey is not a function");
(0, utils_1.assert)(typeof subtle.deriveBits === "function", "subtle.deriveBits is not a function");
return subtle.importKey("raw", secret, { name: "PBKDF2" }, false, ["deriveBits"]).then((key) => subtle
.deriveBits({
name: "PBKDF2",
salt: salt,
iterations: iterations,
hash: { name: "SHA-512" },
}, key, keylen * 8)
.then((buffer) => new Uint8Array(buffer)));
}
async function pbkdf2Sha512Noble(secret, salt, iterations, keylen) {
return (0, pbkdf2_js_1.pbkdf2Async)(sha2_js_1.sha512, secret, salt, { c: iterations, dkLen: keylen });
}
/**
* A pbkdf2 implementation for BIP39. This is not exported at package level and thus a private API.
*/
async function pbkdf2Sha512(secret, salt, iterations, keylen) {
const subtle = await getSubtle();
if (subtle) {
return pbkdf2Sha512Subtle(subtle, secret, salt, iterations, keylen);
}
else {
return pbkdf2Sha512Noble(secret, salt, iterations, keylen);
}
}
//# sourceMappingURL=pbkdf2.js.map