Skip to content

Instantly share code, notes, and snippets.

@insthync
Created November 18, 2021 09:26
Show Gist options
  • Save insthync/f8a24fa94021a37fb7e0a1c6f07b9777 to your computer and use it in GitHub Desktop.
Save insthync/f8a24fa94021a37fb7e0a1c6f07b9777 to your computer and use it in GitHub Desktop.
GetStableHash (The same algorithm with what MMORPG KIT does)
<?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));
}
@MrBeatngU
Copy link

Bro you're awesome. Thank you so much. Just saved me a headache from hell

@shubhank008
Copy link

@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

@insthync
Copy link
Author

@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