【问题标题】:sha1 to brcrypt ruby on railssha1 到 bcrypt ruby​​ on rails
【发布时间】: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 是加密版本

任何帮助将不胜感激

谢谢

【问题讨论】:

标签: ruby-on-rails ruby sha1 bcrypt


【解决方案1】:

好吧,我的错。

实际上这很好,只是我在update_attributes secret_code 上运行了一个回调,它再次加密了哈希。 我现在正在使用`updat

【讨论】:

    猜你喜欢
    • 2016-10-09
    • 2014-04-17
    • 1970-01-01
    • 2013-10-10
    • 2013-04-24
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多