Skip to content

Instantly share code, notes, and snippets.

@acyuta
Created June 23, 2016 06:18
Show Gist options
  • Save acyuta/228b33d8e0396ba89b1e74ba55c8d81c to your computer and use it in GitHub Desktop.
Save acyuta/228b33d8e0396ba89b1e74ba55c8d81c to your computer and use it in GitHub Desktop.
Simple String Hash Function On PHP
<?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