Enhanced mhash function implemented by PHP, and mhash function implemented by php
When I use the php encryption function mhash today, the following error occurs: Fatal error: Call to undefined function mhash ()
Mhash is a built-in function of php, but an error is reported when it is used ..
The following two methods are summarized:
1. Import the php_mhash.dll extension file. In addition, you need to import libmhash. dll (the load dependency file of the mhash library ),
Load LoadFile C:/php/libmhash. dll in the Apache configuration file Httpd. conf ".
2. Use the custom mhash enhancement function.
Copy codeThe Code is as follows:
Function hmac_md5 ($ key, $ data)
{
If (extension_loaded ('mhash '))
{
Return bin2hex (mhash (MHASH_MD5, $ data, $ key ));
}
$ B = 64;
If (strlen ($ key)> $ B)
{
$ Key = pack ('H * ', md5 ($ key ));
}
$ Key = str_pad ($ key, $ B, chr (0x00 ));
$ Ipad = str_pad ('', $ B, chr (0x36 ));
$ Opad = str_pad ('', $ B, chr (0x5c ));
$ K_ipad = $ key ^ $ ipad;
$ K_opad = $ key ^ $ opad;
Return md5 ($ k_opad. pack ('H * ', md5 ($ k_ipad. $ data )));
}
The $ key and $ data parameters in the hmac_md5 function correspond to the original 3 and 2 parameters of mhash.
Both methods can smoothly use the mhash encryption function of php.