PHP的HMAC_SHA1算法实现

function getSignature($str, $key) {  
    $signature = "";  
    if (function_exists('hash_hmac')) {
        $signature = bin2hex(hash_hmac("sha1", $str, $key, true));
    } else {
        $blocksize = 64;  
        $hashfunc = 'sha1';  
        if (strlen($key) > $blocksize) {  
            $key = pack('H*', $hashfunc($key));  
        }  
        $key = str_pad($key, $blocksize, chr(0x00));  
        $ipad = str_repeat(chr(0x36), $blocksize);  
        $opad = str_repeat(chr(0x5c), $blocksize);  
        $hmac = pack(  
            'H*', $hashfunc(  
              ($key ^ $opad) . pack(  
                'H*', $hashfunc(  
                  ($key ^ $ipad) . $str  
              )  
           )  
         )  
        );  
        $signature = bin2hex($hmac);
    }  
    return $signature;  
}

原文地址:https://www.codelovers.cn/article/20180905102023.html

你可能感兴趣的:(PHP)