note 108966 added to function.hash-hmac
| From: | kolonist at xinit dot ru | Date: | Fri, 08 Jun 2012 15:47:50 +0000 |
| Subject: | note 108966 added to function.hash-hmac | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-188464@lists.php.net to get a copy of this message | ||
This is implementation of PBKDF2 algorithm, compiled from #92684 and #101540 comments. I think it
can be a bit more comfortable to use.
<?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;
if ($dkLen <= (pow(2, 32) - 1) * $hLen) {
$DK = '';
//create key
for ($block = 1; $block <= $dkLen; $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;
}
}
----
Server IP: 69.147.83.197
Probable Submitter: 178.210.24.134
----
Manual Page -- http://www.php.net/manual/en/function.hash-hmac.php
Edit -- https://master.php.net/note/edit/108966
Del: integrated -- https://master.php.net/note/delete/108966/integrated
Del: useless -- https://master.php.net/note/delete/108966/useless
Del: bad code -- https://master.php.net/note/delete/108966/bad+code
Del: spam -- https://master.php.net/note/delete/108966/spam
Del: non-english -- https://master.php.net/note/delete/108966/non-english
Del: in docs -- https://master.php.net/note/delete/108966/in+docs
Del: other reasons-- https://master.php.net/note/delete/108966
Reject -- https://master.php.net/note/reject/108966
Search -- https://master.php.net/manage/user-notes.php