我现在正在更改为使用Gem Devise进行用户验证。但我不知道如何匹配加密!Rails中的密码加密问题设计宝石
我知道我们可以编写一个新的加密器并将它分配给初始化器,但重点是加密器只接受4个参数(密码,延伸,盐,胡椒)。但就我而言,我确实在加密中包含了用户的电子邮件和定制盐。
是否可以将用户的电子邮件和定制盐传递给加密器?
ps。我使用database_authenticatable
我现在正在更改为使用Gem Devise进行用户验证。但我不知道如何匹配加密!Rails中的密码加密问题设计宝石
我知道我们可以编写一个新的加密器并将它分配给初始化器,但重点是加密器只接受4个参数(密码,延伸,盐,胡椒)。但就我而言,我确实在加密中包含了用户的电子邮件和定制盐。
是否可以将用户的电子邮件和定制盐传递给加密器?
ps。我使用database_authenticatable
这是可悲的,没有人回答我的问题......
不过,我想我已经找到了答案,尽管它并不像我想象的一样漂亮。
首先,创建于初始化加密类:
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
其次,覆盖的用户模型的一些方法:
# 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
我不知道这是多大年纪,但这似乎是一个更好的支持w这样做:http://presentations.royvandewater.com/authentication-with-devise.html#9
为什么你想包括电子邮件和定制的盐,而不是使用标准的方式? – Peder 2011-06-27 15:13:58
很久以前,如果我记得我的加密之前,改变设计使用另一个盐电子邮件。所以为了不影响用户,我们必须对设计的加密进行微调,以便得到与原始加密相同的结果。 – PeterWong 2011-06-27 18:03:44