Skip to content

Commit

Permalink
Ensure valid_hash? returns a Boolean
Browse files Browse the repository at this point in the history
I noticed that valid_hash? returns either nil or 0, in the usual cases.
Since the docs list valid_hash? as returning a Boolean, I thought this might be a bug.

at the very least I suspect think this would be less confusing.
  • Loading branch information
nathanKramer committed May 26, 2020
1 parent 2fbe541 commit eb567cb
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
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)
h =~ /^\$[0-9a-z]{2}\$[0-9]{2}\$[A-Za-z0-9\.\/]{53}$/
/^\$[0-9a-z]{2}\$[0-9]{2}\$[A-Za-z0-9\.\/]{53}$/ === h
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/bcrypt/password_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@

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_falsey
expect(BCrypt::Password.valid_hash?("i_am_so_not_valid")).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_truthy
expect(BCrypt::Password.valid_hash?(BCrypt::Password.create "i_am_so_valid")).to be(true)
end
end

0 comments on commit eb567cb

Please sign in to comment.