php openssl_pbkdf2 example

by Anish

Posted on Wednesday December 12 , 2018


In cryptography, PBKDF1 and PBKDF2 (Password-Based Key Derivation Function 2) are key derivation functions with a sliding computational cost, aimed to reduce the vulnerability of encrypted keys to brute force attacks.

The PBKDF2 key derivation function has five input parameters:

DK = PBKDF2(PRF, Password, Salt, c, dkLen)

where:

  • PRF is a pseudorandom function of two parameters with output length hLen (e.g. a keyed HMAC)
  • Password is the master password from which a derived key is generated
  • Salt is a sequence of bits, known as a cryptographic salt
  • c is the number of iterations desired
  • dkLen is the desired bit-length of the derived key
  • DK is the generated derived key

php openssl function openssl_pbkdf2: Generates a PKCS5 v2 PBKDF2 string, defaults to SHA-1

  • Supported PHP Versions (PHP 5 >= 5.5.0, PHP 7)

The Syntax

string  openssl_pbkdf2 ( string  $password , string  $salt , int  $key_length , int  $iterations [, string  $digest_algorithm ] )

Best Practice for PBKDF2 :

  • Use a unique 64-bit salt for each password.
  • Use SHA-512
  • Use iteration count of at least 10000, the more you can go do measure the server performance.

Examples

openssl_pbkdf2() example Deriving key for my given password

<?php  
/**  
 * Created by https://8gwifi.org  
  * User: Anish Nath  
 * Date: 2018-12-17 * Time: 14:44 */  
echo "Deriving password for my Given Password\n";  
echo "PBKDF2 Example of using SHA algorithms, with random salt value of 16 and the output keylength of 32\n";  
  
$password= "myverystrongpassword";  
$salt=openssl_cipher_iv_length(16);  
$key_length =32;  
$iterations=10000;  
$dk = openssl_pbkdf2($password,salt,$key_length,$iterations,"SHA");  
echo base64_encode($dk);

The above example will output something similar to:

$ /usr/bin/php pbkdf2.php
Deriving password for my Given Password

UhkRrY90Uu7JYNtBXZJszLr4vZ0NaAvBCRDhoNLPcZA=

Continue Reading how to generate ec key pair

Generate PBKDF2 Keys Online


Thanku for reading !!! Give a Share for Support

Asking for donation sound bad to me, so i'm raising fund from The Modern Cryptography CookBook for Just $9. Leanpub books Discount coupon first 100 reader. No hurry read the sample chapters here then decide.

Alternatively you can buy My all four Cryptography book Just $10.99


Referefce
  • The Modern Cryptography Book.
  • Python Cryptography
  • Cryptography for JavaScript Developer
  • Go lang Cryptography for developers


python Cryptography Topics
Topics
For Coffee/ Beer/ Amazon Bill and further development of the project Support by Purchasing, The Modern Cryptography CookBook for Just $9 Coupon Price

Cryptography for Python Developers

Cryptography for JavaScript Developers

Go lang ryptography for Developers