pbkdf2.js 980 B

1234567891011121314151617181920212223242526
  1. import { pbkdf2 as _pbkdf2, pbkdf2Async as _pbkdf2Async } from "@noble/hashes/pbkdf2";
  2. import { sha256 } from "@noble/hashes/sha256";
  3. import { sha512 } from "@noble/hashes/sha512";
  4. import { assertBytes } from "./utils.js";
  5. export async function pbkdf2(password, salt, iterations, keylen, digest) {
  6. if (!["sha256", "sha512"].includes(digest)) {
  7. throw new Error("Only sha256 and sha512 are supported");
  8. }
  9. assertBytes(password);
  10. assertBytes(salt);
  11. return _pbkdf2Async(digest === "sha256" ? sha256 : sha512, password, salt, {
  12. c: iterations,
  13. dkLen: keylen
  14. });
  15. }
  16. export function pbkdf2Sync(password, salt, iterations, keylen, digest) {
  17. if (!["sha256", "sha512"].includes(digest)) {
  18. throw new Error("Only sha256 and sha512 are supported");
  19. }
  20. assertBytes(password);
  21. assertBytes(salt);
  22. return _pbkdf2(digest === "sha256" ? sha256 : sha512, password, salt, {
  23. c: iterations,
  24. dkLen: keylen
  25. });
  26. }