版博士V2.0程序
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

548 Zeilen
16 KiB

  1. const defaults = {
  2. ignoreUnknown: false,
  3. respectType: false,
  4. respectFunctionNames: false,
  5. respectFunctionProperties: false,
  6. unorderedObjects: true,
  7. unorderedArrays: false,
  8. unorderedSets: false
  9. };
  10. function objectHash(object, options = {}) {
  11. options = { ...defaults, ...options };
  12. const hasher = createHasher(options);
  13. hasher.dispatch(object);
  14. return hasher.toString();
  15. }
  16. function createHasher(options) {
  17. const buff = [];
  18. let context = [];
  19. const write = (str) => {
  20. buff.push(str);
  21. };
  22. return {
  23. toString() {
  24. return buff.join("");
  25. },
  26. getContext() {
  27. return context;
  28. },
  29. dispatch(value) {
  30. if (options.replacer) {
  31. value = options.replacer(value);
  32. }
  33. const type = value === null ? "null" : typeof value;
  34. return this["_" + type](value);
  35. },
  36. _object(object) {
  37. const pattern = /\[object (.*)]/i;
  38. const objString = Object.prototype.toString.call(object);
  39. const _objType = pattern.exec(objString);
  40. const objType = _objType ? _objType[1].toLowerCase() : "unknown:[" + objString.toLowerCase() + "]";
  41. let objectNumber = null;
  42. if ((objectNumber = context.indexOf(object)) >= 0) {
  43. return this.dispatch("[CIRCULAR:" + objectNumber + "]");
  44. } else {
  45. context.push(object);
  46. }
  47. if (typeof Buffer !== "undefined" && Buffer.isBuffer && Buffer.isBuffer(object)) {
  48. write("buffer:");
  49. return write(object.toString("utf8"));
  50. }
  51. if (objType !== "object" && objType !== "function" && objType !== "asyncfunction") {
  52. if (this["_" + objType]) {
  53. this["_" + objType](object);
  54. } else if (options.ignoreUnknown) {
  55. return write("[" + objType + "]");
  56. } else {
  57. throw new Error('Unknown object type "' + objType + '"');
  58. }
  59. } else {
  60. let keys = Object.keys(object);
  61. if (options.unorderedObjects) {
  62. keys = keys.sort();
  63. }
  64. if (options.respectType !== false && !isNativeFunction(object)) {
  65. keys.splice(0, 0, "prototype", "__proto__", "letructor");
  66. }
  67. if (options.excludeKeys) {
  68. keys = keys.filter(function(key) {
  69. return !options.excludeKeys(key);
  70. });
  71. }
  72. write("object:" + keys.length + ":");
  73. for (const key of keys) {
  74. this.dispatch(key);
  75. write(":");
  76. if (!options.excludeValues) {
  77. this.dispatch(object[key]);
  78. }
  79. write(",");
  80. }
  81. }
  82. },
  83. _array(arr, unordered) {
  84. unordered = typeof unordered !== "undefined" ? unordered : options.unorderedArrays !== false;
  85. write("array:" + arr.length + ":");
  86. if (!unordered || arr.length <= 1) {
  87. for (const entry of arr) {
  88. this.dispatch(entry);
  89. }
  90. return;
  91. }
  92. const contextAdditions = [];
  93. const entries = arr.map((entry) => {
  94. const hasher = createHasher(options);
  95. hasher.dispatch(entry);
  96. contextAdditions.push(hasher.getContext());
  97. return hasher.toString();
  98. });
  99. context = [...context, ...contextAdditions];
  100. entries.sort();
  101. return this._array(entries, false);
  102. },
  103. _date(date) {
  104. return write("date:" + date.toJSON());
  105. },
  106. _symbol(sym) {
  107. return write("symbol:" + sym.toString());
  108. },
  109. _error(err) {
  110. return write("error:" + err.toString());
  111. },
  112. _boolean(bool) {
  113. return write("bool:" + bool.toString());
  114. },
  115. _string(string) {
  116. write("string:" + string.length + ":");
  117. write(string.toString());
  118. },
  119. _function(fn) {
  120. write("fn:");
  121. if (isNativeFunction(fn)) {
  122. this.dispatch("[native]");
  123. } else {
  124. this.dispatch(fn.toString());
  125. }
  126. if (options.respectFunctionNames !== false) {
  127. this.dispatch("function-name:" + String(fn.name));
  128. }
  129. if (options.respectFunctionProperties) {
  130. this._object(fn);
  131. }
  132. },
  133. _number(number) {
  134. return write("number:" + number.toString());
  135. },
  136. _xml(xml) {
  137. return write("xml:" + xml.toString());
  138. },
  139. _null() {
  140. return write("Null");
  141. },
  142. _undefined() {
  143. return write("Undefined");
  144. },
  145. _regexp(regex) {
  146. return write("regex:" + regex.toString());
  147. },
  148. _uint8array(arr) {
  149. write("uint8array:");
  150. return this.dispatch(Array.prototype.slice.call(arr));
  151. },
  152. _uint8clampedarray(arr) {
  153. write("uint8clampedarray:");
  154. return this.dispatch(Array.prototype.slice.call(arr));
  155. },
  156. _int8array(arr) {
  157. write("int8array:");
  158. return this.dispatch(Array.prototype.slice.call(arr));
  159. },
  160. _uint16array(arr) {
  161. write("uint16array:");
  162. return this.dispatch(Array.prototype.slice.call(arr));
  163. },
  164. _int16array(arr) {
  165. write("int16array:");
  166. return this.dispatch(Array.prototype.slice.call(arr));
  167. },
  168. _uint32array(arr) {
  169. write("uint32array:");
  170. return this.dispatch(Array.prototype.slice.call(arr));
  171. },
  172. _int32array(arr) {
  173. write("int32array:");
  174. return this.dispatch(Array.prototype.slice.call(arr));
  175. },
  176. _float32array(arr) {
  177. write("float32array:");
  178. return this.dispatch(Array.prototype.slice.call(arr));
  179. },
  180. _float64array(arr) {
  181. write("float64array:");
  182. return this.dispatch(Array.prototype.slice.call(arr));
  183. },
  184. _arraybuffer(arr) {
  185. write("arraybuffer:");
  186. return this.dispatch(new Uint8Array(arr));
  187. },
  188. _url(url) {
  189. return write("url:" + url.toString());
  190. },
  191. _map(map) {
  192. write("map:");
  193. const arr = [...map];
  194. return this._array(arr, options.unorderedSets !== false);
  195. },
  196. _set(set) {
  197. write("set:");
  198. const arr = [...set];
  199. return this._array(arr, options.unorderedSets !== false);
  200. },
  201. _file(file) {
  202. write("file:");
  203. return this.dispatch([file.name, file.size, file.type, file.lastModfied]);
  204. },
  205. _blob() {
  206. if (options.ignoreUnknown) {
  207. return write("[blob]");
  208. }
  209. throw new Error('Hashing Blob objects is currently not supported\nUse "options.replacer" or "options.ignoreUnknown"\n');
  210. },
  211. _domwindow() {
  212. return write("domwindow");
  213. },
  214. _bigint(number) {
  215. return write("bigint:" + number.toString());
  216. },
  217. _process() {
  218. return write("process");
  219. },
  220. _timer() {
  221. return write("timer");
  222. },
  223. _pipe() {
  224. return write("pipe");
  225. },
  226. _tcp() {
  227. return write("tcp");
  228. },
  229. _udp() {
  230. return write("udp");
  231. },
  232. _tty() {
  233. return write("tty");
  234. },
  235. _statwatcher() {
  236. return write("statwatcher");
  237. },
  238. _securecontext() {
  239. return write("securecontext");
  240. },
  241. _connection() {
  242. return write("connection");
  243. },
  244. _zlib() {
  245. return write("zlib");
  246. },
  247. _context() {
  248. return write("context");
  249. },
  250. _nodescript() {
  251. return write("nodescript");
  252. },
  253. _httpparser() {
  254. return write("httpparser");
  255. },
  256. _dataview() {
  257. return write("dataview");
  258. },
  259. _signal() {
  260. return write("signal");
  261. },
  262. _fsevent() {
  263. return write("fsevent");
  264. },
  265. _tlswrap() {
  266. return write("tlswrap");
  267. }
  268. };
  269. }
  270. function isNativeFunction(f) {
  271. if (typeof f !== "function") {
  272. return false;
  273. }
  274. const exp = /^function\s+\w*\s*\(\s*\)\s*{\s+\[native code]\s+}$/i;
  275. return exp.exec(Function.prototype.toString.call(f)) != null;
  276. }
  277. class WordArray {
  278. constructor(words, sigBytes) {
  279. words = this.words = words || [];
  280. this.sigBytes = sigBytes !== void 0 ? sigBytes : words.length * 4;
  281. }
  282. toString(encoder) {
  283. return (encoder || Hex).stringify(this);
  284. }
  285. concat(wordArray) {
  286. this.clamp();
  287. if (this.sigBytes % 4) {
  288. for (let i = 0; i < wordArray.sigBytes; i++) {
  289. const thatByte = wordArray.words[i >>> 2] >>> 24 - i % 4 * 8 & 255;
  290. this.words[this.sigBytes + i >>> 2] |= thatByte << 24 - (this.sigBytes + i) % 4 * 8;
  291. }
  292. } else {
  293. for (let j = 0; j < wordArray.sigBytes; j += 4) {
  294. this.words[this.sigBytes + j >>> 2] = wordArray.words[j >>> 2];
  295. }
  296. }
  297. this.sigBytes += wordArray.sigBytes;
  298. return this;
  299. }
  300. clamp() {
  301. this.words[this.sigBytes >>> 2] &= 4294967295 << 32 - this.sigBytes % 4 * 8;
  302. this.words.length = Math.ceil(this.sigBytes / 4);
  303. }
  304. clone() {
  305. return new WordArray([...this.words]);
  306. }
  307. }
  308. const Hex = {
  309. stringify(wordArray) {
  310. const hexChars = [];
  311. for (let i = 0; i < wordArray.sigBytes; i++) {
  312. const bite = wordArray.words[i >>> 2] >>> 24 - i % 4 * 8 & 255;
  313. hexChars.push(
  314. (bite >>> 4).toString(16),
  315. (bite & 15).toString(16)
  316. );
  317. }
  318. return hexChars.join("");
  319. }
  320. };
  321. const Base64 = {
  322. stringify(wordArray) {
  323. const keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  324. const base64Chars = [];
  325. for (let i = 0; i < wordArray.sigBytes; i += 3) {
  326. const byte1 = wordArray.words[i >>> 2] >>> 24 - i % 4 * 8 & 255;
  327. const byte2 = wordArray.words[i + 1 >>> 2] >>> 24 - (i + 1) % 4 * 8 & 255;
  328. const byte3 = wordArray.words[i + 2 >>> 2] >>> 24 - (i + 2) % 4 * 8 & 255;
  329. const triplet = byte1 << 16 | byte2 << 8 | byte3;
  330. for (let j = 0; j < 4 && i * 8 + j * 6 < wordArray.sigBytes * 8; j++) {
  331. base64Chars.push(keyStr.charAt(triplet >>> 6 * (3 - j) & 63));
  332. }
  333. }
  334. return base64Chars.join("");
  335. }
  336. };
  337. const Latin1 = {
  338. parse(latin1Str) {
  339. const latin1StrLength = latin1Str.length;
  340. const words = [];
  341. for (let i = 0; i < latin1StrLength; i++) {
  342. words[i >>> 2] |= (latin1Str.charCodeAt(i) & 255) << 24 - i % 4 * 8;
  343. }
  344. return new WordArray(words, latin1StrLength);
  345. }
  346. };
  347. const Utf8 = {
  348. parse(utf8Str) {
  349. return Latin1.parse(unescape(encodeURIComponent(utf8Str)));
  350. }
  351. };
  352. class BufferedBlockAlgorithm {
  353. constructor() {
  354. this._minBufferSize = 0;
  355. this.blockSize = 512 / 32;
  356. this.reset();
  357. }
  358. reset() {
  359. this._data = new WordArray();
  360. this._nDataBytes = 0;
  361. }
  362. _append(data) {
  363. if (typeof data === "string") {
  364. data = Utf8.parse(data);
  365. }
  366. this._data.concat(data);
  367. this._nDataBytes += data.sigBytes;
  368. }
  369. _doProcessBlock(_dataWords, _offset) {
  370. }
  371. _process(doFlush) {
  372. let processedWords;
  373. let nBlocksReady = this._data.sigBytes / (this.blockSize * 4);
  374. if (doFlush) {
  375. nBlocksReady = Math.ceil(nBlocksReady);
  376. } else {
  377. nBlocksReady = Math.max((nBlocksReady | 0) - this._minBufferSize, 0);
  378. }
  379. const nWordsReady = nBlocksReady * this.blockSize;
  380. const nBytesReady = Math.min(nWordsReady * 4, this._data.sigBytes);
  381. if (nWordsReady) {
  382. for (let offset = 0; offset < nWordsReady; offset += this.blockSize) {
  383. this._doProcessBlock(this._data.words, offset);
  384. }
  385. processedWords = this._data.words.splice(0, nWordsReady);
  386. this._data.sigBytes -= nBytesReady;
  387. }
  388. return new WordArray(processedWords, nBytesReady);
  389. }
  390. }
  391. class Hasher extends BufferedBlockAlgorithm {
  392. update(messageUpdate) {
  393. this._append(messageUpdate);
  394. this._process();
  395. return this;
  396. }
  397. finalize(messageUpdate) {
  398. if (messageUpdate) {
  399. this._append(messageUpdate);
  400. }
  401. }
  402. }
  403. const H = [1779033703, -1150833019, 1013904242, -1521486534, 1359893119, -1694144372, 528734635, 1541459225];
  404. const K = [1116352408, 1899447441, -1245643825, -373957723, 961987163, 1508970993, -1841331548, -1424204075, -670586216, 310598401, 607225278, 1426881987, 1925078388, -2132889090, -1680079193, -1046744716, -459576895, -272742522, 264347078, 604807628, 770255983, 1249150122, 1555081692, 1996064986, -1740746414, -1473132947, -1341970488, -1084653625, -958395405, -710438585, 113926993, 338241895, 666307205, 773529912, 1294757372, 1396182291, 1695183700, 1986661051, -2117940946, -1838011259, -1564481375, -1474664885, -1035236496, -949202525, -778901479, -694614492, -200395387, 275423344, 430227734, 506948616, 659060556, 883997877, 958139571, 1322822218, 1537002063, 1747873779, 1955562222, 2024104815, -2067236844, -1933114872, -1866530822, -1538233109, -1090935817, -965641998];
  405. const W = [];
  406. class SHA256 extends Hasher {
  407. constructor() {
  408. super();
  409. this.reset();
  410. }
  411. reset() {
  412. super.reset();
  413. this._hash = new WordArray([...H]);
  414. }
  415. _doProcessBlock(M, offset) {
  416. const H2 = this._hash.words;
  417. let a = H2[0];
  418. let b = H2[1];
  419. let c = H2[2];
  420. let d = H2[3];
  421. let e = H2[4];
  422. let f = H2[5];
  423. let g = H2[6];
  424. let h = H2[7];
  425. for (let i = 0; i < 64; i++) {
  426. if (i < 16) {
  427. W[i] = M[offset + i] | 0;
  428. } else {
  429. const gamma0x = W[i - 15];
  430. const gamma0 = (gamma0x << 25 | gamma0x >>> 7) ^ (gamma0x << 14 | gamma0x >>> 18) ^ gamma0x >>> 3;
  431. const gamma1x = W[i - 2];
  432. const gamma1 = (gamma1x << 15 | gamma1x >>> 17) ^ (gamma1x << 13 | gamma1x >>> 19) ^ gamma1x >>> 10;
  433. W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16];
  434. }
  435. const ch = e & f ^ ~e & g;
  436. const maj = a & b ^ a & c ^ b & c;
  437. const sigma0 = (a << 30 | a >>> 2) ^ (a << 19 | a >>> 13) ^ (a << 10 | a >>> 22);
  438. const sigma1 = (e << 26 | e >>> 6) ^ (e << 21 | e >>> 11) ^ (e << 7 | e >>> 25);
  439. const t1 = h + sigma1 + ch + K[i] + W[i];
  440. const t2 = sigma0 + maj;
  441. h = g;
  442. g = f;
  443. f = e;
  444. e = d + t1 | 0;
  445. d = c;
  446. c = b;
  447. b = a;
  448. a = t1 + t2 | 0;
  449. }
  450. H2[0] = H2[0] + a | 0;
  451. H2[1] = H2[1] + b | 0;
  452. H2[2] = H2[2] + c | 0;
  453. H2[3] = H2[3] + d | 0;
  454. H2[4] = H2[4] + e | 0;
  455. H2[5] = H2[5] + f | 0;
  456. H2[6] = H2[6] + g | 0;
  457. H2[7] = H2[7] + h | 0;
  458. }
  459. finalize(messageUpdate) {
  460. super.finalize(messageUpdate);
  461. const nBitsTotal = this._nDataBytes * 8;
  462. const nBitsLeft = this._data.sigBytes * 8;
  463. this._data.words[nBitsLeft >>> 5] |= 128 << 24 - nBitsLeft % 32;
  464. this._data.words[(nBitsLeft + 64 >>> 9 << 4) + 14] = Math.floor(nBitsTotal / 4294967296);
  465. this._data.words[(nBitsLeft + 64 >>> 9 << 4) + 15] = nBitsTotal;
  466. this._data.sigBytes = this._data.words.length * 4;
  467. this._process();
  468. return this._hash;
  469. }
  470. }
  471. function sha256(message) {
  472. return new SHA256().finalize(message).toString();
  473. }
  474. function sha256base64(message) {
  475. return new SHA256().finalize(message).toString(Base64);
  476. }
  477. function hash(object, options = {}) {
  478. const hashed = typeof object === "string" ? object : objectHash(object, options);
  479. return sha256base64(hashed).slice(0, 10);
  480. }
  481. function murmurHash(key, seed = 0) {
  482. if (typeof key === "string") {
  483. key = createBuffer(key);
  484. }
  485. let i = 0;
  486. let h1 = seed;
  487. let k1;
  488. let h1b;
  489. const remainder = key.length & 3;
  490. const bytes = key.length - remainder;
  491. const c1 = 3432918353;
  492. const c2 = 461845907;
  493. while (i < bytes) {
  494. k1 = key[i] & 255 | (key[++i] & 255) << 8 | (key[++i] & 255) << 16 | (key[++i] & 255) << 24;
  495. ++i;
  496. k1 = (k1 & 65535) * c1 + (((k1 >>> 16) * c1 & 65535) << 16) & 4294967295;
  497. k1 = k1 << 15 | k1 >>> 17;
  498. k1 = (k1 & 65535) * c2 + (((k1 >>> 16) * c2 & 65535) << 16) & 4294967295;
  499. h1 ^= k1;
  500. h1 = h1 << 13 | h1 >>> 19;
  501. h1b = (h1 & 65535) * 5 + (((h1 >>> 16) * 5 & 65535) << 16) & 4294967295;
  502. h1 = (h1b & 65535) + 27492 + (((h1b >>> 16) + 58964 & 65535) << 16);
  503. }
  504. k1 = 0;
  505. switch (remainder) {
  506. case 3:
  507. k1 ^= (key[i + 2] & 255) << 16;
  508. break;
  509. case 2:
  510. k1 ^= (key[i + 1] & 255) << 8;
  511. break;
  512. case 1:
  513. k1 ^= key[i] & 255;
  514. k1 = (k1 & 65535) * c1 + (((k1 >>> 16) * c1 & 65535) << 16) & 4294967295;
  515. k1 = k1 << 15 | k1 >>> 17;
  516. k1 = (k1 & 65535) * c2 + (((k1 >>> 16) * c2 & 65535) << 16) & 4294967295;
  517. h1 ^= k1;
  518. }
  519. h1 ^= key.length;
  520. h1 ^= h1 >>> 16;
  521. h1 = (h1 & 65535) * 2246822507 + (((h1 >>> 16) * 2246822507 & 65535) << 16) & 4294967295;
  522. h1 ^= h1 >>> 13;
  523. h1 = (h1 & 65535) * 3266489909 + (((h1 >>> 16) * 3266489909 & 65535) << 16) & 4294967295;
  524. h1 ^= h1 >>> 16;
  525. return h1 >>> 0;
  526. }
  527. function createBuffer(val) {
  528. return new TextEncoder().encode(val);
  529. }
  530. function isEqual(object1, object2, hashOptions = {}) {
  531. if (object1 === object2) {
  532. return true;
  533. }
  534. if (objectHash(object1, hashOptions) === objectHash(object2, hashOptions)) {
  535. return true;
  536. }
  537. return false;
  538. }
  539. /* Injected with object hook! */
  540. export { hash, isEqual, murmurHash, objectHash, sha256 };