cryptojs.js 6.7 KB

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