Neos\Flow\Security\Cryptography\Algorithms::pbkdf2 PHP Method

pbkdf2() public static method

See PKCS #5 v2.0 http://tools.ietf.org/html/rfc2898 for implementation details. The implementation is tested with test vectors from http://tools.ietf.org/html/rfc6070 . If https://wiki.php.net/rfc/hash_pbkdf2 is ever part of PHP we should check for the existence of hash_pbkdf2() and use it if available.
public static pbkdf2 ( string $password, string $salt, integer $iterationCount, integer $derivedKeyLength, string $algorithm = 'sha256' ) : string
$password string Input string / password
$salt string The salt
$iterationCount integer Hash iteration count
$derivedKeyLength integer Derived key length
$algorithm string Hash algorithm to use, see hash_algos(), defaults to sha256
return string The computed derived key as raw binary data
    public static function pbkdf2($password, $salt, $iterationCount, $derivedKeyLength, $algorithm = 'sha256')
    {
        $hashLength = strlen(hash($algorithm, null, true));
        $keyBlocksToCompute = ceil($derivedKeyLength / $hashLength);
        $derivedKey = '';
        for ($block = 1; $block <= $keyBlocksToCompute; $block++) {
            $iteratedBlock = hash_hmac($algorithm, $salt . pack('N', $block), $password, true);
            for ($iteration = 1, $iteratedHash = $iteratedBlock; $iteration < $iterationCount; $iteration++) {
                $iteratedHash = hash_hmac($algorithm, $iteratedHash, $password, true);
                $iteratedBlock ^= $iteratedHash;
            }
            $derivedKey .= $iteratedBlock;
        }
        return substr($derivedKey, 0, $derivedKeyLength);
    }

Usage Example

 /**
  * @test
  * @dataProvider pbkdf2TestVectors
  */
 public function pbkdf2TestVectorsAreCorrect($password, $salt, $iterationCount, $derivedKeyLength, $output)
 {
     $result = Algorithms::pbkdf2($password, $salt, $iterationCount, $derivedKeyLength, 'sha1');
     $this->assertEquals(unpack('H*', pack('H*', $output)), unpack('H*', $result));
 }
All Usage Examples Of Neos\Flow\Security\Cryptography\Algorithms::pbkdf2
Algorithms