Skip to content

Instantly share code, notes, and snippets.

@davestevens
Created January 21, 2015 23:51
Show Gist options
  • Save davestevens/a9187de713a32cb005b1 to your computer and use it in GitHub Desktop.
Save davestevens/a9187de713a32cb005b1 to your computer and use it in GitHub Desktop.
BCrypt Issue
#!/usr/bin/env ruby
require "uri"
require "bcrypt"
secret = "abcd/1234"
encrypted_password = BCrypt::Password.create(secret)
encoded_secret = URI.encode_www_form_component(secret)
password = URI.decode(encoded_secret)
# Interacting with `password` at all affects it.
# In Devise a presence check is performed.
# A `puts` will also cause a breakage.
puts password
bcrypt = BCrypt::Password.new(encrypted_password)
# Devise adds pepper to the password before hashing it
# this pepper can be nil and this results in a corrupt string when it gets to
# the mri layer which BCrypt uses.
password_with_nil = BCrypt::Engine.hash_secret("#{password}#{nil}", bcrypt.salt)
if password_with_nil == encrypted_password
puts "Password with nil was correct"
else
puts "Password with nil was incorrect"
end
password = BCrypt::Engine.hash_secret(password, bcrypt.salt)
if password == encrypted_password
puts "Password was correct"
else
puts "Password was incorrect"
end
source "https://rubygems.org"
gem "bcrypt"
GEM
remote: https://rubygems.org/
specs:
bcrypt (3.1.9)
PLATFORMS
ruby
DEPENDENCIES
bcrypt
@davestevens
Copy link
Author

Commenting out line 15 of bcrypt_issue.rb results in both hashes passing.
This is with Ruby 2.2.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment