2014-04-08 18 views
0

我想使用hmac通過電子郵件對用戶進行身份驗證 hmac將與userid一起存儲在數據庫中。從hmac函數中獲取不到輸出

  • 我還需要存儲$payload解密嗎?

我在做對吧?

我沒有得到任何字符串的url查詢字符串?

這是我testpage具有以下

$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

這些是散列算法的可能值。 http://www.php.net/manual/en/function.hash-algos.php

我移動了你的代碼一點點,並擺脫hash_hmac的原始輸出(不知道爲什麼你想這樣),它適用於我。

<?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"; 
    } 
} 
+0

謝謝,但是您是否還必須保存有效載荷,它是否在文檔中? – Richard