Home  >  Article  >  Backend Development  >  Implementation code of php enhanced mhash function

Implementation code of php enhanced mhash function

WBOY
WBOYOriginal
2016-07-25 08:57:15940browse
This article introduces the function code of an enhanced mhash implemented in PHP. Friends in need can refer to it.

The encryption function mhash of PHP is used in the program, and the error is reported: Fatal error: Call to undefined function mhash()

Two solutions are provided below for reference. 1. Import the php_mhash.dll extension file, and 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.

<?php
/**
* 自定义的mhash增强函数
* edit by bbs.it-home.org
*/
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)));
}
?>

Code description: The parameters $key and $data in the hmac_md5 function correspond to the original 3,2 parameters of mhash. Both methods can successfully use PHP's mhash encryption function.



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