This article mainly introduces the enhanced mhash function implemented in PHP. When using the default mhash function, an error is reported and two solutions are found. Friends in need can refer to it
When I used PHP’s encryption function mhash today, I got an error: Fatal error: Call to undefined function mhash()
mhash is a built-in function of php but an error is reported when using it..
After some research, we summarized two methods:
1. Import the php_mhash.dll extension file. In addition, import libmhash.dll (the loading of the mhash library depends on this file),
Load “LoadFile C:/php/libmhash.dll” in Apache’s configuration file Httpd.conf.
2. Use custom mhash enhancement function.
The 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 parameters $key and $data in the hmac_md5 function correspond to the original 3,2 parameters of mhash.
Both of these methods can be used smoothly using PHP’s mhash encryption function