PHP Encryption Extension Library-mhash extension Library
What is the Mhash extension library: Mhash is a non-reversible PHP encryption extension library based on discrete mathematical principles, which is not turned on by default. Mhash can be used to create checksum values, message summaries, message authentication codes, and key information that does not need to be saved (such as passwords).
1.Mhash Expansion Library Installation
About Mhash Library installation is similar to the MCrypt expansion Library installation, here is not a long introduction, we can refer to the previous article "PHP Encryption extension Library-mcrypt extension Library"!
2.Mhash Extension Library Constants
The Mhash library supports a variety of hashing algorithms such as MD5,SHA,CRC32, which can be used to output supported algorithm names using the Mhash_count () and Mhash_get_hash_name () functions. Here's a look at the sample code:
<?phpheader ("content-type:text/html; Charset=utf-8 "); $num = Mhash_count (); The function returns the maximum hash Idecho "Mhash library supports:<br>"; for ($i =0; $i <= $num; $i + +) { echo $i. = ". Mhash_get_hash_name ($i)." <br> "."; Output the name of each hash ID}?>
The output is:
Note: If you use the above constants in your actual application, you need to prefix the algorithm name with Mhash_, for example, CRC32 is represented as MHASH_CRC32.
3.Mhash applications
Compared to more than 30 functions of the Mcrypt extension library, there are only 5 functions in the Mhash library, except for the two functions used above, the other 3 functions are described below.
(1) mhash_get_block_size () function
The function syntax is formatted as follows:
int mhash_get_block_size (int $hash)
This function is used to obtain the chunk size of the hash of the parameter, for example: Mhash_get_biock_size (MHASH_CRC32).
(2) Mhash () function
The function syntax is formatted as follows:
String Mhash (int hash,string data[,string key])
The function returns a hash value. The parameter hash is the algorithm to be used, the parameter data is to be encrypted, the parameter key is the key used for encryption.
(3) mhash_keygen_s2k () function
The function syntax is formatted as follows:
String mhash_keygen_s2k (int $hash, string $password, string $salt, int $bytes)
The function returns a key value in bytes based on the parameter password and salt, and the parameter hash is the algorithm to be used. Where the salt is a fixed 8-byte value, if the user gives a small value and 8 bytes, will be 0 to be padded.
The following example uses the MHASH_KEYGEN_S2K () function to generate a checksum and uses the Bin2Hex () function to convert the binary result to 16 binary. The specific code is as follows:
<?phpheader ("content-type:text/html; Charset=utf-8 "); $filename =" 08.txt "; File path $str = file_get_contents ($filename); Reads the contents of the file into the variable $str $hash = 2; Set hash Value $password = "111"; Set Variable $password$salt = "1234"; Set Variable $salt$key = mhash_keygen_s2k (1, $password, $salt, ten); Generate key value $str_mhash =bin2hex (Mhash ($hash, $str, $key)); Use the $key value, the $hash value to encrypt the string $str echo "File 08.txt checksum is:". $str _mhash; Output Check Code?>
The result of the output is;
"Recommended"
1. Related topics: "PHP cryptographic Functions"
2.PHP Encryption Extension Library-mcrypt Extension Library instance usage