HmacSHA1.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. var CryptoJSNew = CryptoJSNew || function(g, l) {
  2. var e = {}, d = e.lib = {}, m = function() {}, k = d.Base = {
  3. extend: function(a) {
  4. m.prototype = this;
  5. var c = new m;
  6. a && c.mixIn(a);
  7. c.hasOwnProperty("init") || (c.init = function() {
  8. c.$super.init.apply(this, arguments)
  9. });
  10. c.init.prototype = c;
  11. c.$super = this;
  12. return c
  13. },
  14. create: function() {
  15. var a = this.extend();
  16. a.init.apply(a, arguments);
  17. return a
  18. },
  19. init: function() {},
  20. mixIn: function(a) {
  21. for (var c in a) a.hasOwnProperty(c) && (this[c] = a[c]);
  22. a.hasOwnProperty("toString") && (this.toString = a.toString)
  23. },
  24. clone: function() {
  25. return this.init.prototype.extend(this)
  26. }
  27. },
  28. p = d.WordArray = k.extend({
  29. init: function(a, c) {
  30. a = this.words = a || [];
  31. this.sigBytes = c != l ? c : 4 * a.length
  32. },
  33. toString: function(a) {
  34. return (a || n).stringify(this)
  35. },
  36. concat: function(a) {
  37. var c = this.words,
  38. q = a.words,
  39. f = this.sigBytes;
  40. a = a.sigBytes;
  41. this.clamp();
  42. if (f % 4)
  43. for (var b = 0; b < a; b++) c[f + b >>> 2] |= (q[b >>> 2] >>> 24 - 8 * (b % 4) & 255) << 24 - 8 * ((f + b) % 4);
  44. else if (65535 < q.length)
  45. for (b = 0; b < a; b += 4) c[f + b >>> 2] = q[b >>> 2];
  46. else c.push.apply(c, q);
  47. this.sigBytes += a;
  48. return this
  49. },
  50. clamp: function() {
  51. var a = this.words,
  52. c = this.sigBytes;
  53. a[c >>> 2] &= 4294967295 << 32 - 8 * (c % 4);
  54. a.length = g.ceil(c / 4)
  55. },
  56. clone: function() {
  57. var a = k.clone.call(this);
  58. a.words = this.words.slice(0);
  59. return a
  60. },
  61. random: function(a) {
  62. for (var c = [], b = 0; b < a; b += 4) c.push(4294967296 * g.random() | 0);
  63. return new p.init(c, a)
  64. }
  65. }),
  66. b = e.enc = {}, n = b.Hex = {
  67. stringify: function(a) {
  68. var c = a.words;
  69. a = a.sigBytes;
  70. for (var b = [], f = 0; f < a; f++) {
  71. var d = c[f >>> 2] >>> 24 - 8 * (f % 4) & 255;
  72. b.push((d >>> 4).toString(16));
  73. b.push((d & 15).toString(16))
  74. }
  75. return b.join("")
  76. },
  77. parse: function(a) {
  78. for (var c = a.length, b = [], f = 0; f < c; f += 2) b[f >>> 3] |= parseInt(a.substr(f, 2), 16) << 24 - 4 * (f % 8);
  79. return new p.init(b, c / 2)
  80. }
  81. }, j = b.Latin1 = {
  82. stringify: function(a) {
  83. var c = a.words;
  84. a = a.sigBytes;
  85. for (var b = [], f = 0; f < a; f++) b.push(String.fromCharCode(c[f >>> 2] >>> 24 - 8 * (f % 4) & 255));
  86. return b.join("")
  87. },
  88. parse: function(a) {
  89. for (var c = a.length, b = [], f = 0; f < c; f++) b[f >>> 2] |= (a.charCodeAt(f) & 255) << 24 - 8 * (f % 4);
  90. return new p.init(b, c)
  91. }
  92. }, h = b.Utf8 = {
  93. stringify: function(a) {
  94. try {
  95. return decodeURIComponent(escape(j.stringify(a)))
  96. } catch (c) {
  97. throw Error("Malformed UTF-8 data");
  98. }
  99. },
  100. parse: function(a) {
  101. return j.parse(unescape(encodeURIComponent(a)))
  102. }
  103. },
  104. r = d.BufferedBlockAlgorithm = k.extend({
  105. reset: function() {
  106. this._data = new p.init;
  107. this._nDataBytes = 0
  108. },
  109. _append: function(a) {
  110. "string" == typeof a && (a = h.parse(a));
  111. this._data.concat(a);
  112. this._nDataBytes += a.sigBytes
  113. },
  114. _process: function(a) {
  115. var c = this._data,
  116. b = c.words,
  117. f = c.sigBytes,
  118. d = this.blockSize,
  119. e = f / (4 * d),
  120. e = a ? g.ceil(e) : g.max((e | 0) - this._minBufferSize, 0);
  121. a = e * d;
  122. f = g.min(4 * a, f);
  123. if (a) {
  124. for (var k = 0; k < a; k += d) this._doProcessBlock(b, k);
  125. k = b.splice(0, a);
  126. c.sigBytes -= f
  127. }
  128. return new p.init(k, f)
  129. },
  130. clone: function() {
  131. var a = k.clone.call(this);
  132. a._data = this._data.clone();
  133. return a
  134. },
  135. _minBufferSize: 0
  136. });
  137. d.Hasher = r.extend({
  138. cfg: k.extend(),
  139. init: function(a) {
  140. this.cfg = this.cfg.extend(a);
  141. this.reset()
  142. },
  143. reset: function() {
  144. r.reset.call(this);
  145. this._doReset()
  146. },
  147. update: function(a) {
  148. this._append(a);
  149. this._process();
  150. return this
  151. },
  152. finalize: function(a) {
  153. a && this._append(a);
  154. return this._doFinalize()
  155. },
  156. blockSize: 16,
  157. _createHelper: function(a) {
  158. return function(b, d) {
  159. return (new a.init(d)).finalize(b)
  160. }
  161. },
  162. _createHmacHelper: function(a) {
  163. return function(b, d) {
  164. return (new s.HMAC.init(a, d)).finalize(b)
  165. }
  166. }
  167. });
  168. var s = e.algo = {};
  169. return e
  170. }(Math);
  171. (function() {
  172. var g = CryptoJSNew,
  173. l = g.lib,
  174. e = l.WordArray,
  175. d = l.Hasher,
  176. m = [],
  177. l = g.algo.SHA1 = d.extend({
  178. _doReset: function() {
  179. this._hash = new e.init([1732584193, 4023233417, 2562383102, 271733878, 3285377520])
  180. },
  181. _doProcessBlock: function(d, e) {
  182. for (var b = this._hash.words, n = b[0], j = b[1], h = b[2], g = b[3], l = b[4], a = 0; 80 > a; a++) {
  183. if (16 > a) m[a] = d[e + a] | 0;
  184. else {
  185. var c = m[a - 3] ^ m[a - 8] ^ m[a - 14] ^ m[a - 16];
  186. m[a] = c << 1 | c >>> 31
  187. }
  188. c = (n << 5 | n >>> 27) + l + m[a];
  189. c = 20 > a ? c + ((j & h | ~j & g) + 1518500249) : 40 > a ? c + ((j ^ h ^ g) + 1859775393) : 60 > a ? c + ((j & h | j & g | h & g) - 1894007588) : c + ((j ^ h ^ g) - 899497514);
  190. l = g;
  191. g = h;
  192. h = j << 30 | j >>> 2;
  193. j = n;
  194. n = c
  195. }
  196. b[0] = b[0] + n | 0;
  197. b[1] = b[1] + j | 0;
  198. b[2] = b[2] + h | 0;
  199. b[3] = b[3] + g | 0;
  200. b[4] = b[4] + l | 0
  201. },
  202. _doFinalize: function() {
  203. var d = this._data,
  204. e = d.words,
  205. b = 8 * this._nDataBytes,
  206. g = 8 * d.sigBytes;
  207. e[g >>> 5] |= 128 << 24 - g % 32;
  208. e[(g + 64 >>> 9 << 4) + 14] = Math.floor(b / 4294967296);
  209. e[(g + 64 >>> 9 << 4) + 15] = b;
  210. d.sigBytes = 4 * e.length;
  211. this._process();
  212. return this._hash
  213. },
  214. clone: function() {
  215. var e = d.clone.call(this);
  216. e._hash = this._hash.clone();
  217. return e
  218. }
  219. });
  220. g.SHA1 = d._createHelper(l);
  221. g.HmacSHA1 = d._createHmacHelper(l)
  222. })();
  223. (function() {
  224. var g = CryptoJSNew,
  225. l = g.enc.Utf8;
  226. g.algo.HMAC = g.lib.Base.extend({
  227. init: function(e, d) {
  228. e = this._hasher = new e.init;
  229. "string" == typeof d && (d = l.parse(d));
  230. var g = e.blockSize,
  231. k = 4 * g;
  232. d.sigBytes > k && (d = e.finalize(d));
  233. d.clamp();
  234. for (var p = this._oKey = d.clone(), b = this._iKey = d.clone(), n = p.words, j = b.words, h = 0; h < g; h++) n[h] ^= 1549556828, j[h] ^= 909522486;
  235. p.sigBytes = b.sigBytes = k;
  236. this.reset()
  237. },
  238. reset: function() {
  239. var e = this._hasher;
  240. e.reset();
  241. e.update(this._iKey)
  242. },
  243. update: function(e) {
  244. this._hasher.update(e);
  245. return this
  246. },
  247. finalize: function(e) {
  248. var d = this._hasher;
  249. e = d.finalize(e);
  250. d.reset();
  251. return d.finalize(this._oKey.clone().concat(e))
  252. }
  253. })
  254. })();