PHP技术论坛

标题: PHP使用HMAC-SHA1+base64签名方法 [打印本页]

作者: admin    时间: 2016-11-22 11:59
标题: PHP使用HMAC-SHA1+base64签名方法
  1. function getSignature($str, $key) {
  2.             $signature = "";
  3.             if (function_exists('hash_hmac')) {
  4.                     $signature = base64_encode(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 = base64_encode($hmac);
  24.             }
  25.             return $signature;
  26.     }
复制代码







欢迎光临 PHP技术论坛 (http://php.hh85.com/) Powered by Discuz! X3.2