IGA/collision_test2.ts
2025-05-29 14:17:08 +02:00

76 lines
2.2 KiB
TypeScript

import IGA from "./IGA"
import fs from "fs"
type Collision = {
hash: string
old: string
new: string
randomPadding: boolean
}
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms))
}
const len = 64
const alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let seen = {}
let rawSeen = {}
let collisions: Collision[] = []
let counter = 0
let failCount = 0
const startTime = Date.now()
async function main() {
while (true) {
let str = ""
for (let i = 0; i < len; i++) {
str += alphabet[Math.floor(Math.random() * (alphabet.length - 1))]
}
let h = ""
if (rawSeen[str] == undefined) {
failCount = 0
let hash1 = IGA.hash(str)
h = hash1
let hash2 = IGA.hash(str, 0, 32, 32768)
if (seen[hash1] != undefined) {
collisions.push({
hash: hash1,
old: seen[hash1],
new: str,
randomPadding: true
})
} else {
seen[hash1] = str
}
if (seen[hash2] != undefined) {
if (!hash1.startsWith("8000")) {
collisions.push({
hash: hash2,
old: seen[hash2],
new: str,
randomPadding: false
})
}
} else {
seen[hash2] = str
}
rawSeen[str] = true
} else {
failCount++
if (failCount > 1000) {
console.log("Finding a unique string took too long, exiting...")
process.exit(0)
}
}
if (counter % 1000 == 0) {
let timeTook = Math.floor((Date.now() - startTime) / 100) / 10
console.log(`Last string: ${str}, Last hash: ${h}, Collisions: ${collisions.length}, Time: ${timeTook}s, Speed: ${Math.floor(counter/timeTook*10)/10} hash/s`)
await sleep(1)
}
if (counter % 10000 == 0) {
fs.writeFileSync("collisions.json", JSON.stringify(collisions))
}
counter++
}
}
main()