ethers.js/lib.esm/crypto/pbkdf2.js

36 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-12-03 05:27:06 +03:00
/**
* A **Password-Based Key-Derivation Function** is designed to create
* a sequence of bytes suitible as a **key** from a human-rememberable
* password.
*
* @_subsection: api/crypto:Passwords [about-pbkdf]
*/
2022-09-05 23:57:11 +03:00
import { pbkdf2Sync } from "./crypto.js";
2022-09-16 05:58:45 +03:00
import { getBytes, hexlify } from "../utils/index.js";
2022-09-05 23:57:11 +03:00
let locked = false;
const _pbkdf2 = function (password, salt, iterations, keylen, algo) {
return pbkdf2Sync(password, salt, iterations, keylen, algo);
};
let __pbkdf2 = _pbkdf2;
2022-12-03 05:27:06 +03:00
/**
* Return the [[link-pbkdf2]] for %%keylen%% bytes for %%password%% using
* the %%salt%% and using %%iterations%% of %%algo%%.
*
* This PBKDF is outdated and should not be used in new projects, but is
* required to decrypt older files.
*/
2022-09-05 23:57:11 +03:00
export function pbkdf2(_password, _salt, iterations, keylen, algo) {
2022-09-16 05:58:45 +03:00
const password = getBytes(_password, "password");
const salt = getBytes(_salt, "salt");
2022-09-05 23:57:11 +03:00
return hexlify(__pbkdf2(password, salt, iterations, keylen, algo));
}
pbkdf2._ = _pbkdf2;
pbkdf2.lock = function () { locked = true; };
pbkdf2.register = function (func) {
if (locked) {
throw new Error("pbkdf2 is locked");
}
__pbkdf2 = func;
};
Object.freeze(pbkdf2);
//# sourceMappingURL=pbkdf2.js.map