Skip to content

Instantly share code, notes, and snippets.

@lastguest
Last active March 20, 2024 14:11
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lastguest/235cbf7d9f5cf97abb79 to your computer and use it in GitHub Desktop.
Save lastguest/235cbf7d9f5cf97abb79 to your computer and use it in GitHub Desktop.
Bob Jenkins' One-At-A-Time hashing algorithm.
<?php
// Bob Jenkins' One-At-A-Time hashing algorithm.
function jenkins_hash($key) {
$key = (string)$key;
$len = strlen($key);
for($hash = $i = 0; $i < $len; ++$i) {
$hash += ord($key[$i]);
$hash += ($hash << 10);
$hash ^= ($hash >> 6);
}
$hash += ($hash << 3);
$hash ^= ($hash >> 11);
$hash += ($hash << 15);
return str_pad(dechex($hash),16,0,STR_PAD_LEFT);
}
@noskill24
Copy link

This COPY-PAST C/C++ implementation is wrong for PHP.
Please check sample tests from Wikipedia:
https://en.wikipedia.org/wiki/Jenkins_hash_function

TESTS:

C / C++:
one_at_a_time("The quick brown fox jumps over the lazy dog", 43)
0x519e91f5 - CORRECT!

PHP (code above):
jenkins_hash("The quick brown fox jumps over the lazy dog")
0x31c25ec1 - INCORRECT!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment