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";
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型