sha1.js 3.6 KB

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