php执行sha256和hmac_sha256

最近,在PHP项目中使用到了hmac_sha256加密方式。于是,研究了下sha256和hmac_sha256的加密方式,将使用方法记录下来,为其他同学提供参考。

<?php
$params = array(
            'title' => '测试标题',
            'content' => '测试内容',
            'original' => 1,
            'subTitle' => '测试副标题',
            'reward' => 1,
            'originalUrl' => '',
            'date' => 1591264724237,
            'redirect' => true,
            'coverUrls' => array('aaa','bbb'),
            'tag' => array('djdksld','sdfjllsdkfjsa'),
        );
$content1 = json_encode($params, JSON_UNESCAPED_UNICODE);
echo "sha256 result= " . strtoupper(hash("sha256", $content1));
echo "\n";

$hash = hash_hmac('sha256', $content1, 'mykey');
echo "hash hmac sha256 result= " .  $hash;
?>

计算结果如下所示:

sha256 result= 5BDC1244FA29B6EBE31E0756F6BD169F1DE5F8BCA2F0BD016A3043EF3FDBF76B
hash hmac sha256 result= 89aa21e2237ed2e0c2d672f8bbcc4b920b582167a4ed571c32e34bee7241d83e

猜你喜欢

转载自blog.csdn.net/chinawangfei/article/details/106623402
今日推荐