Rails:BCrypt::Errors::InvalidHash(无效哈希)Devise 1.5.4 Rails 2.2.5

Far*_*had 3 ruby ruby-on-rails devise ruby-on-rails-3

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

config.encryptor = :bcrypt
Run Code Online (Sandbox Code Playgroud)

我们想将其更改为

config.encryptor = :authlogic_sha512
Run Code Online (Sandbox Code Playgroud)

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

但是,更新密码后,注销并登录时出现错误

BCrypt::Errors::InvalidHash (invalid hash):
Run Code Online (Sandbox Code Playgroud)

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

用户模型有以下行:

devise :database_authenticatable, :registerable, :confirmable,
     :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:auth0, :google_oauth2]
Run Code Online (Sandbox Code Playgroud)

所以步骤顺序是这样的:

  1. 用户的信息位于数据库(现有用户)中。使用 bcrypt 加密的密码
  2. 用户登录并在会话控制器中,我们将密码重新哈希为 sha512 并将其存储在 crypto_password 字段中
  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
Run Code Online (Sandbox Code Playgroud)

我还在数据库表中添加了“password_digest”和“password_salt”,但我的“save!” 然后方法失败并显示“未传递额外参数”。

Pro*_*ton 11

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

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

rails aborted!
BCrypt::Errors::InvalidHash: invalid hash
Run Code Online (Sandbox Code Playgroud)

我是这样解决的

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

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

资源BCrypt::Errors::InvalidHash:无效哈希#4861

就这样。

我希望这有帮助

  • 它确实有帮助,谢谢。 (2认同)