在Rails 4中验证Symfony 2密码


Validate Symfony 2 passwords in Rails 4

我必须从Symfony 2导入一个用户表到Rails 4应用程序。所有用户必须使用旧密码使用设计登录新的Rails应用程序。

目前我所做的如下:

class User < ActiveRecord::Base
  alias :devise_valid_password? :valid_password?
  def valid_password?(password)
    salt = self.salt
    begin
      devise_valid_password?(password)
    rescue BCrypt::Errors::InvalidHash
      return false unless Digest::SHA512.hexdigest("#{salt}:#{password}") == encrypted_password
      logger.info "User #{email} is using the old password hashing method, updating."
      self.password = password
      self.salt = nil
      self.save
      true
    end
  end
end

基本上,我正在测试提供的密码是否对使用BCrypt的设计有效,如果不是,它会用盐检查SHA512。Salt先前与加密的旧密码一起导入数据库。如果最后一项检查有效,它将密码存储在BCrypt中。

问题是我没有使用SHA512获得正确的加密密码。我不知道Symfony 2如何处理加密算法的password - salt,并且没有成功读取文档。

进入Symfony应用的安全性。Yml,它写的是:

encoders:
    Foo'BarBundle'Entity'User:
        algorithm:   sha512
        iterations: 1
        encode_as_base64: false

任何人都可以说什么是正确的语句,这一行工作作为Symfony 2做的?

Digest::SHA512.hexdigest("#{salt}:#{password}")

谢谢。

编辑

工作感谢next的回答。要点:https://gist.github.com/CV-Gate/840f2bc1ded9ed642b5f

可能有帮助:

Symfony uses a specific method to combine the salt and encode the password before
comparing it to your encoded password. If getSalt() returns nothing, then the submitted 
password is simply encoded using the algorithm you specify in security.yml. If a salt is 
specified, then the following value is created and then hashed via the algorithm:

$password.'{'.$salt.'}';

那么你可能会以错误的顺序散列密码吗?对不起,我不熟悉ruby,但Digest::SHA512.hexdigest("#{salt}:#{password}")似乎不像$password.'{'.$salt.'}';在php中