php hash_hmac HMAC_SHA1 加密

 
    /**
     * @brief 使用HMAC-SHA1算法生成oauth_signature签名值
     *
     * @param $key  密钥
     * @param $str  源串
     *
     * @return 签名值
     */
1
function getSignature($str, $key) { 2 $signature = ""; 3 if (function_exists('hash_hmac')) { 4 $signature = bin2hex(hash_hmac("sha1", $str, $key, true)); 5 } else { 6 $blocksize = 64; 7 $hashfunc = 'sha1'; 8 if (strlen($key) > $blocksize) { 9 $key = pack('H*', $hashfunc($key)); 10 } 11 $key = str_pad($key, $blocksize, chr(0x00)); 12 $ipad = str_repeat(chr(0x36), $blocksize); 13 $opad = str_repeat(chr(0x5c), $blocksize); 14 $hmac = pack( 15 'H*', $hashfunc( 16 ($key ^ $opad) . pack( 17 'H*', $hashfunc( 18 ($key ^ $ipad) . $str 19 ) 20 ) 21 ) 22 ); 23 $signature = bin2hex($hmac); 24 } 25 return $signature; 26 }

 

posted @ 2019-09-23 10:37  没事就更  阅读(1970)  评论(0编辑  收藏  举报