[#4]
binodkumarluitel [ 2013-10-1722336025336003 ]
peoplewhowantspurephpimplementationofthefunction,I.e.whodon ' t have PHP 5.5 installedwintheirserver, canusethefollowingimplementation.nothinghasbeenmodifiedsofarasfromreference https://defuse.ca/PHP-pbk df2.htmbuttheoploplovovorence
formoreinformationaboutpbkdf 2see :http://en.Wikipedia.org/wiki/pbk df 2
if (! function_exists{
functionhash_pbkdf2($algorithm,$password,$salt,$count,$key_length,$raw_output=false ) {
classpbkdf2{
公共$ algorithm;
公共$ password;
公共$ salt;
公共计数;
public$key_length;
公共$ raw _ output;
private$hash_length;
private$output=';
公共函数_ _ construct ($ data=null ) )。
{
if($data!=null ) {
$this-init($data;
}
}
公共功能($ data ) )。
{
$ this-algorithm=$ data [ ' algorithm ' ];
$ this-password=$ data [ ' password ' ];
$this-salt=$data['salt'];
$this-count=$data['count'];
$ this-key _ length=$ data [ ' key _ length ' ];
$ this-raw _ output=$ data [ ' raw _ output ' ];
}
publicfunctionhash (
{
$ this-algorithm=strtolower ($ this-algorithm );
if (! in_Array($this-algorithm,hash_algos ),true )
throw new exception (' pbk df2error : invalidhashalgorithm.' );
if ($ this-count=0||$ this-key _ length=0) ) ) ) )。
throw new exception (' pbk df2error : invalid parameters.' );
$ this-hash _ length=strlen (hash ($ this-algorithm,',true ) );
$ block _ count=ceil ($ this-key _ length/$ this-hash _ length );
for($I=1; $i=$block_count; $i ) {
//$iencodedas4bytes,bigendian。
$last=$this-salt.pack('n ',$i );
//第一身份
$last=$ xor sum=hash _ hmac ($ this-algorithm,$ last,$this-password,true );
//perform the other $ this-count-1 iterations
for($j=1; $jcount; $j({
$xorsum^=($last=hash_hmac ) ($this-algorithm,$last,$this-password,true ) )
}
$this-output.=$xorsum;
if($this-raw_output )
返回substr ($ this-output,0,$this-key_length );
else
return bin2hex ($ this-output,0,$this-key_length );
}
}
}
$data=array(algorithm )=$algorithm,) password ) $password,) salt )=$salt,) count )=$count,) key。
try{
$pbkdf2=newpbkdf2($data );
return$pbkdf2-hash (;
}catch(exception$e ) {
throw$e;
}
}
}