Bcrypt - Automating password encryption process

0

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

    
asked by anonymous 04.01.2017 / 19:13

1 answer

0

There is a somewhat old solution to this. You can let the devise 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 devise pattern. I hope I have helped.

    
06.01.2017 / 02:35