2017-06-02 1 views
0

Ich bin nicht ganz sicher, ob, wenn Sie has_secure_password in einem Rails-Modell hinzufügen, ist jede Verschlüsselung beteiligt. Ich weiß, dass es Hashing mit einem Salz gibt, aber gibt es Verschlüsselung? bcrypt kann Blowfish verwenden, aber wird es in bcrypt-ruby (der Edelstein hinter all dem) verwendet?has_secure_password - nur Hashes oder verschlüsselt auch?

Antwort

1

TL; DR: has_secure_password werden Sie Bcrypt-Hash-Funktion verwenden, wenn Sie die self.password=-Methode verwenden.


auf den Code von has_secure_password Werfen wir einen Blick:

# File activemodel/lib/active_model/secure_password.rb, line 53 
    def has_secure_password(options = {}) 
     # Load bcrypt gem only when has_secure_password is used. 
     # This is to avoid ActiveModel (and by extension the entire framework) 
     # being dependent on a binary library. 
     begin 
     require "bcrypt" 
     rescue LoadError 
     $stderr.puts "You don't have bcrypt installed in your application. Please add it to your Gemfile and run bundle install" 
     raise 
     end 

     include InstanceMethodsOnActivation 

     if options.fetch(:validations, true) 
     include ActiveModel::Validations 

     # This ensures the model has a password by checking whether the password_digest 
     # is present, so that this works with both new and existing records. However, 
     # when there is an error, the message is added to the password attribute instead 
     # so that the error message will make sense to the end-user. 
     validate do |record| 
      record.errors.add(:password, :blank) unless record.password_digest.present? 
     end 

     validates_length_of :password, maximum: ActiveModel::SecurePassword::MAX_PASSWORD_LENGTH_ALLOWED 
     validates_confirmation_of :password, allow_blank: true 
     end 
    end 

Wir können sehen, dass es funktioniert NICHT hash/etwas verschlüsseln. Dennoch stellen wir fest:

 include InstanceMethodsOnActivation 

Wenn wir auf die Dokumentation von InstanceMethodsOnActivation gehen wir den folgenden Code erhalten:

def password=(unencrypted_password) 
    if unencrypted_password.nil? 
    self.password_digest = nil 
    elsif !unencrypted_password.empty? 
    @password = unencrypted_password 
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost 
    self.password_digest = BCrypt::Password.create(unencrypted_password, cost: cost) 
    end 
end 

Daher wird has_secure_password nicht verschlüsseln/hash alles schließt aber InstanceMethodsOnActivation Modul. Dieses Modul definiert die Methode password=. Der wichtige Teil dieser Methode ist:

self.password_digest = BCrypt::Password.create(unencrypted_password, cost: cost) 

ist jetzt Lass los BCrypt::Password.create ‚s-Code sehen:

def create(secret, options = {}) 
    cost = options[:cost] || BCrypt::Engine.cost 
    raise ArgumentError if cost > 31 
    Password.new(BCrypt::Engine.hash_secret(secret, BCrypt::Engine.generate_salt(cost))) 
    end 

    def valid_hash?(h) 
    h =~ /^\$[0-9a-z]{2}\$[0-9]{2}\$[A-Za-z0-9\.\/]{53}$/ 
    end 
end 

Bei diesem Verfahren bemerken wir insbesondere:

Password.new(BCrypt::Engine.hash_secret(secret, BCrypt::Engine.generate_salt(cost))) 

So scheint es, ein Hash zu sein, was logisch ist (Sie wollen sowieso kein Passwort entschlüsseln).

Verwandte Themen