hmac.js 3.6 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 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 = new hasher.init();
  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. // Clamp excess bits
  44. key.clamp();
  45. // Clone key for inner and outer pads
  46. var oKey = this._oKey = key.clone();
  47. var iKey = this._iKey = key.clone();
  48. // Shortcuts
  49. var oKeyWords = oKey.words;
  50. var iKeyWords = iKey.words;
  51. // XOR keys with pad constants
  52. for (var i = 0; i < hasherBlockSize; i++) {
  53. oKeyWords[i] ^= 0x5c5c5c5c;
  54. iKeyWords[i] ^= 0x36363636;
  55. }
  56. oKey.sigBytes = iKey.sigBytes = hasherBlockSizeBytes;
  57. // Set initial values
  58. this.reset();
  59. },
  60. /**
  61. * Resets this HMAC to its initial state.
  62. *
  63. * @example
  64. *
  65. * hmacHasher.reset();
  66. */
  67. reset: function () {
  68. // Shortcut
  69. var hasher = this._hasher;
  70. // Reset
  71. hasher.reset();
  72. hasher.update(this._iKey);
  73. },
  74. /**
  75. * Updates this HMAC with a message.
  76. *
  77. * @param {WordArray|string} messageUpdate The message to append.
  78. *
  79. * @return {HMAC} This HMAC instance.
  80. *
  81. * @example
  82. *
  83. * hmacHasher.update('message');
  84. * hmacHasher.update(wordArray);
  85. */
  86. update: function (messageUpdate) {
  87. this._hasher.update(messageUpdate);
  88. // Chainable
  89. return this;
  90. },
  91. /**
  92. * Finalizes the HMAC computation.
  93. * Note that the finalize operation is effectively a destructive, read-once operation.
  94. *
  95. * @param {WordArray|string} messageUpdate (Optional) A final message update.
  96. *
  97. * @return {WordArray} The HMAC.
  98. *
  99. * @example
  100. *
  101. * var hmac = hmacHasher.finalize();
  102. * var hmac = hmacHasher.finalize('message');
  103. * var hmac = hmacHasher.finalize(wordArray);
  104. */
  105. finalize: function (messageUpdate) {
  106. // Shortcut
  107. var hasher = this._hasher;
  108. // Compute HMAC
  109. var innerHash = hasher.finalize(messageUpdate);
  110. hasher.reset();
  111. var hmac = hasher.finalize(this._oKey.clone().concat(innerHash));
  112. return hmac;
  113. }
  114. });
  115. }());