【发布时间】:2018-06-28 11:30:56
【问题描述】:
我目前使用 sha1 加密了一个 4 位密码属性。 由于它不是很安全,我想迁移到使用 bcrypt。
为了将 sha1 哈希转换为 bcrypt,我编写了以下 rake 任务
namespace :user do
desc "convert all secret codes encryption from sha1 to bcrypt"
@rainbow_table = {}
task secret_code: :environment do
User.all.each do |user|
clean_secret_code = @rainbow_table[user.secret_code]
bcrypted_secret_code = BCrypt::Password.create(clean_secret_code)
user.update_attributes secret_code: bcrypted_secret_code
p user.valid_secret_code? clean_secret_code.to_s
end
end
def create_rainbow_table
("0000".."9999").each do |i|
@rainbow_table.merge!(Digest::SHA1.hexdigest(i.to_s) => i.to_s)
end
end
end
所以这很好用,但是当我为我的 ruby on rails 应用程序运行时:
BCrypt::Password.new(secret_code) == code.to_s
它返回false
code 是经过哈希处理的密码,secret_code 是加密版本
任何帮助将不胜感激
谢谢
【问题讨论】:
-
你应该检查答案stackoverflow.com/questions/12028910/…,在那里你会找到你的答案。
-
感谢@tukan,但我没有看到与我目前正在做的事情有什么不同
标签: ruby-on-rails ruby sha1 bcrypt