sha1.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. ;(function (root, factory) {
  2. if (typeof exports === "object") {
  3. // CommonJS
  4. module.exports = exports = factory(require("./core"));
  5. } else if (typeof define === "function" && define.amd) {
  6. // AMD
  7. define(["./core"], factory);
  8. } else {
  9. // Global (browser)
  10. factory(root.CryptoJS);
  11. }
  12. }(this, function (CryptoJS) {
  13. (function () {
  14. // Shortcuts
  15. var C = CryptoJS;
  16. var C_lib = C.lib;
  17. var WordArray = C_lib.WordArray;
  18. var Hasher = C_lib.Hasher;
  19. var C_algo = C.algo;
  20. // Reusable object
  21. var W = [];
  22. /**
  23. * SHA-1 hash algorithm.
  24. */
  25. var SHA1 = C_algo.SHA1 = Hasher.extend({
  26. _doReset: function () {
  27. this._hash = new WordArray.init([
  28. 0x67452301, 0xefcdab89,
  29. 0x98badcfe, 0x10325476,
  30. 0xc3d2e1f0
  31. ]);
  32. },
  33. _doProcessBlock: function (M, offset) {
  34. // Shortcut
  35. var H = this._hash.words;
  36. // Working variables
  37. var a = H[0];
  38. var b = H[1];
  39. var c = H[2];
  40. var d = H[3];
  41. var e = H[4];
  42. // Computation
  43. for (var i = 0; i < 80; i++) {
  44. if (i < 16) {
  45. W[i] = M[offset + i] | 0;
  46. } else {
  47. var n = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16];
  48. W[i] = (n << 1) | (n >>> 31);
  49. }
  50. var t = ((a << 5) | (a >>> 27)) + e + W[i];
  51. if (i < 20) {
  52. t += ((b & c) | (~b & d)) + 0x5a827999;
  53. } else if (i < 40) {
  54. t += (b ^ c ^ d) + 0x6ed9eba1;
  55. } else if (i < 60) {
  56. t += ((b & c) | (b & d) | (c & d)) - 0x70e44324;
  57. } else /* if (i < 80) */ {
  58. t += (b ^ c ^ d) - 0x359d3e2a;
  59. }
  60. e = d;
  61. d = c;
  62. c = (b << 30) | (b >>> 2);
  63. b = a;
  64. a = t;
  65. }
  66. // Intermediate hash value
  67. H[0] = (H[0] + a) | 0;
  68. H[1] = (H[1] + b) | 0;
  69. H[2] = (H[2] + c) | 0;
  70. H[3] = (H[3] + d) | 0;
  71. H[4] = (H[4] + e) | 0;
  72. },
  73. _doFinalize: function () {
  74. // Shortcuts
  75. var data = this._data;
  76. var dataWords = data.words;
  77. var nBitsTotal = this._nDataBytes * 8;
  78. var nBitsLeft = data.sigBytes * 8;
  79. // Add padding
  80. dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
  81. dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = Math.floor(nBitsTotal / 0x100000000);
  82. dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = nBitsTotal;
  83. data.sigBytes = dataWords.length * 4;
  84. // Hash final blocks
  85. this._process();
  86. // Return final computed hash
  87. return this._hash;
  88. },
  89. clone: function () {
  90. var clone = Hasher.clone.call(this);
  91. clone._hash = this._hash.clone();
  92. return clone;
  93. }
  94. });
  95. /**
  96. * Shortcut function to the hasher's object interface.
  97. *
  98. * @param {WordArray|string} message The message to hash.
  99. *
  100. * @return {WordArray} The hash.
  101. *
  102. * @static
  103. *
  104. * @example
  105. *
  106. * var hash = CryptoJS.SHA1('message');
  107. * var hash = CryptoJS.SHA1(wordArray);
  108. */
  109. C.SHA1 = Hasher._createHelper(SHA1);
  110. /**
  111. * Shortcut function to the HMAC's object interface.
  112. *
  113. * @param {WordArray|string} message The message to hash.
  114. * @param {WordArray|string} key The secret key.
  115. *
  116. * @return {WordArray} The HMAC.
  117. *
  118. * @static
  119. *
  120. * @example
  121. *
  122. * var hmac = CryptoJS.HmacSHA1(message, key);
  123. */
  124. C.HmacSHA1 = Hasher._createHmacHelper(SHA1);
  125. }());
  126. return CryptoJS.SHA1;
  127. }));