如何在 PHP 中生成密码,就像 Devise Gem 在 Ruby on Rails 中生成的那样

2024-04-09

我正在将网站从 Ruby on Rails 更新为 PHP。 我需要生成由 Devise Gem 在 Ruby on Rails 中生成的密码。 我必须知道密码的哈希方法是什么,才能使用 PHP 创建相同的方法。 但作为初学者,要在 Ruby on Rails 中找到这些代码并不容易。 如果有人知道我应该在哪里检查才能找到它,请帮助我。

这两个都是我找到的:

1) The configuration of encryptor is disabled in devise.rb like below:
  # config.encryptor = :sha1
2) I read the comments very carefully then I found that they using sha512 and bcrypt as default encryptor.
  # (default), :sha512 and :bcrypt. Devise also supports encryptors from others

我尝试用 PHP 以不同的方式制作相同的加密密码:

1) sha1('--'.$password_salt.'--'.$encrypted_password);
2) sha1($password_salt.'-----'.$encrypted_password);
3) sha1('--'.$password_salt.'--'.$encrypted_password.'--');
4) sha1($password_salt.$encrypted_password);
5) sha1($encrypted_password.$password_salt);
6) substr(hash('sha512', $password_salt.$encrypted_password, false), 20);
7) substr(hash('sha512', $encrypted_password.$password_salt, false), 0, 40);
8) hash('sha512', $encrypted_password.$password_salt, false);
9) hash('sha512', $password_salt.$encrypted_password, false);
10) substr(hash('sha512', '--'.$password_salt.'--'.$encrypted_password.'--', false), 0, 40);

我无法从上述任何一个中得到相同的结果。 有谁可以告诉我Devise Gem的加密方法吗?

帮我!!!

附:我不擅长英语。即使我的英语不正确,也请不要生气。


我自己回答:

  1. 加密器是Sha1

    我只在文件夹“\config\initializers”中查找“devise.rb” 加密器的命令为“#config.encryptor = :sha1” 但是 Ruby lib 文件夹中多了一个“devise.rb”, “\Ruby191\lib\ruby\gems\1.9.1\gems\devise-1.0.8\lib\devise.rb” 还有一个配置为“@@encryptor = :sha1”

  2. 使用 Sha1 的加密方法 当您转到下面的文件时,您将看到算法代码: \Ruby191\lib\ruby\gems\1.9.1\gems\devise-1.0.8\lib\devise\cryptors\sha1.rb

    需要“摘要/sha1”

    模块设计 模块加密器 # = Sha1 # 使用Sha1哈希算法对密码进行加密。 类 Sha1

            # Gererates a default password digest based on stretches, salt, pepper and the
            # incoming password.
            def self.digest(password, stretches, salt, pepper)
                digest = pepper
                stretches.times { digest = self.secure_digest(salt, digest, password, pepper) }
                digest
            end
    
            private
    
            # Generate a SHA1 digest joining args. Generated token is something like
            #     --arg1--arg2--arg3--argN--
            def self.secure_digest(*tokens)
                ::Digest::SHA1.hexdigest('--' << tokens.flatten.join('--') << '--')
            end
    
        end
    end
    

    end

所以我翻译成PHP

function encrypt_password($salt, $password) {
    $pepper = '';
    $digest = $pepper;
    $stretches = 10;

    for ($i=0; $i<$stretches; $i++) {
        $join = '--'.$salt.'--'.$digest.'--'.$password.'--'.$pepper.'--';
        $digest = Sha1($join);
    }
    $result = substr($digest, 0, 40);
    return $result;
}

它运行得很好:-)


设计代码如下:

 def self.digest(password, stretches, salt, pepper)
   ::BCrypt::Engine.hash_secret("#{password}#{pepper}",salt, stretches)
 end

您可以在以下位置查看如何在 PHP 中执行 bcrypt:如何在 PHP 中使用 bcrypt 对密码进行哈希处理? https://stackoverflow.com/questions/4795385/how-do-you-use-bcrypt-for-hashing-passwords-in-php

默认情况下,Devise 使用 10 次拉伸。

盐看起来是加密密码的前 29 个字符。你可以(在 Rails 中)User.first.authenticable_salt

胡椒应该列在config/initializers/devise.rb但它可能会使用您的应用程序秘密令牌。

请参见https://github.com/plataformatec/devise/blob/master/lib/devise/models/encryptable.rb https://github.com/plataformatec/devise/blob/master/lib/devise/models/encryptable.rb

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 PHP 中生成密码,就像 Devise Gem 在 Ruby on Rails 中生成的那样 的相关文章

随机推荐