The mhash_keygen_s2k() function is an in-built function in PHP which is used to generate a key according to the given hash, using a user-provided password. This is the salted S2K algorithm specified in the OpenPGP document (RFC 2440). This function can be used to compute the checksum, message digests, etc.
A Salt is a random piece of data used to generate the key. To check the key, you must also know the salt, so it is a good idea to append the salt too.
Syntax:
string mhash_keygen_s2k(int $hash, string $password, string $salt, int $bytes)
Parameter: This function accepts four parameters as mentioned above and described below:
- $Hash: This parameter holds the hash ID i.e. one of the mhash_name constants.
- $Password: This parameter holds the user’s password.
- $Salt: A salt is random data that is used as an additional input to a one-way function that hashes data, a password or passphrase. Salt has a fixed length of 8 bytes and will be padded with zeros if you supply less bytes.
- $Bytes: This parameter represents the key length, in bytes.
Return Value: This function returns the generated key as a string, or FALSE on error.
Below program illustrates the mhash_keygen_s2k() function in PHP:
Program:
PHP
<?php $inputString = "p4ssw0rd" ; $salt = "agejkhgeuka" ; $bytes = "8" ; // bin2hex is used to convert binary // to hex string print_r(bin2hex(mhash_keygen_s2k( MHASH_MD5, $inputString , $salt , $bytes ))); ?> |
Output:
e2dfb845290aae21