PHP技术论坛

搜索
查看: 1319|回复: 0
打印 上一主题 下一主题

PHP使用HMAC-SHA1+base64签名方法

[复制链接]

83

主题

88

帖子

411

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
411
跳转到指定楼层
楼主
发表于 2016-11-22 11:59:43 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  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.     }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|PHP  

GMT+8, 2024-4-29 02:55 , Processed in 0.058735 second(s), 25 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表