我现在更改为使用 Gem Devise 进行用户身份验证。但我不知道如何匹配加密!
我知道我们可以编写一个新的加密器并在初始化器中分配它,但关键是加密器只接受 4 个参数(密码、延伸、盐、胡椒)。但就我而言,我确实在加密中包含了用户的电子邮件和自定义的盐。
是否可以将用户的电子邮件和自定义盐传递到加密器中?
附言。我正在使用 database_authenticable
我现在更改为使用 Gem Devise 进行用户身份验证。但我不知道如何匹配加密!
我知道我们可以编写一个新的加密器并在初始化器中分配它,但关键是加密器只接受 4 个参数(密码、延伸、盐、胡椒)。但就我而言,我确实在加密中包含了用户的电子邮件和自定义的盐。
是否可以将用户的电子邮件和自定义盐传递到加密器中?
附言。我正在使用 database_authenticable
可惜没有人回答我的问题......
然而,我想我已经找到了答案,虽然它不像我想象的那么漂亮。
首先,在初始化程序中创建加密类:
module Devise
module Encryptors
class MySha1 < Base
def self.digest(password, salt)
Digest::SHA1.hexdigest("#{salt}-----#{password}")
end
def self.salt(email)
Digest::SHA1.hexdigest("#{Time.now}-----#{email}")
end
end
end
end
其次,覆盖 User 模型中的一些方法:
# overwrite this method so that we call the encryptor class properly
def encrypt_password
unless @password.blank?
self.password_salt = self.class.encryptor_class.salt(email)
self.encrypted_password = self.class.encryptor_class.digest(@password, self.password_salt)
end
end
# Because when the database_authenticatable wrote the following method to regenerate the password, which in turn passed incorrect params to the encrypt_password, these overwrite is needed!
def password=(password)
@password = password
end
def password_digest(pwd)
self.class.encryptor_class.digest(pwd, self.password_salt)
end
最后,我们必须教导何时加密密码:
before_save :encrypt_password
我不确定这有多老,但这似乎是一种更好的支持方式: http: //presentations.royvandewater.com/authentication-with-devise.html#9