【问题标题】:Rails: BCrypt::Errors::InvalidHash (invalid hash) Devise 1.5.4 Rails 2.2.5Rails:BCrypt::Errors::InvalidHash(无效哈希)设计 1.5.4 Rails 2.2.5
【发布时间】:2017-09-02 05:25:48
【问题描述】:

我们在 devise.rb 中使用以下内容

config.encryptor = :bcrypt

我们想把它改成

config.encryptor = :authlogic_sha512

还编写了代码来解密旧密码并在登录时将其散列到新密码(在会话控制器内)

但是,在更新密码后,在退出和登录时,它给出了一个错误

BCrypt::Errors::InvalidHash (invalid hash):

如果我注释掉错误的原点,这个错误会随机出现。所以我认为设计配置或 user.rb 模型有问题。

用户模型有以下行:

devise :database_authenticatable, :registerable, :confirmable,
     :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:auth0, :google_oauth2]

所以步骤顺序是这样的:

  1. 用户信息在数据库中(现有用户)。使用 bcrypt 加密的密码
  2. 用户登录并在会话控制器中,我们将密码重新散列到 sha512 并将其存储在 encrypted_pa​​ssword 字段中
  3. 用户退出
  4. 用户登录 *** 由于“invalid_hash”错误而无法登录。

知道这里可能是什么问题吗?提前致谢。

编辑:根据要求为上述步骤 2 编写代码:

class Users::SessionsController < Devise::SessionsController
...
...
email = params[:user]['login']

@user = User.find_by_email(email)
return if @user.nil?
# Get old password and salt
bcrypt = BCrypt::Password.new(@user.encrypted_password)
salt = bcrypt.salt

pwd = params[:user]['password']

pass = ::BCrypt::Engine.hash_secret("#{pwd}#{Devise.pepper}", salt)
# If passwords, match, re-hash it with SHA-512
if @user.encrypted_password == pass
  @user.password_digest = ::Devise::Encryptors::AuthlogicSha512.digest(pass, Devise.stretches, salt, Devise.pepper)
  @user.password_salt = salt
  @user.save!
end

我还在 db 表中添加了“password_digest”和“password_salt”,但我的“保存!”然后方法因“未传递额外参数”而失败。

【问题讨论】:

  • #2 的实际源代码可能会有所帮助,因为这可能是问题所在。我假设您正在覆盖 Devise 的SessionController?怎么样?
  • @ma_il 按要求添加了代码。
  • 你找到这个问题的解决方案了吗??
  • @abby37 不,没有找到解决方案。
  • 你得到了 encrypted_pa​​ssword,那你为什么还需要 password_digest??

标签: ruby-on-rails ruby ruby-on-rails-3 devise


【解决方案1】:

我在使用 Ruby 2.5 设置 Rails 5.1.6 应用程序时遇到了类似的问题。

当我尝试使用rails db:seed 播种数据时遇到以下错误:

rails aborted!
BCrypt::Errors::InvalidHash: invalid hash

我是这样解决的

我使用的Bcrypt版本是3.1.11,而Devise版本是4.4.3

我将 gem 更新为 Bcrypt 3.1.16Devise 4.7.3,从而解决了这个问题。

资源BCrypt::Errors::InvalidHash: invalid hash #4861

就是这样。

我希望这会有所帮助

【讨论】:

  • 它确实有帮助,谢谢。
【解决方案2】:

您似乎假设用户密码已使用 BCrypt 进行哈希处理,即使它在第一次运行时已经转换为 Authlogic。您可以检查是否@user.password_digest.present?,如果是这种情况,您可能应该检查参数中的密码。

此外,在为 Authlogic 创建摘要时,您将在 pass 中获取已经加密的值并再次对其进行哈希处理。相反,在创建 authlogic 摘要时,它也只需要参数中的密码。

【讨论】:

  • password_digest 在哪里设置?每当我尝试访问它时,它都会显示“访问受保护字段时出错”。对不起,菜鸟问题,但我是红宝石的新手。 password_digest 也应该是“用户”数据库表中的一个字段吗?那么password_salt呢?
  • u.password_digest.present? NoMethodError:为# <0x007f822415abf0>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-04
  • 2017-01-24
  • 1970-01-01
  • 2012-06-17
  • 1970-01-01
  • 2021-10-19
  • 1970-01-01
相关资源
最近更新 更多