2017-02-03 58 views
1

我有一個JavaScript函數可以創建基於時間的一次性密碼(TOTP)。 現在我必須使用PHP在服務器上創建相同的TOTP。 我在PHP中編寫了與我的javascript函數相同的邏輯。 爲了方便起見,我創建了一個JavaScript小提琴和一個PHP小提琴。所以你可以比較代碼並查看輸出。HMAC Javascript函數的結果不如PHP

的JavaScript版本:(小提琴:https://jsfiddle.net/rrfk4ey9/1/

var TOTP = function() { 
 
    var dec2hex = function(s) { 
 
     return (s < 15.5 ? "0" : "") + Math.round(s).toString(16); 
 
    }; 
 
    var hex2dec = function(s) { 
 
     return parseInt(s, 16); 
 
    }; 
 
    var leftpad = function(s, l, p) { 
 
     if(l + 1 >= s.length) { 
 
      s = Array(l + 1 - s.length).join(p) + s; 
 
     } 
 
     return s; 
 
    }; 
 
    
 
    this.getOTP = function(secret) { 
 
     try { 
 
      var epoch = Math.round(new Date().getTime()/1000.0); 
 
      
 
      // For testing, we take a fixed time. (same as in PHP version). 
 
      var time = "0000000002f3e3c9";//leftpad(dec2hex(Math.floor(epoch/30)), 16, "0"); 
 
      
 
      document.getElementById("key").innerHTML = secret; 
 
      document.getElementById("time").innerHTML = time; 
 
      
 
      var hmacObj = new jsSHA(time, "HEX"); 
 
      var hmac = hmacObj.getHMAC(secret, "TEXT", "SHA-1", "HEX"); 
 
      
 
      document.getElementById("hmac-out").innerHTML = hmac; 
 
      
 
      var offset = hex2dec(hmac.substring(hmac.length - 1)); 
 
      var otp = (hex2dec(hmac.substr(offset * 2, 8)) & hex2dec("7fffffff")) + ""; 
 
      otp = (otp).substr(otp.length - 6, 6); 
 
      
 
     } catch (error) { 
 
\t  alert("Error: " + error); 
 
      throw error; 
 
     } 
 
     
 
     return otp; 
 
    }; 
 
    
 
}; 
 
var totpObj = new TOTP(); 
 
    document.getElementById("result").innerHTML = totpObj.getOTP("someSecret");
output { 
 
    font-family: monospace; 
 
    white-space: pre; 
 
} 
 
#result { 
 
    color: orange; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jsSHA/1.6.0/sha.js"></script> 
 
<body> 
 
<aside>This snippet uses external library <b>jsSHA</b> version <b>1.6.0</b></aside> 
 
<hr /> 
 
<output> 
 
    <div>Key: <b id="key"></b></div> 
 
    <div>Time: <b id="time"></b></div> 
 
    <div>HMAC: <b id="hmac-out"></b></div> 
 
    <div>code: <b id="result"></b></div> 
 
</output> 
 

 
</body>

PHP版本:http://ideone.com/s0Bwqu

<?php 
function leftPad($in, $len, $str) { 

    return str_pad($in, $len, $str, STR_PAD_LEFT); 
} 

$key = "someSecret"; 

$epoch = time(); 
// For testing, we take a fixed time. (same as in JS version). 
$time = "0000000002f3e3c9";//leftPad(dechex(floor($epoch/30)), 16, "0"); 
echo "Key:  " . $key . "\n"; 
echo "Time: " . $time . "\n"; 

$hmac = hash_hmac("sha1", $time, $key, false); 
echo "HMAC: " . $hmac . "\n"; 

$offset = hexdec(substr($hmac, strlen($hmac) - 1)); 
$otp = (hexdec(substr($hmac, $offset * 2, 8)) & hexdec("7fffffff")) . ""; 
$otp = substr($otp, strlen($otp) - 6, 6); 

echo "Code: " . $otp . "\n"; 

?> 

這產生了:

Key:  someSecret 
Time: 0000000002f3e3c9 
HMAC: 5dcab54740bdca71e706c7e38a5c59fec3cb9c1a 
Code: 094428 

注意,在兩個版本(JS和PHP)的timekey是相同的。 HMAC不同,所以在我的理解這裏開始的問題。 JavaScript版本是我首先創建的版本,並且經過驗證可以正常工作。

我很確定這個問題是由jsSHA庫的行爲引起的。 所以我把一些事情的角度:

  • jsSHA圖書館HEX格式需要time。同樣在PHP中,當放入hash_hmac函數時,時間是十六進制格式。
  • secretkey)以TEXT格式放入jsSHA。也在PHP中。

實際上,重點是在PHP中獲得與javascript中的jsSHA庫相同的結果。 我確定我錯過了一些東西。我一直在嘗試,甚至谷歌也不知道答案。

+2

請直接在您的問題中發佈代碼,而不是通過第三方服務。 – Narf

回答

3

你告訴jsSHA庫,輸入的是十六進制字符串在這裏:

var hmacObj = new jsSHA(time, "HEX");

但你沒有做任何事情相當於在PHP端前/ hash_hmac()調用中。這意味着jsSHA獲取原始二進制數據,而PHP的hash_hmac()獲取「0000000002f3e3c9」的文字字符串。
當然,這不會產生相同的結果。

在將其傳遞到hash_hmac()之前應用hex2bin()$time

+0

謝謝,這是答案。 +1 – user2190492