Skip to content
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

Add support for Bcrypt algorithm version 2b #11595

Merged
merged 8 commits into from
Jan 30, 2022
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
19 changes: 19 additions & 0 deletions spec/std/crypto/bcrypt/password_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ describe "Crypto::Bcrypt::Password" do
Crypto::Bcrypt::Password.new("blarp")
end
end

it "raises on unsupported version (#11584)" do
expect_raises(Crypto::Bcrypt::Error, "Invalid hash version") do
Crypto::Bcrypt::Password.new("$-1$10$blarp")
end
end
end

describe "create" do
Expand All @@ -46,6 +52,9 @@ describe "Crypto::Bcrypt::Password" do

describe "verify" do
password = Crypto::Bcrypt::Password.create("secret", 4)
password2 = Crypto::Bcrypt::Password.new("$2$04$ZsHrsVlj.dsmn74Az1rjmeE/21nYRC0vB5LPjG7ySBfi6lRaO/P22")
password2a = Crypto::Bcrypt::Password.new("$2a$04$ZsHrsVlj.dsmn74Az1rjmeE/21nYRC0vB5LPjG7ySBfi6lRaO/P22")
password2b = Crypto::Bcrypt::Password.new("$2b$04$ZsHrsVlj.dsmn74Az1rjmeE/21nYRC0vB5LPjG7ySBfi6lRaO/P22")

it "verifies password is incorrect" do
(password.verify "wrong").should be_false
Expand All @@ -54,5 +63,15 @@ describe "Crypto::Bcrypt::Password" do
it "verifies password is correct" do
(password.verify "secret").should be_true
end

it "verifies password version 2 is correct (#11584)" do
(password2.verify "secret").should be_true
end
it "verifies password version 2a is correct (#11584)" do
(password2a.verify "secret").should be_true
end
it "verifies password version 2b is correct (#11584)" do
(password2b.verify "secret").should be_true
end
end
end
6 changes: 5 additions & 1 deletion src/crypto/bcrypt/password.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ require "../subtle"
#
# See `Crypto::Bcrypt` for hints to select the cost when generating hashes.
class Crypto::Bcrypt::Password
private SUPPORTED_VERSIONS = ["2", "2a", "2b"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're already in here, should we throw in 2y? Is the version that is used in PHP based implementations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Blacksmoke16 sure, I'll just do it in a separate PR since I'd also like to add tests etc., so it's not a 1 line change.


# Hashes a password.
#
# ```
Expand Down Expand Up @@ -45,6 +47,7 @@ class Crypto::Bcrypt::Password
def initialize(@raw_hash : String)
parts = @raw_hash.split('$')
raise Error.new("Invalid hash string") unless parts.size == 4
raise Error.new("Invalid hash version") unless SUPPORTED_VERSIONS.includes?(parts[1])

@version = parts[1]
@cost = parts[2].to_i
Expand All @@ -67,7 +70,8 @@ class Crypto::Bcrypt::Password
# ```
def verify(password : String) : Bool
hashed_password = Bcrypt.new(password, salt, cost)
Crypto::Subtle.constant_time_compare(@raw_hash, hashed_password)
hashed_password_digest = Base64.encode(hashed_password.digest, hashed_password.digest.size - 1)
Crypto::Subtle.constant_time_compare(@digest, hashed_password_digest)
end

def to_s(io : IO) : Nil
Expand Down