Skip to content

Instantly share code, notes, and snippets.

@freekrai
Created January 20, 2014 19:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freekrai/8527310 to your computer and use it in GitHub Desktop.
Save freekrai/8527310 to your computer and use it in GitHub Desktop.
DJB2 Hash in PHP
<?php
function hash_djb2($str){
$hash = 5381;
$length = strlen($str);
for($i = 0; $i &lt; $length; $i++) {
$hash = ( ($hash << 5) + $hash ) + $str[$i];
}
return ($hash & 0xFFFFFFFF);
}
// how to call the function:
$hash = hash_djb2( "bacon" );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment