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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM