Google authenticator 谷歌身份驗證,實現動態口令
google authenticator php 服務端
使用PHP類
require_once '../PHPGangsta/GoogleAuthenticator.php';
$ga = new PHPGangsta_GoogleAuthenticator(); //創建一個新的"安全密匙SecretKey" //把本次的"安全密匙SecretKey" 入庫,和賬戶關系綁定,客戶端也是綁定這同一個"安全密匙SecretKey"
$secret = $ga->createSecret();
echo "安全密匙SecretKey: ".$secret."\n\n";
$qrCodeUrl = $ga->getQRCodeGoogleUrl('www.iamle.com', $secret); //第一個參數是"標識",第二個參數為"安全密匙SecretKey" 生成二維碼信息
echo "Google Charts URL for the QR-Code: ".$qrCodeUrl."\n\n"; //Google Charts接口 生成的二維碼圖片,方便手機端掃描綁定安全密匙SecretKey
$oneCode = $ga->getCode($secret); //服務端計算"一次性驗證碼"
echo "服務端計算的驗證碼是:".$oneCode."\n\n";
//把提交的驗證碼和服務端上生成的驗證碼做對比
//$secret 服務端的 "安全密匙SecretKey"
// $oneCode 手機上看到的 "一次性驗證碼"
// 最后一個參數 為容差時間,這里是2 那么就是 2* 30 sec 一分鍾.
// 這里改成自己的業務邏輯
$checkResult = $ga->verifyCode($secret, $oneCode, 2);
if ($checkResult) {
echo '匹配! OK';
} else {
echo '不匹配! FAILED';
}
服務端例子:
安全密匙SecretKey: NI5RHMWOTBIY6KP4
Google Charts URL for the QR-Code: https://chart.googleapis.com/chart?chs=200×200&chld=M|0&cht=qr&chl=otpauth%3A%2F%2Ftotp%2Fwwek–www.iamle.com%3Fsecret%3DNI5RHMWOTBIY6KP4
服務端計算出的驗證碼是:255466
匹配! OK
Google authenticator 手機客戶端
在您手機的應用市場搜索“Google身份驗證器”或“Google Authenticator”,下載安裝即可。擁有Google身份驗證器的市場有:Google Play;應用匯;安卓市場;百度移動應用;優億市場;安智市場 等
iOS移動設備
應用市場,搜索“Google Authenticator”,下載安裝即可。
測試上面的例子
手機中的驗證碼 和 服務端的驗證碼 都是 255466 驗證通過