Home >Backend Development >PHP Tutorial >PHP encryption extension library-detailed explanation of usage of Mhash extension library examples

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

黄舟
黄舟Original
2017-05-25 16:15:003585browse

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:

<?php
header("Content-Type:text/html; charset=utf-8");
$num = mhash_count();  //函数返回最大的hash id
echo "mhash库支持的算法有:<br>";
for($i=0;$i<=$num;$i++){
    echo $i."=>".mhash_get_hash_name($i)."<br>"."";       //输出每一个hash id 的名称
}
?>

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 )

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])

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 )

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:

<?php
header("Content-Type:text/html; charset=utf-8");
$filename = "08.txt";                          // 文件路径
$str = file_get_contents($filename);            //读取文件内容到变量$str中
$hash = 2 ;                                     //设置hash值
$password = "111";                             //设置变量$password
$salt = "1234";                                //设置变量$salt
$key = mhash_keygen_s2k(1,$password,$salt,10);  //生成key 值
$str_mhash =bin2hex(mhash($hash,$str,$key));    //使用$key 值、$hash值对字符串$str 加密
echo "文件 08.txt 的校验码是:".$str_mhash;  //输出校验码
?>

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!

Statement:
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