Created
June 23, 2016 06:18
-
-
Save acyuta/228b33d8e0396ba89b1e74ba55c8d81c to your computer and use it in GitHub Desktop.
Simple String Hash Function On PHP
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
<?php | |
function e($str, $modul) { | |
$intLength = strlen($str) / 4; | |
$sum = 0; | |
$MUL = 256; | |
for($j = 0; $j < $intLength; $j++) { | |
$c = substr($str, $j * 4, 4); | |
$mult = 1; | |
for ($k = 0; $k < strlen($c); $k++) { | |
$sum += ord($c[$k]) * $mult; | |
$mult *= $MUL; | |
} | |
} | |
$c = substr($str, $intLength * 4); | |
$mult = 1; | |
for ($k = 0; $k < strlen($c); $k++) { | |
$sum += ord($c[$k]) * $mult; | |
$mult *= $MUL; | |
} | |
return $sum % $modul; | |
} | |
/* FUNCTION USAGE */ | |
// Any prime | |
$prime = 2147483123; | |
$s = "Hello bro. This is simple hash string function. Yo. RTFM X_X"; | |
echo "Hash(" . $s . ") = ". e($s, $prime); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment