Skip to content

Commit 0d99733

Browse files
committed
Use constant-time comparison for sensitive hashes
Variable time comparison leaves you vulnerable to timing attacks
1 parent fd41f8a commit 0d99733

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

security/constant-time-comparison.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Constant time comparison
2+
3+
Using `==` to compare sensitive hashes leaves you vulnerable to timing attacks.
4+
This is because `==` returns `false` as soon as it finds two characters that
5+
don't match. An attacker can make many requests with different values and
6+
compare times to figure out how many characters were correct (the shorter the
7+
response, the fewer correct characters).
8+
9+
The solution to this problem is to use a constant-time comparison algorithm.
10+
This ensures that the method will always take the same amount of time,
11+
regardless of how similar the hashes are. In Ruby, you can use
12+
[`Rack::Utils.secure_compare`] or
13+
[`ActiveSupport::SecurityUtils.secure_compare`].
14+
15+
For more information, check out this excellent [blog post].
16+
17+
[`Rack::Utils.secure_compare`]:
18+
http://www.rubydoc.info/github/rack/rack/Rack/Utils#secure_compare-class_method
19+
[`ActiveSupport::SecurityUtils.secure_compare`]:
20+
http://api.rubyonrails.org/classes/ActiveSupport/SecurityUtils.html#method-c-secure_compare
21+
[blog post]: http://codahale.com/a-lesson-in-timing-attacks/

0 commit comments

Comments
 (0)