Created
February 5, 2024 01:31
-
-
Save wulliy/f9da8ece87a05c6cfcecb39ea3584e62 to your computer and use it in GitHub Desktop.
hashing function, but made to work in Scratch.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// wuilly | |
// 11/6/2023 | |
function hash(str, salt) { | |
// TODO: make this hashing function more collision-resistant | |
if (salt != null) { | |
str = `${salt}${str}` | |
} | |
if (str == null || str == "") { | |
return "" | |
} | |
const soup = " `-=[]\\;\',./!@#$%^&*()_+{}|:\"<>?abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNOPQRSTUVWXYZ0123456789" | |
const soup_len = soup.length | |
const r = 1234 // Math.floor((3.141592653589793 + 32) ** 2) | |
let hash = [] | |
for (const a of str) { | |
let result = null | |
for (const b of str) { | |
const len = hash.length | |
// all of the string comparisons are the real magic behind this hashing function :3 | |
result = (2 - (a > b) + 0) % 2 | |
for (const c of soup) { | |
if (a != b) { | |
result += (b > c < a) | |
} else { | |
result += (c < a) | |
} | |
} | |
const val = hash[len-1] | |
if (len > 0) { | |
hash[len-1] = ((r * val) + (0-(val % 2)*result)) % soup_len | |
if (!result && len > 1) { | |
const tmp = hash[len-2] | |
hash[len-1] = tmp | |
hash[len-2] = (val + r % len) % soup_len | |
} | |
} | |
} | |
if (result != null) { | |
hash.push(result) | |
} else { | |
hash.push((hash.length + r) % soup_len) | |
} | |
} | |
return hash.map(idx => soup[idx]).join("") | |
} | |
const salt = "SALTSALTSALT" | |
hash("@!", salt) | |
hash("@2", salt) | |
hash("@3", salt) | |
hash("asdfb", salt) | |
hash("asdfgb", salt) | |
hash("asdfg", salt) | |
hash("chicken butt") | |
hash("chicken butt", salt) | |
hash("The quick brown fox jumps over the lazy dog.", salt) | |
hash("The quick brown fox jumps over the lazy dog!", salt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment