本文共 1254 字,大约阅读时间需要 4 分钟。
我们有时候需要用到 HMAC-SHA256 算法,就是在哈希算法的基础上加上了密钥,安全性更高。
hash_hmac ( string $algo , string $data , string $key [, bool $raw_output = FALSE ] ) : string
参数 algo 要使用的哈希算法名称,例如:“md5”,“sha256”,“haval160,4” 等。 如何获取受支持的算法清单,请参见 hash_hmac_algos() 函数。data
要进行哈希运算的消息。key
使用 HMAC 生成信息摘要时所使用的密钥。raw_output
设置为 TRUE 输出原始二进制数据, 设置为 FALSE 输出小写 16 进制字符串class robotReadSkillService{ const READ_SKILL_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; public function checkSign($request) { $signingContent = 'aaaaaaa'; $signature = $this->signature( $signingContent, self::READ_SKILL_SECRET, $this->getEncryptMethod('TSK-HMAC-SHA256-BASIC') ); return $signature == $authorizationArr['signature']; } public function signature($input, $key, $alg, $raw_output = false) { if (empty($alg)) { return ''; } return hash_hmac($alg, $input, $key, $raw_output); } private function getEncryptMethod($string) { $arr = [ 'TSK-HMAC-SHA256-BASIC' => 'sha256' ]; if (isset($arr[$string])) { return $arr[$string]; } else { throw new \Exception('签名算法' . $string . '未定义!'); } }}
转载地址:http://lgxui.baihongyu.com/