sha1.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * [js-sha1]{@link https://github.com/emn178/js-sha1}
  3. *
  4. * @version 0.6.0
  5. * @author Chen, Yi-Cyuan [emn178@gmail.com]
  6. * @copyright Chen, Yi-Cyuan 2014-2017
  7. * @license MIT
  8. */
  9. /*jslint bitwise: true */
  10. (function() {
  11. 'use strict';
  12. var root = typeof window === 'object' ? window : {};
  13. var NODE_JS = !root.JS_SHA1_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node;
  14. if (NODE_JS) {
  15. root = global;
  16. }
  17. var COMMON_JS = !root.JS_SHA1_NO_COMMON_JS && typeof module === 'object' && module.exports;
  18. var AMD = typeof define === 'function' && define.amd;
  19. var HEX_CHARS = '0123456789abcdef'.split('');
  20. var EXTRA = [-2147483648, 8388608, 32768, 128];
  21. var SHIFT = [24, 16, 8, 0];
  22. var OUTPUT_TYPES = ['hex', 'array', 'digest', 'arrayBuffer'];
  23. var blocks = [];
  24. var createOutputMethod = function (outputType) {
  25. return function (message) {
  26. return new Sha1(true).update(message)[outputType]();
  27. };
  28. };
  29. var createMethod = function () {
  30. var method = createOutputMethod('hex');
  31. if (NODE_JS) {
  32. method = nodeWrap(method);
  33. }
  34. method.create = function () {
  35. return new Sha1();
  36. };
  37. method.update = function (message) {
  38. return method.create().update(message);
  39. };
  40. for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
  41. var type = OUTPUT_TYPES[i];
  42. method[type] = createOutputMethod(type);
  43. }
  44. return method;
  45. };
  46. var nodeWrap = function (method) {
  47. var crypto = eval("require('crypto')");
  48. var Buffer = eval("require('buffer').Buffer");
  49. var nodeMethod = function (message) {
  50. if (typeof message === 'string') {
  51. return crypto.createHash('sha1').update(message, 'utf8').digest('hex');
  52. } else if (message.constructor === ArrayBuffer) {
  53. message = new Uint8Array(message);
  54. } else if (message.length === undefined) {
  55. return method(message);
  56. }
  57. return crypto.createHash('sha1').update(new Buffer(message)).digest('hex');
  58. };
  59. return nodeMethod;
  60. };
  61. function Sha1(sharedMemory) {
  62. if (sharedMemory) {
  63. blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
  64. blocks[4] = blocks[5] = blocks[6] = blocks[7] =
  65. blocks[8] = blocks[9] = blocks[10] = blocks[11] =
  66. blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
  67. this.blocks = blocks;
  68. } else {
  69. this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  70. }
  71. this.h0 = 0x67452301;
  72. this.h1 = 0xEFCDAB89;
  73. this.h2 = 0x98BADCFE;
  74. this.h3 = 0x10325476;
  75. this.h4 = 0xC3D2E1F0;
  76. this.block = this.start = this.bytes = this.hBytes = 0;
  77. this.finalized = this.hashed = false;
  78. this.first = true;
  79. }
  80. Sha1.prototype.update = function (message) {
  81. if (this.finalized) {
  82. return;
  83. }
  84. var notString = typeof(message) !== 'string';
  85. if (notString && message.constructor === root.ArrayBuffer) {
  86. message = new Uint8Array(message);
  87. }
  88. var code, index = 0, i, length = message.length || 0, blocks = this.blocks;
  89. while (index < length) {
  90. if (this.hashed) {
  91. this.hashed = false;
  92. blocks[0] = this.block;
  93. blocks[16] = blocks[1] = blocks[2] = blocks[3] =
  94. blocks[4] = blocks[5] = blocks[6] = blocks[7] =
  95. blocks[8] = blocks[9] = blocks[10] = blocks[11] =
  96. blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
  97. }
  98. if(notString) {
  99. for (i = this.start; index < length && i < 64; ++index) {
  100. blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
  101. }
  102. } else {
  103. for (i = this.start; index < length && i < 64; ++index) {
  104. code = message.charCodeAt(index);
  105. if (code < 0x80) {
  106. blocks[i >> 2] |= code << SHIFT[i++ & 3];
  107. } else if (code < 0x800) {
  108. blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
  109. blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
  110. } else if (code < 0xd800 || code >= 0xe000) {
  111. blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3];
  112. blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
  113. blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
  114. } else {
  115. code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
  116. blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3];
  117. blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3];
  118. blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
  119. blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
  120. }
  121. }
  122. }
  123. this.lastByteIndex = i;
  124. this.bytes += i - this.start;
  125. if (i >= 64) {
  126. this.block = blocks[16];
  127. this.start = i - 64;
  128. this.hash();
  129. this.hashed = true;
  130. } else {
  131. this.start = i;
  132. }
  133. }
  134. if (this.bytes > 4294967295) {
  135. this.hBytes += this.bytes / 4294967296 << 0;
  136. this.bytes = this.bytes % 4294967296;
  137. }
  138. return this;
  139. };
  140. Sha1.prototype.finalize = function () {
  141. if (this.finalized) {
  142. return;
  143. }
  144. this.finalized = true;
  145. var blocks = this.blocks, i = this.lastByteIndex;
  146. blocks[16] = this.block;
  147. blocks[i >> 2] |= EXTRA[i & 3];
  148. this.block = blocks[16];
  149. if (i >= 56) {
  150. if (!this.hashed) {
  151. this.hash();
  152. }
  153. blocks[0] = this.block;
  154. blocks[16] = blocks[1] = blocks[2] = blocks[3] =
  155. blocks[4] = blocks[5] = blocks[6] = blocks[7] =
  156. blocks[8] = blocks[9] = blocks[10] = blocks[11] =
  157. blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
  158. }
  159. blocks[14] = this.hBytes << 3 | this.bytes >>> 29;
  160. blocks[15] = this.bytes << 3;
  161. this.hash();
  162. };
  163. Sha1.prototype.hash = function () {
  164. var a = this.h0, b = this.h1, c = this.h2, d = this.h3, e = this.h4;
  165. var f, j, t, blocks = this.blocks;
  166. for(j = 16; j < 80; ++j) {
  167. t = blocks[j - 3] ^ blocks[j - 8] ^ blocks[j - 14] ^ blocks[j - 16];
  168. blocks[j] = (t << 1) | (t >>> 31);
  169. }
  170. for(j = 0; j < 20; j += 5) {
  171. f = (b & c) | ((~b) & d);
  172. t = (a << 5) | (a >>> 27);
  173. e = t + f + e + 1518500249 + blocks[j] << 0;
  174. b = (b << 30) | (b >>> 2);
  175. f = (a & b) | ((~a) & c);
  176. t = (e << 5) | (e >>> 27);
  177. d = t + f + d + 1518500249 + blocks[j + 1] << 0;
  178. a = (a << 30) | (a >>> 2);
  179. f = (e & a) | ((~e) & b);
  180. t = (d << 5) | (d >>> 27);
  181. c = t + f + c + 1518500249 + blocks[j + 2] << 0;
  182. e = (e << 30) | (e >>> 2);
  183. f = (d & e) | ((~d) & a);
  184. t = (c << 5) | (c >>> 27);
  185. b = t + f + b + 1518500249 + blocks[j + 3] << 0;
  186. d = (d << 30) | (d >>> 2);
  187. f = (c & d) | ((~c) & e);
  188. t = (b << 5) | (b >>> 27);
  189. a = t + f + a + 1518500249 + blocks[j + 4] << 0;
  190. c = (c << 30) | (c >>> 2);
  191. }
  192. for(; j < 40; j += 5) {
  193. f = b ^ c ^ d;
  194. t = (a << 5) | (a >>> 27);
  195. e = t + f + e + 1859775393 + blocks[j] << 0;
  196. b = (b << 30) | (b >>> 2);
  197. f = a ^ b ^ c;
  198. t = (e << 5) | (e >>> 27);
  199. d = t + f + d + 1859775393 + blocks[j + 1] << 0;
  200. a = (a << 30) | (a >>> 2);
  201. f = e ^ a ^ b;
  202. t = (d << 5) | (d >>> 27);
  203. c = t + f + c + 1859775393 + blocks[j + 2] << 0;
  204. e = (e << 30) | (e >>> 2);
  205. f = d ^ e ^ a;
  206. t = (c << 5) | (c >>> 27);
  207. b = t + f + b + 1859775393 + blocks[j + 3] << 0;
  208. d = (d << 30) | (d >>> 2);
  209. f = c ^ d ^ e;
  210. t = (b << 5) | (b >>> 27);
  211. a = t + f + a + 1859775393 + blocks[j + 4] << 0;
  212. c = (c << 30) | (c >>> 2);
  213. }
  214. for(; j < 60; j += 5) {
  215. f = (b & c) | (b & d) | (c & d);
  216. t = (a << 5) | (a >>> 27);
  217. e = t + f + e - 1894007588 + blocks[j] << 0;
  218. b = (b << 30) | (b >>> 2);
  219. f = (a & b) | (a & c) | (b & c);
  220. t = (e << 5) | (e >>> 27);
  221. d = t + f + d - 1894007588 + blocks[j + 1] << 0;
  222. a = (a << 30) | (a >>> 2);
  223. f = (e & a) | (e & b) | (a & b);
  224. t = (d << 5) | (d >>> 27);
  225. c = t + f + c - 1894007588 + blocks[j + 2] << 0;
  226. e = (e << 30) | (e >>> 2);
  227. f = (d & e) | (d & a) | (e & a);
  228. t = (c << 5) | (c >>> 27);
  229. b = t + f + b - 1894007588 + blocks[j + 3] << 0;
  230. d = (d << 30) | (d >>> 2);
  231. f = (c & d) | (c & e) | (d & e);
  232. t = (b << 5) | (b >>> 27);
  233. a = t + f + a - 1894007588 + blocks[j + 4] << 0;
  234. c = (c << 30) | (c >>> 2);
  235. }
  236. for(; j < 80; j += 5) {
  237. f = b ^ c ^ d;
  238. t = (a << 5) | (a >>> 27);
  239. e = t + f + e - 899497514 + blocks[j] << 0;
  240. b = (b << 30) | (b >>> 2);
  241. f = a ^ b ^ c;
  242. t = (e << 5) | (e >>> 27);
  243. d = t + f + d - 899497514 + blocks[j + 1] << 0;
  244. a = (a << 30) | (a >>> 2);
  245. f = e ^ a ^ b;
  246. t = (d << 5) | (d >>> 27);
  247. c = t + f + c - 899497514 + blocks[j + 2] << 0;
  248. e = (e << 30) | (e >>> 2);
  249. f = d ^ e ^ a;
  250. t = (c << 5) | (c >>> 27);
  251. b = t + f + b - 899497514 + blocks[j + 3] << 0;
  252. d = (d << 30) | (d >>> 2);
  253. f = c ^ d ^ e;
  254. t = (b << 5) | (b >>> 27);
  255. a = t + f + a - 899497514 + blocks[j + 4] << 0;
  256. c = (c << 30) | (c >>> 2);
  257. }
  258. this.h0 = this.h0 + a << 0;
  259. this.h1 = this.h1 + b << 0;
  260. this.h2 = this.h2 + c << 0;
  261. this.h3 = this.h3 + d << 0;
  262. this.h4 = this.h4 + e << 0;
  263. };
  264. Sha1.prototype.hex = function () {
  265. this.finalize();
  266. var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4;
  267. return HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] +
  268. HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] +
  269. HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] +
  270. HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
  271. HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] +
  272. HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] +
  273. HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] +
  274. HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
  275. HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] +
  276. HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] +
  277. HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] +
  278. HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
  279. HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F] +
  280. HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] +
  281. HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] +
  282. HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
  283. HEX_CHARS[(h4 >> 28) & 0x0F] + HEX_CHARS[(h4 >> 24) & 0x0F] +
  284. HEX_CHARS[(h4 >> 20) & 0x0F] + HEX_CHARS[(h4 >> 16) & 0x0F] +
  285. HEX_CHARS[(h4 >> 12) & 0x0F] + HEX_CHARS[(h4 >> 8) & 0x0F] +
  286. HEX_CHARS[(h4 >> 4) & 0x0F] + HEX_CHARS[h4 & 0x0F];
  287. };
  288. Sha1.prototype.toString = Sha1.prototype.hex;
  289. Sha1.prototype.digest = function () {
  290. this.finalize();
  291. var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4;
  292. return [
  293. (h0 >> 24) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 8) & 0xFF, h0 & 0xFF,
  294. (h1 >> 24) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 8) & 0xFF, h1 & 0xFF,
  295. (h2 >> 24) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 8) & 0xFF, h2 & 0xFF,
  296. (h3 >> 24) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 8) & 0xFF, h3 & 0xFF,
  297. (h4 >> 24) & 0xFF, (h4 >> 16) & 0xFF, (h4 >> 8) & 0xFF, h4 & 0xFF
  298. ];
  299. };
  300. Sha1.prototype.array = Sha1.prototype.digest;
  301. Sha1.prototype.arrayBuffer = function () {
  302. this.finalize();
  303. var buffer = new ArrayBuffer(20);
  304. var dataView = new DataView(buffer);
  305. dataView.setUint32(0, this.h0);
  306. dataView.setUint32(4, this.h1);
  307. dataView.setUint32(8, this.h2);
  308. dataView.setUint32(12, this.h3);
  309. dataView.setUint32(16, this.h4);
  310. return buffer;
  311. };
  312. var exports = createMethod();
  313. if (COMMON_JS) {
  314. module.exports = exports;
  315. } else {
  316. root.sha1 = exports;
  317. if (AMD) {
  318. define(function () {
  319. return exports;
  320. });
  321. }
  322. }
  323. })();