Forked from insthync/gist:f8a24fa94021a37fb7e0a1c6f07b9777
Created
December 23, 2021 10:25
-
-
Save shubhank008/298ec079f44a16d26d6f09469113a5c2 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)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment