PHP | hash_pbkdf2() Function

The hash_pbkdf2() function is an inbuilt function in PHP which is used to generate a PBKDF2 key derivation of a supplied password.

Syntax:

string hash_pbkdf2( $algo, $pass, $salt, $itr, $len, $raw_opt )

Parameters: This function accept six parameters as mention above and describe below.

  • $algo: It is the required parameter which specifies the selected hashing algorithm (like- “md5”, “sha256”, “sha1”).
  • $pass: This parameter is used to specify the password to use for the derivation.
  • $salt: This parameter is used for the derivation and the value should be generated randomly.
  • $itr: This parameter count the number of internal iterations.
  • $len: This parameter is used to hold the length of the output string.
  • $raw_opt: If this parameter set to True then its output will be raw binary data and if this parameter set to false then output will be lowercase hexits.

Return Value: This function returns the string containing the calculated message digest as lowercase hexits.

Below programs illustrate the hash_pbkdf2() function in PHP:
Program 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$gfg = "GeeksforGeeks";
$iterations = 142;
  
// Generate a random IV using 
// openssl_random_pseudo_bytes()
// random_bytes() or another 
// suitable source of randomness.
$salt = openssl_random_pseudo_bytes(16);
  
// Using hash_pbkdf2 function
$hash = hash_pbkdf2("md5",
    $gfg, $salt, $iterations, 30);
  
// Display result
echo $hash;
?>

chevron_right


Output:

f0ebbbf59869d76f946c4b15340761

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$gfg = "Contribute1234";
$iterations = 100;
  
// Generate a random IV using 
// openssl_random_pseudo_bytes()
// random_bytes() or another 
// suitable source of randomness.
$salt = openssl_random_pseudo_bytes(8);
  
// Using hash_pbkdf2 function
$hash = hash_pbkdf2("md5",
    $gfg, $salt, $iterations, 20, false);
  
// Display result
echo $hash;
?>

chevron_right


Output:

715b385158045923923c

Reference: http://php.net/manual/en/function.hash-pbkdf2.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.