更新 ruby​​ on Rails 中的数据库字段

发布于 2024-12-23 15:55:20 字数 1015 浏览 3 评论 0原文

我正在更新数据库哈希算法。

我当前的系统运行在md5上,我想将其更改为BCrypt +salt

我的问题是,当旧用户(密码以 md5 进行哈希处理的用户)使用旧密码登录时,我想自动将数据库中的密码更改为 BCrypt+salt。

       if // check if the password stored in bcrypt
        salt = IDA::Config.get_configuration('salt')
        hash_password = BCrypt::Password.new(hash)
         return (BCrypt::Password.create(salt['salt_value']+password) == (salt['salt_value']+password)) ? true : false

      else // for users who's password encrypted in md5.

        salt = IDA::Config.get_configuration('salt') // i"m getting a salt here 
         BCrypt::Password.create(salt['salt_value']+password) // Im getting a salted bcryptted password  and I tried to put this into db manually and try to login it works perfectly
          // I want to write this new salted password into db once the user is authenticated with his old password
        return (Digest::MD5.hexdigest(password) == hash) ? true : false

我想在模型中写下这个。任何帮助将不胜感激。 谢谢

I'm updating a database hashing algorithm.

My current system runs on md5 and I want to change it into BCrypt +salt.

My problem is when an old user(users whos password hashed in md5) is login with his old password I want to automatically change the password to BCrypt+salt in database.

       if // check if the password stored in bcrypt
        salt = IDA::Config.get_configuration('salt')
        hash_password = BCrypt::Password.new(hash)
         return (BCrypt::Password.create(salt['salt_value']+password) == (salt['salt_value']+password)) ? true : false

      else // for users who's password encrypted in md5.

        salt = IDA::Config.get_configuration('salt') // i"m getting a salt here 
         BCrypt::Password.create(salt['salt_value']+password) // Im getting a salted bcryptted password  and I tried to put this into db manually and try to login it works perfectly
          // I want to write this new salted password into db once the user is authenticated with his old password
        return (Digest::MD5.hexdigest(password) == hash) ? true : false

I want to write this in model.Any help will be greatly appreciated.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的绿洲 2024-12-30 15:55:20

首先,BCrypt(图书馆和宝石)处理盐,因此您可以消除所有盐业务。

其次,您真正想要的是一种重新散列所有记录敏感数据(我假设是密码)的方法。因此,您要做的就是:

  1. 向模型添加一个字段,例如“is_bcrypt?”和布尔值。
  2. 编写并运行它:

# First we need to make sure the bcrypt library is there
require 'bcrypt'

# Gather all of the records
records = YourModel.all

# Go over each of the records
records.each do |record|
  # Check to see if the record has a bcrypt'ed password
  unless record.is_bcrypt?
    # If it doesn't take the value of password, unhash it, rehash it
    record.password = BCrypt::Password.create Digest::MD5.hexdigest password

    # If it saves correctly, mark the thing as being rehashed
    record.is_bcrypt = true if record.save
  end
end

有关详细信息,请参阅评论。新字段 is_bcrypt 只是为了让您知道哪些记录已被散列,哪些没有。只有当他们也真正储蓄时才会发生这种情况。

完成此操作后,并且您确定所有有关密码的代码都已重构,您可以取出该字段。

First off, BCrypt (both the library and the gem) handles salt so you can nix all of the salt business.

Second what you really want is a way of re-hashing all of your records sensitive data (passwords I assume). So here's what you do:

  1. Add a field to the model, something like "is_bcrypt?" and boolean.
  2. Write and run this:

# First we need to make sure the bcrypt library is there
require 'bcrypt'

# Gather all of the records
records = YourModel.all

# Go over each of the records
records.each do |record|
  # Check to see if the record has a bcrypt'ed password
  unless record.is_bcrypt?
    # If it doesn't take the value of password, unhash it, rehash it
    record.password = BCrypt::Password.create Digest::MD5.hexdigest password

    # If it saves correctly, mark the thing as being rehashed
    record.is_bcrypt = true if record.save
  end
end

See comments for details. The new field is_bcrypt is just so you can know which records have been hashed and which haven't. It only happens if they actually save too.

When this is done, and you're sure all the code concerning passwords is refactored you can take out that field.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文