From ad5fb0aad5f36accac16be072866b3c1ab5f0c7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Sun, 8 Jul 2018 13:17:31 +0200 Subject: [PATCH] Fix: Add type restriction to Crypto::Bcrypt::Password#== --- spec/std/crypto/bcrypt/password_spec.cr | 11 +++++++++++ src/crypto/bcrypt/password.cr | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/spec/std/crypto/bcrypt/password_spec.cr b/spec/std/crypto/bcrypt/password_spec.cr index fb8f773c4345..033325b9c9cf 100644 --- a/spec/std/crypto/bcrypt/password_spec.cr +++ b/spec/std/crypto/bcrypt/password_spec.cr @@ -48,5 +48,16 @@ describe "Crypto::Bcrypt::Password" do it "verifies password is correct" do (password == "secret").should be_true end + + it "works with Password" do + (password == password).should be_true + + other_password = Crypto::Bcrypt::Password.create("wrong", 4) + (password == other_password).should be_false + end + + it "works with other types" do + (password == 0.815).should be_false + end end end diff --git a/src/crypto/bcrypt/password.cr b/src/crypto/bcrypt/password.cr index 7e80965dd162..aa8a05a66111 100644 --- a/src/crypto/bcrypt/password.cr +++ b/src/crypto/bcrypt/password.cr @@ -58,7 +58,7 @@ class Crypto::Bcrypt::Password # password == "wrong secret" # => false # password == "super secret" # => true # ``` - def ==(password) + def ==(password : String) : Bool hashed_password = Bcrypt.new(password, salt, cost) Crypto::Subtle.constant_time_compare(@raw_hash, hashed_password) end