#What we are going to learn this time is another Hash encryption extension. However, this extension Mhash has been integrated into the Hash extension. At the same time, it should be noted that this extension is no longer recommended. We should directly use the functions in the Hash extension to perform Hash encryption operations. Therefore, we still understand for the purpose of learning today. Regarding the content of Hash extension, we can check out the previous article: PHP Hash Information Summary Extension Framework.
Use of cryptographic hash function
$hash = mhash(MHASH_MD5, "测试Mhash"); echo $hash, PHP_EOL; echo bin2hex($hash), PHP_EOL; // /�8�><�۠�P4q�j� // 2fcb38e93e3cc8dba09f503471846a9d $hash = hash('md5', "测试Mhash"); echo $hash, PHP_EOL; // 2fcb38e93e3cc8dba09f503471846a9d $hash = mhash(MHASH_MD5, "测试Mhash", 'hmac secret'); echo $hash, PHP_EOL; echo bin2hex($hash), PHP_EOL; // �k�<F�m �OM���� // b86bb83c46b76d09be4f4daf18ebfe85
As can be seen from the code, the use of mhash() function and hash() are very similar. Of course, their functions Same thing. However, what is encrypted by the mhash() function is directly binary. After we convert this content into hexadecimal through bin2hex(), we can see that the structure encrypted by the ordinary hash() function is exactly the same.
When performing hmac encryption, just add key directly to the third parameter.
Traverse all supported algorithm types
Of course, just like Hash encryption, Mhash encryption can also choose different algorithms. We can also directly use the relevant functions to see the encryption algorithms supported in the current environment.
echo mhash_count(), PHP_EOL; $nr = mhash_count(); // 33 for ($i = 0; $i <= $nr; $i++) { echo sprintf("Hash:%s,块大小为: %d\n", mhash_get_hash_name($i), mhash_get_block_size($i)); } // Hash:CRC32,块大小为: 4 // Hash:MD5,块大小为: 16 // Hash:SHA1,块大小为: 20 // Hash:HAVAL256,块大小为: 32 // Hash:,块大小为: 0 // Hash:RIPEMD160,块大小为: 20 // Hash:,块大小为: 0 // Hash:TIGER,块大小为: 24 // Hash:GOST,块大小为: 32 // Hash:CRC32B,块大小为: 4 // Hash:HAVAL224,块大小为: 28 // Hash:HAVAL192,块大小为: 24 // Hash:HAVAL160,块大小为: 20 // Hash:HAVAL128,块大小为: 16 // Hash:TIGER128,块大小为: 16 // Hash:TIGER160,块大小为: 20 // Hash:MD4,块大小为: 16 // Hash:SHA256,块大小为: 32 // Hash:ADLER32,块大小为: 4 // Hash:SHA224,块大小为: 28 // Hash:SHA512,块大小为: 64 // Hash:SHA384,块大小为: 48 // Hash:WHIRLPOOL,块大小为: 64 // Hash:RIPEMD128,块大小为: 16 // Hash:RIPEMD256,块大小为: 32 // Hash:RIPEMD320,块大小为: 40 // Hash:,块大小为: 0 // Hash:SNEFRU256,块大小为: 32 // Hash:MD2,块大小为: 16 // Hash:FNV132,块大小为: 4 // Hash:FNV1A32,块大小为: 4 // Hash:FNV164,块大小为: 8 // Hash:FNV1A64,块大小为: 8 // Hash:JOAAT,块大小为: 4
PHP also provides a lot of constants to represent these algorithms, such as the MHASH_MD5 we used in the previous code. In fact, just add MHASH_ in front of the content we traverse. The specific list of supported constants can be found in the official manual, so we will not copy and paste it here.
Salted S2K algorithm generates password digest
In addition, Mhash also provides us with a very convenient Salted S2K algorithm that can be used to easily generate a very convenient set of Password encrypts content.
// OpenPGP 指定的 Salted S2K 算法 $hashPassword = mhash_keygen_s2k(MHASH_SHA1, '我的密码', random_bytes(2), 4); echo $hashPassword, PHP_EOL; echo bin2hex($hashPassword), PHP_EOL; // �-!= // 101ab899
Of course, this algorithm is also relatively safe. It has a salt parameter, and it can specify the length of the returned data. It also returns binary data. If you need to save standard text content, you need to convert it into hexadecimal form. But relatively speaking, I think it is safer to directly generate binary content.
Summary
Different functions have different application scenarios, but in fact, Mhash has no special application scenarios. After all, the related functions in the Hash extension are fully capable It has replaced its function, and it is richer and easier to use. If you see the use of these functions in old projects, you can slowly replace them with new functions through reconstruction.
Test code:
https://github.com/zhangyue0503/dev-blog/blob/master/php/202007/source/PHP%E7%9A%84Mhash%E6%89%A9%E5%B1%95%E5%87%BD%E6%95%B0%E7%9A%84%E5%AD%A6%E4%B9%A0.php
Recommended learning: php video tutorial