hmac.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. CryptoJS v3.0.2
  3. code.google.com/p/crypto-js
  4. (c) 2009-2012 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 C_enc = C.enc;
  13. var Utf8 = C_enc.Utf8;
  14. var C_algo = C.algo;
  15. /**
  16. * HMAC algorithm.
  17. */
  18. var HMAC = C_algo.HMAC = Base.extend({
  19. /**
  20. * Initializes a newly created HMAC.
  21. *
  22. * @param {Hasher} hasher The hash algorithm to use.
  23. * @param {WordArray|string} key The secret key.
  24. *
  25. * @example
  26. *
  27. * var hmacHasher = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, key);
  28. */
  29. init: function (hasher, key) {
  30. // Init hasher
  31. hasher = this._hasher = hasher.create();
  32. // Convert string to WordArray, else assume WordArray already
  33. if (typeof key == 'string') {
  34. key = Utf8.parse(key);
  35. }
  36. // Shortcuts
  37. var hasherBlockSize = hasher.blockSize;
  38. var hasherBlockSizeBytes = hasherBlockSize * 4;
  39. // Allow arbitrary length keys
  40. if (key.sigBytes > hasherBlockSizeBytes) {
  41. key = hasher.finalize(key);
  42. }
  43. // Clone key for inner and outer pads
  44. var oKey = this._oKey = key.clone();
  45. var iKey = this._iKey = key.clone();
  46. // Shortcuts
  47. var oKeyWords = oKey.words;
  48. var iKeyWords = iKey.words;
  49. // XOR keys with pad constants
  50. for (var i = 0; i < hasherBlockSize; i++) {
  51. oKeyWords[i] ^= 0x5c5c5c5c;
  52. iKeyWords[i] ^= 0x36363636;
  53. }
  54. oKey.sigBytes = iKey.sigBytes = hasherBlockSizeBytes;
  55. // Set initial values
  56. this.reset();
  57. },
  58. /**
  59. * Resets this HMAC to its initial state.
  60. *
  61. * @example
  62. *
  63. * hmacHasher.reset();
  64. */
  65. reset: function () {
  66. // Shortcut
  67. var hasher = this._hasher;
  68. // Reset
  69. hasher.reset();
  70. hasher.update(this._iKey);
  71. },
  72. /**
  73. * Updates this HMAC with a message.
  74. *
  75. * @param {WordArray|string} messageUpdate The message to append.
  76. *
  77. * @return {HMAC} This HMAC instance.
  78. *
  79. * @example
  80. *
  81. * hmacHasher.update('message');
  82. * hmacHasher.update(wordArray);
  83. */
  84. update: function (messageUpdate) {
  85. this._hasher.update(messageUpdate);
  86. // Chainable
  87. return this;
  88. },
  89. /**
  90. * Finalizes the HMAC computation.
  91. * Note that the finalize operation is effectively a destructive, read-once operation.
  92. *
  93. * @param {WordArray|string} messageUpdate (Optional) A final message update.
  94. *
  95. * @return {WordArray} The HMAC.
  96. *
  97. * @example
  98. *
  99. * var hmac = hmacHasher.finalize();
  100. * var hmac = hmacHasher.finalize('message');
  101. * var hmac = hmacHasher.finalize(wordArray);
  102. */
  103. finalize: function (messageUpdate) {
  104. // Shortcut
  105. var hasher = this._hasher;
  106. // Compute HMAC
  107. var innerHash = hasher.finalize(messageUpdate);
  108. hasher.reset();
  109. var hmac = hasher.finalize(this._oKey.clone().concat(innerHash));
  110. return hmac;
  111. }
  112. });
  113. }());