validate-hash-collision-test.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /* eslint-disable no-await-in-loop -- no concurrent */
  2. import { fdir as Fdir } from 'fdir';
  3. import { OUTPUT_SURGE_DIR } from './constants/dir';
  4. import path from 'node:path';
  5. import { readFileIntoProcessedArray } from './lib/fetch-text-by-line';
  6. import { xxhash3 } from 'hash-wasm';
  7. (async () => {
  8. const hashMap = new Map<string, Set<string>>();
  9. const runHash = async (inputs: string[]) => {
  10. for (const input of inputs) {
  11. const hash = await xxhash3(input);
  12. if (!hashMap.has(hash)) {
  13. hashMap.set(hash, new Set());
  14. }
  15. hashMap.get(hash)!.add(input);
  16. }
  17. };
  18. const files = await new Fdir()
  19. .withRelativePaths()
  20. .crawl(OUTPUT_SURGE_DIR)
  21. .withPromise();
  22. for (const file of files) {
  23. const fullpath = path.join(OUTPUT_SURGE_DIR, file);
  24. if (file.startsWith('domainset' + path.sep)) {
  25. await runHash((await readFileIntoProcessedArray(fullpath)).map(i => (i[0] === '.' ? i.slice(1) : i)));
  26. } else if (file.startsWith('non_ip' + path.sep)) {
  27. await runHash((await readFileIntoProcessedArray(fullpath)).map(i => i.split(',')[1]));
  28. }
  29. }
  30. console.log(hashMap.size);
  31. let collision = 0;
  32. hashMap.forEach((v, k) => {
  33. if (v.size > 1) {
  34. collision++;
  35. console.log(k, '=>', v);
  36. }
  37. });
  38. if (collision === 0) {
  39. console.log(hashMap);
  40. }
  41. })();