dongzhaiqiang6108 2014-04-08 15:47
浏览 61

从hmac函数中得不到输出

I want to use hmac to authenticate a user by email The hmac will be stored in the database along with the userid.

  • Do I also need to store the $payload for decryption as well?

Am I doing it right?

I don't get any string in the url querystring?

This is my testpage with the following

$secret = "dfjhglkhniuh65645";
$payload= "234|somedata";

$hmac = hash_hmac("sha2", $payload, $secret, true);
//$hmac = base64_encode($hmac);
if (! function_exists("hash_hmac")) {
    echo "hmac function does not exist";
}
$uri="test.php?hash=$hmac";

if(!isset($_GET['hash'])){
    header('location: ' . $uri);
    exit();
}
echo "testpage<br><br>";
if(isset($_GET['hash']) && !empty($_GET['hash']))
{
    $sig = $_GET['hash'];
    $expected_sig = hash_hmac("sha2", $payload, $secret, true);
    if($expected_sig === $sig)echo "verification succeeded";

}
  • 写回答

1条回答 默认 最新

  • dongzuoyue6556 2014-04-08 16:12
    关注

    These are the possible values for hashing algorithms. http://www.php.net/manual/en/function.hash-algos.php

    I moved your code a little bit, and got rid of the raw output for hash_hmac (not sure why you wanted that) and it works for me.

    <?php
    
    if (!function_exists("hash_hmac")) {
        echo "hmac function does not exist";
        die();
    }
    
    $secret = "dfjhglkhniuh65645";
    $payload = "234|somedata";
    
    $hmac = hash_hmac('sha256', $payload, $secret);
    $uri = "test.php?hash=$hmac";
    
    if (!isset($_GET['hash'])) {
        header('location: ' . $uri);
        exit();
    }
    
    echo "testpage<br><br>";
    if (isset($_GET['hash']) && !empty($_GET['hash'])) {
        $sig = $_GET['hash'];
        $expected_sig = hash_hmac("sha256", $payload, $secret);
        if ($expected_sig === $sig) {
            echo "verification succeeded";
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!