PHP code example of infocyph / otp

1. Go to this page and download the library: Download infocyph/otp 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/ */

    

infocyph / otp example snippets


use Infocyph\OTP\TOTP;

$totp = new TOTP(TOTP::generateSecret());
$uri = $totp->getProvisioningUri('[email protected]', 'Example App');
$valid = $totp->verify($submittedCode);

use Infocyph\OTP\Stores\InMemoryReplayStore;

$result = $totp->verifyWithWindow(
    $submittedCode,
    replayStore: new InMemoryReplayStore(), // examples/tests only
    factorId: 'totp-enrollment-v1',
);

use Infocyph\OTP\HOTP;

$hotp = new HOTP(HOTP::generateSecret(), digits: 6, algorithm: 'sha1');
$code = $hotp->generate(counter: 10);
$result = $hotp->verifyWithResult($code, counter: 10);

$matched = $result->matchedCounter; // 10
$persist = $result->nextCounter;    // 11

use Infocyph\OTP\GenericOtp;
use Infocyph\OTP\Stores\InMemoryOtpStore;

$otp = new GenericOtp(
    store: new InMemoryOtpStore(), // examples/tests only
    key: $purposeSpecificApplicationKey,
    digits: 6,
    ttlSeconds: 300,
    maxAttempts: 3,
);

$code = $otp->generate('login-challenge-123');
$valid = $otp->verify('login-challenge-123', $submittedCode);

use Infocyph\OTP\OCRA;

$ocra = new OCRA(
    'OCRA-1:HOTP-SHA256-8:C-QN08-PSHA1',
    '12345678901234567890123456789012', // raw key for RFC integrations
);

$code = $ocra->generate(
    challenge: '12345678',
    counter: 4,
    pin: '1234',
);

use Infocyph\OTP\RecoveryCodes;
use Infocyph\OTP\Stores\InMemoryRecoveryCodeStore;

$recovery = new RecoveryCodes(
    new InMemoryRecoveryCodeStore(), // examples/tests only
    $separateRecoveryHmacKey,
);
$batch = $recovery->generate('user-42'); // XXXX-XXXX-XXXX, about 60 bits
$result = $recovery->consume('user-42', $submittedCode);