pbkdf2.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2012 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Implementation of PBKDF2 in JavaScript.
  16. * @see http://en.wikipedia.org/wiki/PBKDF2
  17. *
  18. * Currently we only support HMAC-SHA1 as the underlying hash function. To add a
  19. * new hash function, add a static method similar to deriveKeyFromPasswordSha1()
  20. * and implement the specific computeBlockCallback() using the hash function.
  21. *
  22. * Usage:
  23. * var key = pbkdf2.deriveKeySha1(
  24. * stringToByteArray('password'), stringToByteArray('salt'), 1000, 128);
  25. *
  26. */
  27. goog.provide('goog.crypt.pbkdf2');
  28. goog.require('goog.array');
  29. goog.require('goog.asserts');
  30. goog.require('goog.crypt');
  31. goog.require('goog.crypt.Hmac');
  32. goog.require('goog.crypt.Sha1');
  33. /**
  34. * Derives key from password using PBKDF2-SHA1
  35. * @param {!Array<number>} password Byte array representation of the password
  36. * from which the key is derived.
  37. * @param {!Array<number>} initialSalt Byte array representation of the salt.
  38. * @param {number} iterations Number of interations when computing the key.
  39. * @param {number} keyLength Length of the output key in bits.
  40. * Must be multiple of 8.
  41. * @return {!Array<number>} Byte array representation of the output key.
  42. */
  43. goog.crypt.pbkdf2.deriveKeySha1 = function(
  44. password, initialSalt, iterations, keyLength) {
  45. // Length of the HMAC-SHA1 output in bits.
  46. var HASH_LENGTH = 160;
  47. /**
  48. * Compute each block of the key using HMAC-SHA1.
  49. * @param {!Array<number>} index Byte array representation of the index of
  50. * the block to be computed.
  51. * @return {!Array<number>} Byte array representation of the output block.
  52. */
  53. var computeBlock = function(index) {
  54. // Initialize the result to be array of 0 such that its xor with the first
  55. // block would be the first block.
  56. var result = goog.array.repeat(0, HASH_LENGTH / 8);
  57. // Initialize the salt of the first iteration to initialSalt || i.
  58. var salt = initialSalt.concat(index);
  59. var hmac = new goog.crypt.Hmac(new goog.crypt.Sha1(), password, 64);
  60. // Compute and XOR each iteration.
  61. for (var i = 0; i < iterations; i++) {
  62. // The salt of the next iteration is the result of the current iteration.
  63. salt = hmac.getHmac(salt);
  64. result = goog.crypt.xorByteArray(result, salt);
  65. }
  66. return result;
  67. };
  68. return goog.crypt.pbkdf2.deriveKeyFromPassword_(
  69. computeBlock, HASH_LENGTH, keyLength);
  70. };
  71. /**
  72. * Compute each block of the key using PBKDF2.
  73. * @param {Function} computeBlock Function to compute each block of the output
  74. * key.
  75. * @param {number} hashLength Length of each block in bits. This is determined
  76. * by the specific hash function used. Must be multiple of 8.
  77. * @param {number} keyLength Length of the output key in bits.
  78. * Must be multiple of 8.
  79. * @return {!Array<number>} Byte array representation of the output key.
  80. * @private
  81. */
  82. goog.crypt.pbkdf2.deriveKeyFromPassword_ = function(
  83. computeBlock, hashLength, keyLength) {
  84. goog.asserts.assert(keyLength % 8 == 0, 'invalid output key length');
  85. // Compute and concactate each block of the output key.
  86. var numBlocks = Math.ceil(keyLength / hashLength);
  87. goog.asserts.assert(numBlocks >= 1, 'invalid number of blocks');
  88. var result = [];
  89. for (var i = 1; i <= numBlocks; i++) {
  90. var indexBytes = goog.crypt.pbkdf2.integerToByteArray_(i);
  91. result = result.concat(computeBlock(indexBytes));
  92. }
  93. // Trim the last block if needed.
  94. var lastBlockSize = keyLength % hashLength;
  95. if (lastBlockSize != 0) {
  96. var desiredBytes = ((numBlocks - 1) * hashLength + lastBlockSize) / 8;
  97. result.splice(desiredBytes, (hashLength - lastBlockSize) / 8);
  98. }
  99. return result;
  100. };
  101. /**
  102. * Converts an integer number to a 32-bit big endian byte array.
  103. * @param {number} n Integer number to be converted.
  104. * @return {!Array<number>} Byte Array representation of the 32-bit big endian
  105. * encoding of n.
  106. * @private
  107. */
  108. goog.crypt.pbkdf2.integerToByteArray_ = function(n) {
  109. var result = new Array(4);
  110. result[0] = n >> 24 & 0xFF;
  111. result[1] = n >> 16 & 0xFF;
  112. result[2] = n >> 8 & 0xFF;
  113. result[3] = n & 0xFF;
  114. return result;
  115. };