PHP encryption extension library-detailed explanation of usage of Mhash extension library examples

黄舟
Release: 2023-03-07 12:58:01
Original
3504 people have browsed it

PHP encryption extension library—Mhash extension library

What is the Mhash extension library: Mhash is an irreversible PHP encryption extension library based on the principle of discrete mathematics , which is not enabled by default. mhash can be used to create verification values, message digests, message authentication codes, and save key information (such as passwords) without the original text.

1.Mhash extension library installation

The installation of Mhash library is similar to the installation of Mcrypt extension library. I won’t introduce it in detail here. You can refer to it. Previous article "PHP Encryption Extension Library—Mcrypt Extension Library"!

2.Mhash extension library constants

The Mhash library supports multiple hashing algorithms such as MD5, SHA, CRC32, etc., and can be output using the mhash_count() and mhash_get_hash_name() functions Supported algorithm names. Let’s take a look at the sample code:

"; for($i=0;$i<=$num;$i++){ echo $i."=>".mhash_get_hash_name($i)."
".""; //输出每一个hash id 的名称 } ?>
Copy after login

The output result is:

PHP encryption extension library-detailed explanation of usage of Mhash extension library examples

Note: If you use the above constants in actual applications, you need to The algorithm name is prefixed with MHASH_, for example, CRC32 is represented as MHASH_CRC32.

3.Mhash application

Compared with more than 30 functions in the Mcrypt extension library, there are only 5 functions in the Mhash library, except for the two used above. In addition to these functions, the following three other functions are introduced.

(1)mhash_get_block_size() function

The function syntax format is as follows:

int mhash_get_block_size ( int $hash )
Copy after login

This function is used to obtain the block size of the parameter hash, such as :mhash_get_biock_size(MHASH_CRC32).

(2)mhash() function

The function syntax format is as follows:

string mhash(int hash,string data[,string key])
Copy after login

This function returns a hash value. The parameter hash is the algorithm to be used, the parameter data is the data to be encrypted, and the parameter key is the key used for encryption.

(3) mhash_keygen_s2k() function

The function syntax format is as follows:

string mhash_keygen_s2k ( int $hash , string $password , string $salt, int $bytes )
Copy after login

This function will return a unit of words according to the parameters password and salt The key value of the section, the parameter hash is the algorithm to be used. The salt is a fixed 8-byte value. If the value given by the user is less than 8 bytes, it will be padded with 0s.

In the following example, the mhash_keygen_s2k() function is used to generate a check code, and the bin2hex() function is used to convert the binary result to hexadecimal. The specific code is as follows:

Copy after login

The output result is;

PHP encryption extension library-detailed explanation of usage of Mhash extension library examples


##[Related recommendations]

1. Relevant topic recommendations: "

PHP Encryption Function"

2.

PHP Encryption Extension Library—Mcrypt Extension Library Instance Usage



The above is the detailed content of PHP encryption extension library-detailed explanation of usage of Mhash extension library examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!