Skip to content

Instantly share code, notes, and snippets.

@paragonie-scott
Created February 9, 2016 22:23
Show Gist options
  • Save paragonie-scott/697438aba38d8bed30d2 to your computer and use it in GitHub Desktop.
Save paragonie-scott/697438aba38d8bed30d2 to your computer and use it in GitHub Desktop.
scott@debian ~ $ php -dmbstring.func_overload=2 sammy_test.php
bool(true)
scott@debian ~ $ php sammy_test.php
bool(false)
<?php
function sammy_hash_equals($knownString, $userString)
{
$kLen = strlen($knownString);
$uLen = strlen($userString);
if ($kLen !== $uLen) {
return false;
}
$result = 0;
for ($i = 0; $i < $kLen; $i++) {
$result |= (ord($knownString[$i]) ^ ord($userString[$i]));
}
// They are only identical strings if $result is exactly 0...
return 0 === $result;
}
// 4 chars but 16 bytes
$hashA = "\xF0\x9D\x92\xB3" . "\xF0\x9D\x92\xB3" . "\xF0\x9D\x92\xB3". "\xF0\x9D\x92\xB3";
$hashB = "\xF0\x9D\x92\xB3" . "\xF0\x9D\x92\xB4" . "\xF0\x9D\x92\xB4" . "\xF0\x9D\x92\xB4";
var_dump(sammy_hash_equals($hashA, $hashB));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment