8000 Use a constant-time secure comparison for passwords by glittershark · Pull Request #119 · bcrypt-ruby/bcrypt-ruby · GitHub
[go: up one dir, main page]

Skip to content

Use a constant-time secure comparison for passwords #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/bcrypt/password.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,17 @@ def initialize(raw_hash)
end
end

# Compares a potential secret against the hash. Returns true if the secret is the original secret, false otherwise.
# Compares a potential secret against the hash using a constant-time comparison function. Returns true if the secret
# is the original secret, false otherwise.
def ==(secret)
super(BCrypt::Engine.hash_secret(secret, @salt))
hash = BCrypt::Engine.hash_secret(secret, @salt)

return false if hash.strip.empty? || strip.empty? || hash.bytesize != bytesize
l = hash.unpack "C#{hash.bytesize}"

res = 0
each_byte { |byte| res |= byte ^ l.shift }
res == 0
end
alias_method :is_password?, :==

Expand Down
0