title: How to generate the password in PHP as it did by Devise Gem in Ruby on Rails
body: <p>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.</p>
  
  <p>These two are all what I found:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I tried to make the same encrypted password in different ways with PHP:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>I couldn't get the same result from any of above.
  Is there anybody whom could tell me the encryption method of Devise Gem??</p>
  
  <p>HELP ME!!!</p>
  
  <p>ps. I'm not good at English. Even if my English is not correct, please don't be angry.</p>
  
  <hr>
  
  <p>I'm answering myself:</p>
  
  <ol>
  <li><p>The Encryptor is Sha1</p>
  
  <p>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"</p></li>
  <li><p>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</p>
  
  <p>require "digest/sha1"</p>
  
  <p>module Devise
      module Encryptors
          # = Sha1
          # Uses the Sha1 hash algorithm to encrypt passwords.
          class Sha1 &lt; Base</p>
  
  <pre><code>        # 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('--' &lt;&lt; tokens.flatten.join('--') &lt;&lt; '--')
          end
  
      end
  end
  </code></pre>
  
  <p>end</p></li>
  </ol>
  
  <p>So I translated to PHP</p>
  
  <pre><code>function encrypt_password($salt, $password) {
      $pepper = '';
      $digest = $pepper;
      $stretches = 10;
  
      for ($i=0; $i&lt;$stretches; $i++) {
          $join = '--'.$salt.'--'.$digest.'--'.$password.'--'.$pepper.'--';
          $digest = Sha1($join);
      }
      $result = substr($digest, 0, 40);
      return $result;
  }
  </code></pre>
  
  <p>it's working very well :-)</p>