Skip to content

Instantly share code, notes, and snippets.

@caiquecastro
Created September 21, 2016 13:52
Show Gist options
  • Save caiquecastro/eee6b70177d6b8b0f3d0acfe976dff0d to your computer and use it in GitHub Desktop.
Save caiquecastro/eee6b70177d6b8b0f3d0acfe976dff0d to your computer and use it in GitHub Desktop.
TOTP library for PHP
{
"name": "caique/otp",
"authors": [
{
"name": "Caique de Castro",
"email": "[email protected]"
}
],
"require": {
"rych/otp": "^1.1",
"christian-riesen/otp": "^2.2"
}
}
<?php
use Base32\Base32;
use Otp\GoogleAuthenticator;
require "vendor/autoload.php";
$secret = "5DWZSI6QPOAQGKZQFP3JE2BRO44HEDUL"; #fgets($handle);
$code = "885578";
echo "O secret é $secret e o code é $code.";
// Generates a 20-byte (160-bit) secret key
//$otpSeed = Seed::generate();
//
// // -OR- use a pre-generated string
#$otpSeed = new Seed('ThisIsMySecretSeed');
$totp = new \Rych\OTP\TOTP($secret);
//$secret = GoogleAuthenticator::generateRandom();
$otp = new \Otp\Otp();
echo "Validation from current lib: \n";
var_dump($totp->validate($code));
echo "validation from other lib:\n";
var_dump($otp->checkTotp(Base32::decode($secret), $code));
// // Display secret key details
#printf("Secret (HEX): %s\n", $otpSeed->getValue(Seed::FORMAT_HEX));
#printf("Secret (BASE32): %s\n", $otpSeed->getValue(Seed::FORMAT_BASE32));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment