簡體   English   中英

sha1將brcrypt ruby​​帶入軌道

[英]sha1 to brcrypt ruby on rails

我有一個4位數的密碼屬性,目前使用sha1加密。 由於它不是很安全,所以我想遷移到使用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

所以這工作正常,但是當我為我的Rails應用程序運行Ruby時,我這樣做:

BCrypt::Password.new(secret_code) == code.to_s

它返回false codesecret_code加密的秘密代碼),並且secret_code是加密的版本

任何幫助將不勝感激

謝謝

好的,我不好。

實際上,這工作正常,只是我在update_attributes secret_code上運行了一個回調,該回調再次對哈希進行加密。 我現在正在使用`updat

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM