note 109158 added to function.hash-hmac

From: Date: Sun, 24 Jun 2012 20:15:02 +0000
Subject: note 109158 added to function.hash-hmac
Groups: php.notes 
Request: Send a blank email to php-notes+get-188778@lists.php.net to get a copy of this message
Edward, try this implementation, which I fixed after letter from one user: <?php /** * Implementation of the PBKDF2 key derivation function as described in * RFC 2898. * * @param string $PRF Hash algorithm. * @param string $P Password. * @param string $S Salt. * @param int $c Iteration count. * @param mixed $dkLen Derived key length (in octets). If $dkLen is FALSE * then length will be set to $PRF output length (in * octets). * @param bool $raw_output When set to TRUE, outputs raw binary data. FALSE * outputs lowercase hexits. * @return mixed Derived key or FALSE if $dkLen > (2^32 - 1) * hLen (hLen * denotes the length in octets of $PRF output). */ function pbkdf2($PRF, $P, $S, $c, $dkLen = false, $raw_output = false) { //default $hLen is $PRF output length $hLen = strlen(hash($PRF, '', true)); if ($dkLen === false) $dkLen = $hLen; //4294967295 == pow(2, 32) - 1 if ($dkLen <= $hLen * 4294967295) { $DK = ''; //create key $blocks_count = ceil($dkLen / $hLen); for ($block = 1; $block <= $blocks_count; $block++) { //initial hash for this block $ib = $h = hash_hmac($PRF, $S.pack('N', $block), $P, true); //perform block iterations for ($i = 1; $i < $c; $i++) { $ib ^= ($h = hash_hmac($PRF, $h, $P, true)); } //append iterated block $DK .= $ib; } $DK = substr($DK, 0, $dkLen); if (!$raw_output) $DK = bin2hex($DK); return $DK; //derived key too long } else { return false; } } ?> It seems to be a lot faster compared to the implementation you mentioned below. I use this file to test PBKDF2 with PKCS #5 PBKDF2 Test Vectors: #!/usr/bin/php <?php require_once 'pbkdf2.inc.php'; //require_once 'pbkdf2_v1.inc.php'; //from http://tools.ietf.org/html/draft-josefsson-pbkdf2-test-vectors-06 $test_vectors = [ [ 'P' => "password", 'S' => "salt", 'c' => 1, 'dkLen' => 20, 'DK' => '0c60c80f961f0e71f3a9b524af6012062fe037a6' ], [ 'P' => "password", 'S' => "salt", 'c' => 2, 'dkLen' => 20, 'DK' => 'ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957' ], [ 'P' => "password", 'S' => "salt", 'c' => 4096, 'dkLen' => 20, 'DK' => '4b007901b765489abead49d926f721d065a429c1' ], [ 'P' => "password", 'S' => "salt", 'c' => 16777216, 'dkLen' => 20, 'DK' => 'eefe3d61cd4da4e4e9945b3d6ba2158c2634e984' ], [ 'P' => "passwordPASSWORDpassword", 'S' => "saltSALTsaltSALTsaltSALTsaltSALTsalt", 'c' => 4096, 'dkLen' => 25, 'DK' => '3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038' ], [ 'P' => "pass\0word", 'S' => "sa\0lt", 'c' => 4096, 'dkLen' => 16, 'DK' => '56fa6aa75548099dcc37d7f03425e0c3' ] ]; $time = microtime(true); foreach ($test_vectors as $vector) { $pbkdf2 = pbkdf2('sha1', $vector['P'], $vector['S'], $vector['c'], $vector['dkLen']); //$pbkdf2 = bin2hex($pbkdf2); echo "pbkdf2(sha1, {$vector['P']}, {$vector['S']}, {$vector['c']}, {$vector['dkLen']}): $pbkdf2 - ", ($pbkdf2 === $vector['DK']) ? 'PASS' : 'WRONG', "\n"; } $time = microtime(true) - $time; echo "Time: $time s\n"; ?> ---- Server IP: 195.54.192.44 Probable Submitter: 178.210.22.117 ---- Manual Page -- http://www.php.net/manual/en/function.hash-hmac.php Edit -- https://master.php.net/note/edit/109158 Del: integrated -- https://master.php.net/note/delete/109158/integrated Del: useless -- https://master.php.net/note/delete/109158/useless Del: bad code -- https://master.php.net/note/delete/109158/bad+code Del: spam -- https://master.php.net/note/delete/109158/spam Del: non-english -- https://master.php.net/note/delete/109158/non-english Del: in docs -- https://master.php.net/note/delete/109158/in+docs Del: other reasons-- https://master.php.net/note/delete/109158 Reject -- https://master.php.net/note/reject/109158 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#188778) next »