Skip to content

Commit

Permalink
Changed BCrypt::Engine.hash to BCrypt::Engine.hash_secret to avoid Me…
Browse files Browse the repository at this point in the history
…rb sorting issues. Thanks to Lee Pope for

the patch, including specs!
  • Loading branch information
codahale committed May 8, 2008
1 parent 17a8701 commit 1696fcd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@
- Fixed example code in the README [Winson]
- Fixed Solaris compatibility [Jeremy LaTrasse, Twitter crew]

SVN Blah
- Made exception classes descend from StandardError, not Exception [Dan42]
HEAD
- Made exception classes descend from StandardError, not Exception [Dan42]
- Changed BCrypt::Engine.hash to BCrypt::Engine.hash_secret to avoid Merb
sorting issues. [Lee Pope]
8 changes: 4 additions & 4 deletions lib/bcrypt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Engine

# Given a secret and a valid salt (see BCrypt::Engine.generate_salt) calculates
# a bcrypt() password hash.
def self.hash(secret, salt)
def self.hash_secret(secret, salt)
if valid_secret?(secret)
if valid_salt?(salt)
__bc_crypt(secret.to_s, salt)
Expand Down Expand Up @@ -123,7 +123,7 @@ class << self
#
# @password = BCrypt::Password.create("my secret", :cost => 13)
def create(secret, options = { :cost => BCrypt::Engine::DEFAULT_COST })
Password.new(BCrypt::Engine.hash(secret, BCrypt::Engine.generate_salt(options[:cost])))
Password.new(BCrypt::Engine.hash_secret(secret, BCrypt::Engine.generate_salt(options[:cost])))
end
end

Expand All @@ -139,7 +139,7 @@ def initialize(raw_hash)

# Compares a potential secret against the hash. Returns true if the secret is the original secret, false otherwise.
def ==(secret)
super(BCrypt::Engine.hash(secret, @salt))
super(BCrypt::Engine.hash_secret(secret, @salt))
end
alias_method :is_password?, :==

Expand All @@ -158,4 +158,4 @@ def split_hash(h)
return v, c.to_i, h[0, 29], mash[-31, 31]
end
end
end
end
12 changes: 6 additions & 6 deletions spec/bcrypt/engine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@
end

specify "should produce a string" do
BCrypt::Engine.hash(@password, @salt).should be_an_instance_of(String)
BCrypt::Engine.hash_secret(@password, @salt).should be_an_instance_of(String)
end

specify "should raise an InvalidSalt error if the salt is invalid" do
lambda { BCrypt::Engine.hash(@password, 'nino') }.should raise_error(BCrypt::Errors::InvalidSalt)
lambda { BCrypt::Engine.hash_secret(@password, 'nino') }.should raise_error(BCrypt::Errors::InvalidSalt)
end

specify "should raise an InvalidSecret error if the secret is invalid" do
lambda { BCrypt::Engine.hash(nil, @salt) }.should_not raise_error(BCrypt::Errors::InvalidSecret)
lambda { BCrypt::Engine.hash(false, @salt) }.should_not raise_error(BCrypt::Errors::InvalidSecret)
lambda { BCrypt::Engine.hash_secret(nil, @salt) }.should_not raise_error(BCrypt::Errors::InvalidSecret)
lambda { BCrypt::Engine.hash_secret(false, @salt) }.should_not raise_error(BCrypt::Errors::InvalidSecret)
end

specify "should be interoperable with other implementations" do
Expand All @@ -57,7 +57,7 @@
["0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", "$2a$05$abcdefghijklmnopqrstuu", "$2a$05$abcdefghijklmnopqrstuu5s2v8.iXieOjg/.AySBTTZIIVFJeBui"]
]
for secret, salt, test_vector in test_vectors
BCrypt::Engine.hash(secret, salt).should eql(test_vector)
BCrypt::Engine.hash_secret(secret, salt).should eql(test_vector)
end
end
end
end

0 comments on commit 1696fcd

Please sign in to comment.