How to Use the mhash_keygen_s2k() Function in PHP
The aim of this snippet is to represent to you the PHP mhash_keygen_s2k() function. Read on and check out the example for better understanding.
PHP has an inbuilt <kbd class="highlighted">mhash_keygen_s2k()</kbd> function, used for creating a key according to the given hash with the help of a user-provided password.
The mhash extension was removed in PHP 7.0.0. This function is no longer available in modern PHP versions. For secure key derivation, use hash_pbkdf2() instead.
It is the salted S2K algorithm, indicated in the OpenPGP document (RFC 2440). It is especially handy for computing checksums, message digests, and so on.
Note that for checking the key, the salt should also be known.
The syntax of the <kbd class="highlighted">mhash_keygen_s2k()</kbd> function is demonstrated below:
php mhash_keygen_s2k() function syntax
string mhash_keygen_s2k(int $hash, string $password, string $salt, int $bytes)From the syntax, you can see that this function accepts four parameters. The description of those parameters is given below:
- $hash: Specifies the hash algorithm ID. It must be one of the
MHASH_*constants. - $password: Specifies the user-provided password.
- $salt: Specifies random data used as additional input for a one-way function that hashes a password or passphrase. Its length is fixed at 8 bytes.
- $bytes: Specifies the length of the generated key in bytes.
On success, the function returns the created key as a string, and false otherwise.
For a better understanding, here is an example:
php use mhash_keygen_s2k() function
<?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)));
?>php use mhash_keygen_s2k() function, output
e2dfb845290aae21