Created
November 18, 2021 09:26
-
-
Save insthync/f8a24fa94021a37fb7e0a1c6f07b9777 to your computer and use it in GitHub Desktop.
GetStableHash (The same algorithm with what MMORPG KIT does)
This file contains hidden or 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
<?php | |
function getUncheckedInt32($r) { | |
$r = $r & 0xFFFFFFFF; | |
if ($r & 0x80000000) | |
{ | |
$r = $r & ~0x80000000; | |
$r = -2147483648 + $r; | |
} | |
return $r; | |
} | |
function getStableHash($str) { | |
$id = str_split($str); | |
$hash1 = (int)5381; | |
$hash2 = $hash1; | |
$len = count($id); | |
$end = 0; | |
for ($i = 0; $i < $len && ord($id[$i]) != $end; $i += 2) | |
{ | |
$char = ord($id[$i]); | |
$hash1 = getUncheckedInt32(getUncheckedInt32(getUncheckedInt32($hash1 << 5) + $hash1) ^ $char); | |
$char = ord($id[$i + 1]); | |
if ($i == $len - 1 || $char == $end) | |
break; | |
$hash2 = getUncheckedInt32(getUncheckedInt32(getUncheckedInt32($hash2 << 5) + $hash2) ^ $char); | |
} | |
return getUncheckedInt32($hash1 + getUncheckedInt32($hash2 * 1566083941)); | |
} |
@insthync can you create a reverse version of the above code as well ? Where we input it the hash and it gives us the string
@shubhank008 Can't do that, can only be hashed to int only, if you want to do it you may store string value as dictionary (Dictionary<int, string>)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bro you're awesome. Thank you so much. Just saved me a headache from hell