我不完全確定,當您在Rails模型中添加has_secure_password
時,會涉及任何加密。我知道絕對有一個鹽的散列,但有加密嗎? bcrypt可以使用河豚,但它被用在bcrypt-ruby
(所有這一切的寶石)?has_secure_password - 只有散列或加密?
0
A
回答
1
TL; DR:has_secure_password
將使您在使用self.password=
方法時使用Bcrypt的散列函數。
讓我們來看看has_secure_password
代碼:
# 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
我們可以看到,它不散列/加密任何東西。然而,我們注意到:
include InstanceMethodsOnActivation
如果我們去的InstanceMethodsOnActivation
的文檔,我們得到如下代碼上:
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
因此,has_secure_password
不加密/哈希什麼,但包括InstanceMethodsOnActivation
模塊。該模塊定義了password=
方法。這種方法的重要組成部分是:
self.password_digest = BCrypt::Password.create(unencrypted_password, cost: cost)
現在讓我們去看看BCrypt::Password.create
的代碼:
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
在這種方法中,我們注意到一個特殊:
Password.new(BCrypt::Engine.hash_secret(secret, BCrypt::Engine.generate_salt(cost)))
如此看來成爲一個散列,這是合乎邏輯的(無論如何你都不想解密密碼)。
相關問題
- 1. 數只散列或加密解密
- 2. Castle ActiveRecord/NHibernate - 密碼加密或散列
- 3. MD5散列或加密其字符串?
- 4. 散列,加密或兩者都
- 5. 散列密碼的加密?
- 6. 加密與散列密碼
- 7. 加密散列元素樹
- 8. has_secure_password不加密password_digest Ruby on Rails的
- 9. DotNetNuke - 純文本加密/散列密碼
- 10. 散列密碼
- 11. 散列密碼
- 12. 密碼散列
- 13. 解密散列
- 14. 散列密碼
- 15. 散列密碼
- 16. 在Ruby中如何檢索只有散列和散列數組的散列的密鑰
- 17. 只有在散列表條件下才將值添加到密鑰
- 18. 解密散列密碼
- 19. 散列或加密的口令不自動生成鍵
- 20. 數據是否在Corda Ledger中進行了加密或散列?
- 21. password_verify - 密碼散列
- 22. 解密散列值
- 23. 散列密碼,SQL
- 24. PHP:密碼散列
- 25. 有沒有人認識到這種散列/加密?
- 26. 這是什麼樣的散列/加密?
- 27. 這是什麼樣的散列加密?
- 28. 散列/電子郵件加密
- 29. 使用散列MD5對DES加密
- 30. 加密和散列之間的區別