pbkdf2.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. CryptoJS v3.1.2
  3. code.google.com/p/crypto-js
  4. (c) 2009-2013 by Jeff Mott. All rights reserved.
  5. code.google.com/p/crypto-js/wiki/License
  6. */
  7. (function () {
  8. // Shortcuts
  9. var C = CryptoJS;
  10. var C_lib = C.lib;
  11. var Base = C_lib.Base;
  12. var WordArray = C_lib.WordArray;
  13. var C_algo = C.algo;
  14. var SHA1 = C_algo.SHA1;
  15. var HMAC = C_algo.HMAC;
  16. /**
  17. * Password-Based Key Derivation Function 2 algorithm.
  18. */
  19. var PBKDF2 = C_algo.PBKDF2 = Base.extend({
  20. /**
  21. * Configuration options.
  22. *
  23. * @property {number} keySize The key size in words to generate. Default: 4 (128 bits)
  24. * @property {Hasher} hasher The hasher to use. Default: SHA1
  25. * @property {number} iterations The number of iterations to perform. Default: 1
  26. */
  27. cfg: Base.extend({
  28. keySize: 128/32,
  29. hasher: SHA1,
  30. iterations: 1
  31. }),
  32. /**
  33. * Initializes a newly created key derivation function.
  34. *
  35. * @param {Object} cfg (Optional) The configuration options to use for the derivation.
  36. *
  37. * @example
  38. *
  39. * var kdf = CryptoJS.algo.PBKDF2.create();
  40. * var kdf = CryptoJS.algo.PBKDF2.create({ keySize: 8 });
  41. * var kdf = CryptoJS.algo.PBKDF2.create({ keySize: 8, iterations: 1000 });
  42. */
  43. init: function (cfg) {
  44. this.cfg = this.cfg.extend(cfg);
  45. },
  46. /**
  47. * Computes the Password-Based Key Derivation Function 2.
  48. *
  49. * @param {WordArray|string} password The password.
  50. * @param {WordArray|string} salt A salt.
  51. *
  52. * @return {WordArray} The derived key.
  53. *
  54. * @example
  55. *
  56. * var key = kdf.compute(password, salt);
  57. */
  58. compute: function (password, salt) {
  59. // Shortcut
  60. var cfg = this.cfg;
  61. // Init HMAC
  62. var hmac = HMAC.create(cfg.hasher, password);
  63. // Initial values
  64. var derivedKey = WordArray.create();
  65. var blockIndex = WordArray.create([0x00000001]);
  66. // Shortcuts
  67. var derivedKeyWords = derivedKey.words;
  68. var blockIndexWords = blockIndex.words;
  69. var keySize = cfg.keySize;
  70. var iterations = cfg.iterations;
  71. // Generate key
  72. while (derivedKeyWords.length < keySize) {
  73. var block = hmac.update(salt).finalize(blockIndex);
  74. hmac.reset();
  75. // Shortcuts
  76. var blockWords = block.words;
  77. var blockWordsLength = blockWords.length;
  78. // Iterations
  79. var intermediate = block;
  80. for (var i = 1; i < iterations; i++) {
  81. intermediate = hmac.finalize(intermediate);
  82. hmac.reset();
  83. // Shortcut
  84. var intermediateWords = intermediate.words;
  85. // XOR intermediate with block
  86. for (var j = 0; j < blockWordsLength; j++) {
  87. blockWords[j] ^= intermediateWords[j];
  88. }
  89. }
  90. derivedKey.concat(block);
  91. blockIndexWords[0]++;
  92. }
  93. derivedKey.sigBytes = keySize * 4;
  94. return derivedKey;
  95. }
  96. });
  97. /**
  98. * Computes the Password-Based Key Derivation Function 2.
  99. *
  100. * @param {WordArray|string} password The password.
  101. * @param {WordArray|string} salt A salt.
  102. * @param {Object} cfg (Optional) The configuration options to use for this computation.
  103. *
  104. * @return {WordArray} The derived key.
  105. *
  106. * @static
  107. *
  108. * @example
  109. *
  110. * var key = CryptoJS.PBKDF2(password, salt);
  111. * var key = CryptoJS.PBKDF2(password, salt, { keySize: 8 });
  112. * var key = CryptoJS.PBKDF2(password, salt, { keySize: 8, iterations: 1000 });
  113. */
  114. C.PBKDF2 = function (password, salt, cfg) {
  115. return PBKDF2.create(cfg).compute(password, salt);
  116. };
  117. }());