如何在Ruby中生成密码,就像在Ruby on Rails中的Devise Gem一样 [英] How to generate the password in PHP as it did by Devise Gem in Ruby on Rails

查看:258
本文介绍了如何在Ruby中生成密码,就像在Ruby on Rails中的Devise Gem一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这两个都是我发现的:

  1)devise.rb中的encrypter配置被禁用,如下所示:
#config.encryptor =:sha1
2)我读评论非常仔细,然后我发现他们使用sha512和bcrypt作为默认加密器。
#(默认),:sha512和:bcrypt。 Devise还支持来自其他人的加密程序

我尝试使用PHP以不同的方式创建相同的加密密码: / p>

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

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



帮助我!!!



ps。我的英文说的不是很好。即使我的英文不正确,请不要生气。






我在回答自己:


  1. 加密器是Sha1



    我只看devise.rb在文件夹\config\initializers
    加密器被称为#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\encryptors\sha1.rb



    需要digest / sha1



    模块设计
    模块加密器
    #= Sha1
    #使用Sha1哈希算法加密密码
    class Sha1 <基础

     #根据拉伸,盐,胡椒和
    #输入密码,输入默认密码摘要。
    def self.digest(密码,拉伸,盐,胡椒)
    digest = pepper
    stretchches.times {digest = self.secure_digest(salt,digest,password,pepper)}
    digest
    end

    private

    #生成加入args的SHA1摘要。生成的令牌就像
    #--arg1 - arg2 - arg3 - argN--
    def self.secure_digest(* tokens)
    :: Digest :: SHA1.hexdigest(' - '< tokens.flatten.join(' - ')< - ')
    end

    end
    end

    结束


所以我翻译成PHP

  function encrypt_password($ salt,$ password){
$ pepper = ;
$ digest = $ pepper;
$ stretchings = 10; $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ - $密码。' $辣椒。''。
$ digest = Sha1($ join);
}
$ result = substr($ digest,0,40);
return $ result;
}

它的工作非常好: - )

解决方案

设计代码如下:

  def self.digest (密码,拉伸,盐,胡椒)
:: BCrypt :: Engine.hash_secret(#{password}#{pepper},salt,stretch)
end

您可以在PHP中查看如何使用bcrypt在如何在PHP中使用bcrypt进行哈希密码?



由默认情况下,Devise使用10段。



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



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



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


I'm renewing a website from Ruby on Rails to PHP. I need to generate the passwords which are generated by Devise Gem in Ruby on Rails. I have to know what is the hashing method for password to create same method with PHP. but it's not easy to find that codes inside the Ruby on Rails as a beginner. If somebody know where should I check to find it, please help me.

These two are all what I found:

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

I tried to make the same encrypted password in different ways with 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);

I couldn't get the same result from any of above. Is there anybody whom could tell me the encryption method of Devise Gem??

HELP ME!!!

ps. I'm not good at English. Even if my English is not correct, please don't be angry.


I'm answering myself:

  1. The Encryptor is Sha1

    I was looking only "devise.rb" in the folder "\config\initializers" The encryptor was commanted as "# config.encryptor = :sha1" But there is one more "devise.rb" inside the Ruby lib folder, "\Ruby191\lib\ruby\gems\1.9.1\gems\devise-1.0.8\lib\devise.rb" There is one more configuration as "@@encryptor = :sha1"

  2. Encryption Method using Sha1 When you go to the file below you will see the codes for algorithm: \Ruby191\lib\ruby\gems\1.9.1\gems\devise-1.0.8\lib\devise\encryptors\sha1.rb

    require "digest/sha1"

    module Devise module Encryptors # = Sha1 # Uses the Sha1 hash algorithm to encrypt passwords. class Sha1 < Base

            # 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

So I translated to 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;
}

it's working very well :-)

解决方案

The devise code looks like:

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

You can see how to do bcrypt in PHP at How do you use bcrypt for hashing passwords in PHP?

By default, Devise uses 10 stretches.

The salt looks to be the first 29 characters of the encrypted password. You can do (in rails) User.first.authenticable_salt

The pepper should be listed in config/initializers/devise.rb but it may use your application secret token.

Please see https://github.com/plataformatec/devise/blob/master/lib/devise/models/encryptable.rb

这篇关于如何在Ruby中生成密码,就像在Ruby on Rails中的Devise Gem一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆