PHP code example of nacosvel / 2fa
1. Go to this page and download the library: Download nacosvel/2fa library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nacosvel / 2fa example snippets
use Nacosvel\Authenticator\Authentication;
$secret = Authentication::generateSecret(20, false);
// string(32) "RIPPSUOU3EQBR3FXML2QL43SRYGWCKY3"
use Nacosvel\Authenticator\HOTP;
use Nacosvel\Authenticator\TOTP;
$hotpToken = HOTP::generateToken($secret, 30, 6, 'sha1');
$totpToken = TOTP::generateToken($secret, 30, 6, 'sha1');
// string(6) "577349"
// string(6) "981726"
use Nacosvel\Authenticator\HOTP;
use Nacosvel\Authenticator\TOTP;
$hotpValidate = HOTP::validate($secret, $hotpToken, 30, 6, 'sha1', 3);
$totpValidate = TOTP::validate($secret, $totpToken, 30, 6, 'sha1', 3);
// bool(true)
// bool(true)
use Nacosvel\Authenticator\HOTP;
use Nacosvel\Authenticator\TOTP;
$hotpURI = HOTP::buildURI($secret, 'Mr.Alex', 'Github', 30, 6, 'sha1')->toString();
$totpURI = TOTP::buildURI($secret, 'Mr.Alex', 'Github', 30, 6, 'sha1')->toString();
// string(118) "otpauth://hotp/Github:Mr.Alex?secret=LWN4DKX2KHW4X7VMSWNIRJVHW4F4SQ4Z&counter=30&digits=6&algorithm=sha1&issuer=Github"
// string(117) "otpauth://totp/Github:Mr.Alex?secret=RIPPSUOU3EQBR3FXML2QL43SRYGWCKY3&period=30&digits=6&algorithm=sha1&issuer=Github"
use Nacosvel\Authenticator\URI;
$uri = URI::fromString('otpauth://totp/Github:Mr.Alex?secret=RIPPSUOU3EQBR3FXML2QL43SRYGWCKY3&period=30&digits=6&algorithm=sha1&issuer=Github');
var_dump([
'type' => $uri->getType(), // totp
'issuer' => $uri->getIssuer(), // Github
'account' => $uri->getAccount(), // Mr.Alex
'secret' => $uri->getSecret(), // RIPPSUOU3EQBR3FXML2QL43SRYGWCKY3
'digits' => $uri->getDigits(), // 6
'algorithm' => $uri->getAlgorithm(),// sha1
]);
use Nacosvel\Authenticator\URI;
$hotpURI = URI::buildURI('hotp', 'Mr.Alex', 'Github')->secret($secret)->toString();
$totpURI = URI::buildURI('totp', 'Mr.Alex', 'Github')->secret($secret)->toString();
// string(83) "otpauth://hotp/Github:Mr.Alex?secret=LWN4DKX2KHW4X7VMSWNIRJVHW4F4SQ4Z&issuer=Github"
// string(83) "otpauth://totp/Github:Mr.Alex?secret=RIPPSUOU3EQBR3FXML2QL43SRYGWCKY3&issuer=Github"