2013-04-21 145 views
1

有人可以指導我在實施Google身份驗證應用程序時做了什麼錯誤嗎? 以下是我沒有成功嘗試:谷歌身份驗證應用程序的雙向身份驗證

1#創建密鑰

$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'; // allowed characters in Base32 
$secret = ''; 
for ($i = 0; $i < 16; $i++) 
{ $secret .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } 

2#創建通過谷歌服務(介紹+密鑰)QR碼和掃描智能手機和6位數字每30秒由谷歌認證應用程序生成

3#。現在,我想驗證它...([使用base32算法中] [1])

$tm = floor(time()/30); // for time purpose 
$secretkey=Base32::decode($secretkey); // return blank 
$time=chr(0).chr(0).chr(0).chr(0).pack('N*',$tm+$i); 

// Hash it with users secret key 
$hm = hash_hmac('SHA1', $time, $secretkey, true); 

// Use last nipple of result as index/offset 
$offset = ord(substr($hm,-1)) & 0x0F; 

// grab 4 bytes of the result 
$hashpart=substr($hm,$offset,4); 

// Unpak binary value 
$value=unpack("N",$hashpart); 
$value=$value[1]; 

// Only 32 bits 
$value = $value & 0x7FFFFFFF; 
$value = $value % 1000000; 

上面的代碼是基於其他來源(WordPress的谷歌認證的應用程序).​​.....但它不是加工。

$ secretkey = Base32 :: decode($ secretkey); 總是返回空白(空)

有沒有另一種方法來驗證或實現基於時間的算法?或者我可以閱讀有關如何爲網站進行雙向身份驗證的代碼的鏈接?

感謝您的提前和明顯的努力。 (我認爲它是一種安全的目的問題,所以我已經把這裏的,而不是計算器.. :))

+0

@Ladadadada:感謝改善.. :) – 2013-04-21 19:31:12

+0

TOTP的RFC包含一些可以用作基礎的java代碼(它對我有用)。 https://tools.ietf.org/html/rfc6238 – 2013-04-21 20:48:09

+1

你見過https://github.com/chregu/GoogleAuthenticator.php嗎? – Maerlyn 2013-04-21 21:51:00

回答

1

實現使用github.com/chregu/GoogleAuthenticator.php