PBKDF2.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. (function () {
  2. var C = typeof window === 'undefined' ? require('./Crypto').Crypto : window.Crypto;
  3. // Shortcuts
  4. var util = C.util;
  5. var charenc = C.charenc;
  6. var UTF8 = charenc.UTF8;
  7. var Binary = charenc.Binary;
  8. C.PBKDF2 = function (password, salt, keylen, options) {
  9. // Convert to byte arrays
  10. if (password.constructor == String) {
  11. password = UTF8.stringToBytes(password);
  12. }
  13. if (salt.constructor == String) {
  14. salt = UTF8.stringToBytes(salt);
  15. }
  16. /* else, assume byte arrays already */
  17. // Defaults
  18. var hasher = (options && options.hasher) || C.SHA1;
  19. var iterations = (options && options.iterations) || 1; // Pseudo-random function
  20. function PRF(password, salt) {
  21. return C.HMAC(hasher, salt, password, {
  22. asBytes: true
  23. });
  24. }
  25. // Generate key
  26. var derivedKeyBytes = [];
  27. var blockindex = 1;
  28. while (derivedKeyBytes.length < keylen) {
  29. var block = PRF(password, salt.concat(util.wordsToBytes([blockindex])));
  30. for (var u = block, i = 1; i < iterations; i++) {
  31. u = PRF(password, u);
  32. for (var j = 0; j < block.length; j++) {
  33. block[j] ^= u[j];
  34. }
  35. }
  36. derivedKeyBytes = derivedKeyBytes.concat(block);
  37. blockindex++;
  38. }
  39. // Truncate excess bytes
  40. derivedKeyBytes.length = keylen;
  41. return options && options.asBytes ? derivedKeyBytes : options && options.asString ? Binary.bytesToString(derivedKeyBytes) : util.bytesToHex(derivedKeyBytes);
  42. };
  43. })();