From 78bcace8ed7a1c867f273ef286bca84ce8539615 Mon Sep 17 00:00:00 2001
From: afonya2 <adamhir@freemail.hu>
Date: Thu, 29 May 2025 14:17:08 +0200
Subject: [PATCH] fix a potential freeze

---
 IGA.ts             | 24 ++++++++++++++++++------
 collision_test2.ts |  2 +-
 2 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/IGA.ts b/IGA.ts
index 8852fe5..a681006 100644
--- a/IGA.ts
+++ b/IGA.ts
@@ -126,7 +126,7 @@ function checksum(input: string) {
     return state.join("")
 }
 
-function hashInternal(toHash: number[], len: number, salt: number): number[] {
+function hashInternal(toHash: number[], len: number, salt: number, first: boolean): number[] {
     let complete: number[] = []
     
     while (toHash.length > 0) {
@@ -142,7 +142,15 @@ function hashInternal(toHash: number[], len: number, salt: number): number[] {
         toHash = toHash.slice(len, toHash.length)
         let curr = 0
         for (let i = 0; i < inp.length; i++) {
-            selected[curr] = (selected[curr] + inp[i] + salt) % 65536
+            let temp = selected[curr] + inp[i] + salt
+            while (temp >= 65536) {
+                let sub = selected[curr-1] ? selected[curr-1] : selected[selected.length-1]
+                if (first || sub == 0) {
+                    sub = 2411
+                }
+                temp -= sub
+            }
+            selected[curr] = temp
             curr++
             if (curr >= len) {
                 curr = 0
@@ -155,12 +163,16 @@ function hashInternal(toHash: number[], len: number, salt: number): number[] {
 
     return complete
 }
-function hashInternal2(toHash: number[], salt: number): number[] {
+function hashInternal2(toHash: number[], salt: number, first: boolean): number[] {
     let state = copy(toHash, true)
     for (let i = 0; i < toHash.length; i++) {
         let temp = state[i] + (state[i+1] ? state[i+1] : state[0]) + salt
         while (temp >= 65536) {
-            temp -= state[i-1] ? state[i-1] : state[state.length-1]
+            let sub = state[i-1] ? state[i-1] : state[state.length-1]
+            if (first || sub == 0) {
+                sub = 2411
+            }
+            temp -= sub
         }
         toHash[i] = temp
     }
@@ -201,8 +213,8 @@ function hash(input: string, salt: number = 0, len: number = 32, seed?: number):
         toHash.push(padding.generator.next())
     }
     for (let it = 0; it < iterations; it++) {
-        toHash = hashInternal(toHash, len, salt)
-        toHash = hashInternal2(toHash, salt)
+        toHash = hashInternal(toHash, len, salt, it == 0)
+        toHash = hashInternal2(toHash, salt, it == 0)
     }
     const pBin = fixbin(dec2bin(padding.seed), 16)
     let dataBin = ""
diff --git a/collision_test2.ts b/collision_test2.ts
index 251b40d..4c0fc31 100644
--- a/collision_test2.ts
+++ b/collision_test2.ts
@@ -58,7 +58,7 @@ async function main() {
         } else {
             failCount++
             if (failCount > 1000) {
-                console.log("Finding a qunique string took too long, exiting...")
+                console.log("Finding a unique string took too long, exiting...")
                 process.exit(0)
             }
         }