Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion lib/bcrypt/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def self.generate_salt(cost = self.cost)

# Returns true if +salt+ is a valid bcrypt() salt, false if not.
def self.valid_salt?(salt)
!!(salt =~ /^\$[0-9a-z]{2,}\$[0-9]{2,}\$[A-Za-z0-9\.\/]{22,}$/)
!!(salt =~ /\A\$[0-9a-z]{2,}\$[0-9]{2,}\$[A-Za-z0-9\.\/]{22,}\z/)
end

# Returns true if +secret+ is a valid bcrypt() secret, false if not.
Expand Down
2 changes: 1 addition & 1 deletion lib/bcrypt/password.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def create(secret, options = {})
end

def valid_hash?(h)
/^\$[0-9a-z]{2}\$[0-9]{2}\$[A-Za-z0-9\.\/]{53}$/ === h
/\A\$[0-9a-z]{2}\$[0-9]{2}\$[A-Za-z0-9\.\/]{53}\z/ === h
end
end

Expand Down
2 changes: 2 additions & 0 deletions spec/bcrypt/password_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
describe "Validating a generated salt" do
specify "should not accept an invalid salt" do
expect(BCrypt::Engine.valid_salt?("invalid")).to eq(false)
expect(BCrypt::Engine.valid_salt?("invalid\n#{BCrypt::Engine.generate_salt}\ninvalid")).to eq(false)
end
specify "should accept a valid salt" do
expect(BCrypt::Engine.valid_salt?(BCrypt::Engine.generate_salt)).to eq(true)
Expand All @@ -117,6 +118,7 @@
describe "Validating a password hash" do
specify "should not accept an invalid password" do
expect(BCrypt::Password.valid_hash?("i_am_so_not_valid")).to be(false)
expect(BCrypt::Password.valid_hash?("invalid\n#{BCrypt::Password.create "i_am_so_valid"}\ninvalid")).to be(false)
end
specify "should accept a valid password" do
expect(BCrypt::Password.valid_hash?(BCrypt::Password.create "i_am_so_valid")).to be(true)
Expand Down