Tutorialspoint.dev

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:



<?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;
?>

Output:

f0ebbbf59869d76f946c4b15340761

Program 2:

<?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;
?>

Output:

715b385158045923923c

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



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter