Constant time string comparisons in ruby
require 'same_time'
SameTime.equal?('encrypted_value', 'encrypted_value') # => true
SameTime.equal?('encrypted_value', 'mismatched_encrypted_value') # => false
SameTime.equal?(' ', ' ') # => false
SameTime.equal?(' ', ' ', allow_blank: true) # => trueThe algorithm:
def equal?(a, b, opts = {})
return false if a.bytesize != b.bytesize ||
(!opts[:allow_blank] && a !~ /[^[:space:]]/)
y = b.bytes
a.bytes.inject(0) { |r,x| r ^ (x ^ y.shift) } == 0
end
Add this line to your application's Gemfile:
gem 'same_time'
And then execute:
$ bundle
- Fork it ( https://github.com/[my-github-username]/constant_time_compare/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request