Created
July 5, 2013 19:43
-
-
Save Zegnat/5936767 to your computer and use it in GitHub Desktop.
For secure and slow string comparisons. This helps against timing attacks on secret strings.
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 slow_compare($str1, $str2, $length = null) { | |
$out = ($str1_len = strlen($str1 .= chr(0))) - ($str2_len = strlen($str2 .= chr(0))); | |
if (!is_int($length)) $length = $str1_len; | |
for ($i = 0; $i < $length; $i++) | |
$out |= ord($str1[$i % $str1_len]) ^ ord($str2[$i % $str2_len]); | |
return 0 === $out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment