Ruby BCrypt hash comparison not working -
i new comer ruby, apologies if question has been answered. have read other questions , still cannot figure out doing wrong.
i creating hashed passwords storing in db this:
new_user.password = bcrypt::password.create(unhashed_password) # write user database new_user.store_user
i retrieve user db checking against inputed user name, , check password this:
# user database def self.get_user(check_user_name) db = user.open_db user = user.new user_arr = db.execute("select * user_data user_name = ?", check_user_name).first db.close # if user exists check password if user_arr.size != 0 print "enter password : " # password user user_input_password_attempt = gets.chomp end # parse db user user class if password guess correct stored_password = bcrypt::password.new(user_arr[2]) if user_input_password_attempt == stored_password @@users_logged_in += 1 user.user_id = user_arr[0] user.user_name = user_arr[1] user.password = user_arr[2] return user end :no_user
end
my problem var stored_password returning hash , != user_input_password_attempt have read ruby-doc , googled extensively
when use ==
calling ==
method defined on object on left hand side, passing right hand side argument:
a == b
is equivalent to
a.==(b)
depending on object call ==
method might receive different result. in other words:
a == b
might or might not return different result than
b ==
while think nonsense , equality operators should transitive
, symetric
, reflexive
bcrypt people have decided implement in way:
def ==(secret) super(bcrypt::engine.hash_secret(secret, @salt)) end
(taken http://bcrypt-ruby.rubyforge.org/classes/bcrypt/password.html#m000009)
this means have write:
stored_password = bcrypt::password.new(user_arr[2]) if stored_password == user_input_password_attempt ... end
in order call ==
method on password
instance.
Comments
Post a Comment