pbkdf2.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. /**
  3. * A **Password-Based Key-Derivation Function** is designed to create
  4. * a sequence of bytes suitible as a **key** from a human-rememberable
  5. * password.
  6. *
  7. * @_subsection: api/crypto:Passwords [about-pbkdf]
  8. */
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. exports.pbkdf2 = void 0;
  11. const crypto_js_1 = require("./crypto.js");
  12. const index_js_1 = require("../utils/index.js");
  13. let locked = false;
  14. const _pbkdf2 = function (password, salt, iterations, keylen, algo) {
  15. return (0, crypto_js_1.pbkdf2Sync)(password, salt, iterations, keylen, algo);
  16. };
  17. let __pbkdf2 = _pbkdf2;
  18. /**
  19. * Return the [[link-pbkdf2]] for %%keylen%% bytes for %%password%% using
  20. * the %%salt%% and using %%iterations%% of %%algo%%.
  21. *
  22. * This PBKDF is outdated and should not be used in new projects, but is
  23. * required to decrypt older files.
  24. *
  25. * @example:
  26. * // The password must be converted to bytes, and it is generally
  27. * // best practices to ensure the string has been normalized. Many
  28. * // formats explicitly indicate the normalization form to use.
  29. * password = "hello"
  30. * passwordBytes = toUtf8Bytes(password, "NFKC")
  31. *
  32. * salt = id("some-salt")
  33. *
  34. * // Compute the PBKDF2
  35. * pbkdf2(passwordBytes, salt, 1024, 16, "sha256")
  36. * //_result:
  37. */
  38. function pbkdf2(_password, _salt, iterations, keylen, algo) {
  39. const password = (0, index_js_1.getBytes)(_password, "password");
  40. const salt = (0, index_js_1.getBytes)(_salt, "salt");
  41. return (0, index_js_1.hexlify)(__pbkdf2(password, salt, iterations, keylen, algo));
  42. }
  43. exports.pbkdf2 = pbkdf2;
  44. pbkdf2._ = _pbkdf2;
  45. pbkdf2.lock = function () { locked = true; };
  46. pbkdf2.register = function (func) {
  47. if (locked) {
  48. throw new Error("pbkdf2 is locked");
  49. }
  50. __pbkdf2 = func;
  51. };
  52. Object.freeze(pbkdf2);
  53. //# sourceMappingURL=pbkdf2.js.map