File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change
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/
You can’t perform that action at this time.
0 commit comments