Reputation: 1159
I'm trying to do simple user authentication, but the call to Digest::SHA1::hexdigest
in the sessions controller is producing a different hash than the one in the user model.
user.rb:
class User < ActiveRecord::Base
before_save {|user| user.password = Digest::SHA1.hexdigest(user.password)}
...
end
sessions_controller.rb
def create
user = User.where(:username => params[:username], :password => Digest::SHA1.hexdigest(params[:password])).first
end
is there any way to fix this?
For example, the string, "password", when I try to login shows up as "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8".
However, in the database, the same string, "password", shows up as: "353e8061f2befecb6818ba0c034c632fb0bcae1b"
Upvotes: 1
Views: 3995
Reputation: 8834
before_save runs any time you save, so on create you encrypt, and then on any updates you're encrypting (the already encrypted one) again. Try using before_create.
Upvotes: 3