Bcrypt - Automating password encryption process

Asked

Viewed 47 times

0

Hello. I already have a column in my database called password. Use Password for authentication, so he created another column called encypted_password. Even if I copy all the password values and play for encrypted_password, I would face an error, since the passwords were not encrypted. Is there any way to make that already at the time of copying, the password values were encrypted beautiful bcrypt and thrown in the destination column? Without something like that, I’d have to do everything in my hand, one by one.

1 answer

0


There is a solution, a bit old, for this. You can let Aim do it for you. This solution comes from this gist.

class User < ActiveRecord::Base
  alias :devise_valid_password? :valid_password?

  def valid_password?(password)
    begin
      devise_valid_password?(password)
    rescue BCrypt::Errors::InvalidHash
      return false unless Digest::SHA1.hexdigest(password) == encrypted_password
      logger.info "User #{email} is using the old password hashing method, updating attribute."
      self.password = password
      true
    end
  end
end

This will convert the passwords to the default from Windows. I hope I helped.

  • 1

    Same question asked on stack overflow gringo, it may be useful for several answers - http://stackoverflow.com/questions/6113375/converting-existing-password-hash-to-devise

Browser other questions tagged

You are not signed in. Login or sign up in order to post.