note 92684 added to function.hash-hmac
| From: | HenryMerriam at osu1 dot php dot net | Date: | Tue, 04 Aug 2009 05:36:13 +0000 |
| Subject: | note 92684 added to function.hash-hmac | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-159071@lists.php.net to get a copy of this message | ||
<?php
/**
* Implementation of the PBKDF2 key derivation function as described in RFC 2898.
*
* PBKDF2 was published as part of PKCS #5 v2.0 by RSA Security. The standard is
* also documented in IETF RFC 2898.
*
* The first four function arguments are as the standard describes:
*
* PBKDF2(P, S, c, dkLen)
*
* The fifth function argument specifies the hash function to be used. This should
* be provided in the same format as used for the hash() function. The default
* hash algorithm is SHA-1, but this is not recommended for new applications.
*
* The function returns false if dk_len is too large. Otherwise it returns the
* derived key as a binary string.
*
* @author Henry Merriam <php@henrymerriam.com>
*
* @param string p password
* @param string s salt
* @param int c iteration count
* @param int dk_len derived key length (octets)
* @param string algo hash algorithm
*
* @return string derived key
*/
function pbkdf2($p, $s, $c, $dk_len, $algo = 'sha1') {
// experimentally determine h_len for the algorithm in question
static $lengths;
if (!isset($lengths[$algo])) { $lengths[$algo] = strlen(hash($algo, null, true)); }
$h_len = $lengths[$algo];
if ($dk_len > (pow(2, 32) - 1) * $h_len) {
return false; // derived key is too long
} else {
$l = ceil($dk_len / $h_len); // number of derived key blocks to compute
$t = null;
for ($i = 1; $i <= $l; $i++) {
$f = $u = hash_hmac($algo, $s . pack('N', $i), $p, true); // first iterate
for ($j = 1; $j < $c; $j++) {
$f ^= ($u = hash_hmac($algo, $u, $p, true)); // xor each iterate
}
$t .= $f; // concatenate blocks of the derived key
}
return substr($t, 0, $dk_len); // return the derived key of correct length
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 69.19.14.30 (proxied: 72.168.228.129)
----
Manual Page -- http://www.php.net/manual/en/function.hash-hmac.php
Edit -- https://master.php.net/note/edit/92684
Del: integrated -- https://master.php.net/note/delete/92684/integrated
Del: useless -- https://master.php.net/note/delete/92684/useless
Del: bad code -- https://master.php.net/note/delete/92684/bad+code
Del: spam -- https://master.php.net/note/delete/92684/spam
Del: non-english -- https://master.php.net/note/delete/92684/non-english
Del: in docs -- https://master.php.net/note/delete/92684/in+docs
Del: other reasons-- https://master.php.net/note/delete/92684
Reject -- https://master.php.net/note/reject/92684
Search -- https://master.php.net/manage/user-notes.php