encryptPassword.js 850 B

12345678910111213141516171819202122232425262728293031323334353637
  1. let generateSalt = ()=>{
  2. const length = 16;
  3. const salt = new Uint8Array(length);
  4. crypto.getRandomValues(salt);
  5. return btoa(String.fromCharCode(...salt));
  6. }
  7. export default async (password) =>{
  8. const salt = generateSalt();
  9. const iterations = 100000;
  10. const hash = "SHA-256";
  11. const encoder = new TextEncoder();
  12. const baseKey = await crypto.subtle.importKey(
  13. "raw",
  14. encoder.encode(password),
  15. {name: "PBKDF2"},
  16. false,
  17. ["deriveBits"]
  18. );
  19. const derivedBits = await crypto.subtle.deriveBits(
  20. {
  21. name: "PBKDF2",
  22. salt: encoder.encode(salt),
  23. iterations,
  24. hash
  25. },
  26. baseKey,
  27. 256
  28. );
  29. return {
  30. hash: btoa(String.fromCharCode(...new Uint8Array(derivedBits))),
  31. salt: salt
  32. };
  33. }