pbkdf2.js 1.2 KB

123456789101112131415161718192021222324252627282930
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.pbkdf2 = pbkdf2;
  4. exports.pbkdf2Sync = pbkdf2Sync;
  5. const pbkdf2_1 = require("@noble/hashes/pbkdf2");
  6. const sha256_1 = require("@noble/hashes/sha256");
  7. const sha512_1 = require("@noble/hashes/sha512");
  8. const utils_js_1 = require("./utils.js");
  9. async function pbkdf2(password, salt, iterations, keylen, digest) {
  10. if (!["sha256", "sha512"].includes(digest)) {
  11. throw new Error("Only sha256 and sha512 are supported");
  12. }
  13. (0, utils_js_1.assertBytes)(password);
  14. (0, utils_js_1.assertBytes)(salt);
  15. return (0, pbkdf2_1.pbkdf2Async)(digest === "sha256" ? sha256_1.sha256 : sha512_1.sha512, password, salt, {
  16. c: iterations,
  17. dkLen: keylen
  18. });
  19. }
  20. function pbkdf2Sync(password, salt, iterations, keylen, digest) {
  21. if (!["sha256", "sha512"].includes(digest)) {
  22. throw new Error("Only sha256 and sha512 are supported");
  23. }
  24. (0, utils_js_1.assertBytes)(password);
  25. (0, utils_js_1.assertBytes)(salt);
  26. return (0, pbkdf2_1.pbkdf2)(digest === "sha256" ? sha256_1.sha256 : sha512_1.sha512, password, salt, {
  27. c: iterations,
  28. dkLen: keylen
  29. });
  30. }