Could someone please tell me what a string comparison timing attack is in simple terms? I have Googled this, but all the explanations are very technical. Also, is this attack an better than a brute force attack? Please correct me if I am wrong, but I believe this attack is used to crack passwords.

1 Answer 1


When checking strings for equality you need to check that every character matches. Most programming languages will take a short-cut and return False - or Not Equal - as soon as they find a single character that doesn't match. For example,

str1 = "1111111111111111"
str2 = "1101111111111111"

You know that they are not equal as soon as you hit that 0, why would you keep checking?

This is super exploitable. Imagine that I'm trying to crack a password (or a hash, or a MAC tag, or anything that needs to be compared for equality), knowing that the server will do a lazy string comparison somewhere inside, I might get the following timing results (completely made up):

Tried "aaaaaaaa", it took 0.2 ms
Tried "bbbbbbbb", it took 0.2 ms
Tried "cccccccc", it took 0.4 ms

Cool, now I know that the first letter is 'c' because it took longer. Now I can do the same trick with "caaaaaaa", "cbbbbbbb", etc. Cracking one letter at a time is HUGELY faster than cracking the whole password at a time.

This "lazy" string comparison is generally a good thing for programmers because it makes things run faster, but it's bad for security. Secure string comparisons will check all the way to the end, even if it found a difference so that there's never a timing difference.

  • 1
    Amending this, it is the same case with any kind of comparison where determining the result takes any kind of a pipeline and returns result on first failure of a condition - for example, when checking credentials (user,pass) in sequence !user exists ? return : check pass (+N time)
    – Matiss
    Jul 22, 2015 at 0:18
  • Is the difference really measurable though? Would the time distance even be measurable? Computers are so fast these days, it's hard to believe the time difference is very big....
    – APCoding
    Jul 22, 2015 at 2:37
  • It's definitely a hard attack to pull off because the timing difference you're looking for is so small. You have to hit the server with the same (user, pass) like 100 times to average out the network lag and stuff, but it still can save you time over a brute-force. Jul 22, 2015 at 3:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.