title: PHP alternative of "PBKDF2WithHmacSHA256" from Java
body: <p>I need to generate in PHP the same hash as as the <code>PBKDF2WithHmacSHA256</code> algorithm generates in Java.</p>
  
  <p>I did some research and found two functions that seem to be connected to <code>PBKDF2</code> and <code>HMAC</code>:</p>
  
  <ul>
  <li>hash_pbkdf2 - <a href="http://php.net/manual/en/function.hash-pbkdf2.php" rel="nofollow noreferrer">http://php.net/manual/en/function.hash-pbkdf2.php</a></li>
  <li>hash_hmac - <a href="http://php.net/manual/en/function.hash-hmac.php" rel="nofollow noreferrer">http://php.net/manual/en/function.hash-hmac.php</a></li>
  </ul>
  
  <p>What PHP functions should I use? Is it even possible with native PHP functions?</p>
  
  <p><strong>Edit #1</strong></p>
  
  <p>My Java code, the same result I need achieve in PHP</p>
  
  <pre><code>public static byte[] derivateKey(char[] password, byte[] salt, int iterations, int keyLengthBits) {
          try {
              SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
              PBEKeySpec spec = new PBEKeySpec(password, salt, iterations, keyLengthBits);
              SecretKey key = skf.generateSecret(spec);
              byte[] res = key.getEncoded();
              return res;
  
          } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
              throw new IllegalStateException("Could not create hash", e);
          }
      }
  </code></pre>