<?php
function pbkdf2( $algorithm = 'sha512', $password, $salt, $count = 2048, $key_length = 512, $raw_output = false ){
if($count <= 0 || $key_length <= 0) {
$count = 2048;
$key_length = 512;
}
$hash_length = strlen(hash($algorithm, "", true));
$block_count = ceil($key_length / $hash_length);
$output = "";
for($i = 1; $i <= $block_count; $i++) {
$last = $salt . pack("N", $i);
$last = $xorsum = hash_hmac($algorithm, $last, $password, true);
for ($j = 1; $j < $count; $j++) {
$xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true));
}
$output .= $xorsum;
}
if($raw_output) {
return substr($output, 0, $key_length);
} else {
return bin2hex(substr($output, 0, $key_length));
}
}
function generate_salt(){
$random = file_get_contents( '/dev/urandom', false, null, 0, 256 );
return bin2hex( $random );
}
function get_hash( $plain_text, $salt ){
if( !isset($plain_text) || !isset($salt) ){
return false;
}
if( gettype($salt) == "string" ){
$salt = array( $salt, $salt );
}
$iterations = 1024;
if( !function_exists("hash_pbkdf2") ){
$hash = pbkdf2( "whirlpool", $plain_text, $salt[0], $iterations, 512, true );
$hash = pbkdf2( "sha512", $hash, $salt[1], $iterations, 512 );
} else {
$hash = hash_pbkdf2( "whirlpool", $plain_text, $salt[0], $iterations, 512, true );
$hash = hash_pbkdf2( "sha512", $hash, $salt[1], $iterations, 512 );
}
return $hash;
}
function test(){
$salt = array(
0 => generate_salt(),
1 => generate_salt()
);
$salt = array(
0 => '5162e9d43703517e8efa9c0ff835e6d49914e60ae567574466e21bdbb6d6e647d58e468c3b5ff1b4b5bf667ce7d9896d0a8535bb9e862c65f998ba45fafa5fee1eef524873f3daf5405a36a471dc87803ac25df9d05463649e80941982c97315fb94b0d8d812420e1433fa45b75cbfba70723d5ad7fefe10a4e4d6e4759bacaa93903947e1f9157b204fbd17c489cc5edeb02f35301b45a4e3f0097aadf74e7bbdc25050c685e236fd90125e236e2eb349ec1bf2a7a33f3e6e1555b8658c0fcb155220b14cf03f420a7fda1012b089238c2c6226bf0d5c38742df8853caccd6f23baa0b2430731d4ba9c52d5b1d7e07622524969399971ad42a4af1195b14e7b',
1 => '81c36d5f5612aeae6b5d5595f0ed1541f6f732f1512695baceb514e1964f9a0551d902945df52c81a77f3b66936040be1eb723e571e5d1de7769fe016ccfa7b9468e1d912b17d608f052a3ae834254445be575c47f75f88464116698dba29694e5da8082c1e97430ab46cf7d5be5d0e2c4f94db93eb8c7bfdd5bb84d68d4685a31f6fb06e2408a268a651395b9faef509acbfdbb9d32fd0ffe5b8b7f7f71e72c825fe2e516e3be082041807978bc17a48f2ed53ed00a8533cbaa90b49b6ebda6028406a1845ada1ac77ca7433ee27449913afd82726d47c96237b9f5059404033e1bc62a60ffcd9d174f6ca49b6b07a0ae3042bc1ba06ad56e85466cf4cc6025'
);
$plain_text = "mysupersecurepassword";
echo "start \n";
$time = time();
$result = get_hash( $plain_text, $salt );
echo $result;
echo "\ntime: ". ( time() - $time );
}
test();
?>
preferences:
42.73 ms | 402 KiB | 5 Q