note 118301 added to function.hash-pbkdf2

From: Date: Wed, 11 Nov 2015 13:45:35 +0000
Subject: note 118301 added to function.hash-pbkdf2
Groups: php.notes 
Request: Send a blank email to php-notes+get-204138@lists.php.net to get a copy of this message
This is a light-weight drop-in replacement for PHP's hash_pbkdf2(); written for compatibility with older versions of PHP. Written, formatted and tested by myself, but using code and ideas based on the following: https://defuse.ca/php-pbkdf2.htm https://github.com/rchouinard/hash_pbkdf2-compat/blob/master/src/hash_pbkdf2.php https://gist.github.com/rsky/5104756 My main goals: 1) Maximum compatibility with PHP hash_pbkdf2(), ie. a drop-in replacement function 2) Minimum code size/bloat 3) Easy to copy/paste 4) No classes, and not encapsulated in a class! Why write a class when a simple function will do? 5) Eliminate calls to sprintf(). (used by other examples for the error reporting) 6) No other dependencies, ie. extra required functions <?php if (!function_exists('hash_pbkdf2')) { function hash_pbkdf2($algo, $password, $salt, $count, $length = 0, $raw_output = false) { if (!in_array(strtolower($algo), hash_algos())) trigger_error(__FUNCTION__ . '(): Unknown hashing algorithm: ' . $algo, E_USER_WARNING); if (!is_numeric($count)) trigger_error(__FUNCTION__ . '(): expects parameter 4 to be long, ' . gettype($count) . ' given', E_USER_WARNING); if (!is_numeric($length)) trigger_error(__FUNCTION__ . '(): expects parameter 5 to be long, ' . gettype($length) . ' given', E_USER_WARNING); if ($count <= 0) trigger_error(__FUNCTION__ . '(): Iterations must be a positive integer: ' . $count, E_USER_WARNING); if ($length < 0) trigger_error(__FUNCTION__ . '(): Length must be greater than or equal to 0: ' . $length, E_USER_WARNING); $output = ''; $block_count = $length ? ceil($length / strlen(hash($algo, '', $raw_output))) : 1; for ($i = 1; $i <= $block_count; $i++) { $last = $xorsum = hash_hmac($algo, $salt . pack('N', $i), $password, true); for ($j = 1; $j < $count; $j++) { $xorsum ^= ($last = hash_hmac($algo, $last, $password, true)); } $output .= $xorsum; } if (!$raw_output) $output = bin2hex($output); return $length ? substr($output, 0, $length) : $output; } } ---- Server IP: 72.52.91.14 Probable Submitter: 105.225.243.183 ---- Manual Page -- http://php.net/manual/en/function.hash-pbkdf2.php Edit -- https://master.php.net/note/edit/118301 Del: integrated -- https://master.php.net/note/delete/118301/integrated Del: useless -- https://master.php.net/note/delete/118301/useless Del: bad code -- https://master.php.net/note/delete/118301/bad+code Del: spam -- https://master.php.net/note/delete/118301/spam Del: non-english -- https://master.php.net/note/delete/118301/non-english Del: in docs -- https://master.php.net/note/delete/118301/in+docs Del: other reasons-- https://master.php.net/note/delete/118301 Reject -- https://master.php.net/note/reject/118301 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#204138) next »