PHP code example of abmmhasan / otp

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

    

abmmhasan / otp example snippets


use Infocyph\OTP\TOTP;

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

$result = $totp->verifyWithWindow(
    otp: $submittedCode,
    cache: $stateCache,
    factorId: 'user-42:totp:secret-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;

$otp = new GenericOtp(
    cache: $stateCache,
    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',
);

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

use Infocyph\OTP\AOTP;

// Enrollment: generate on the client/device where possible.
$keys = AOTP::generateKeyPair();
$clientPrivateKey = $keys->privateKey;
$storedPublicKey = $keys->publicKey;

// Verifier side.
$aotp = new AOTP($storedPublicKey, 'login.example.com');
$flowId = bin2hex(random_bytes(16));
$context = 'login:web:' . $flowId;

$challenge = $aotp->issue(
    cache: $stateCache,
    factorId: 'user-42:aotp:key-v1',
    context: $context,
);

// Client side: this value comes from trusted local flow state established when
// the login was initiated, never by copying challenge->context.
$locallyKnownFlowId = loadLocallyInitiatedFlowId();

$response = AOTP::respond(
    privateKey: $clientPrivateKey,
    challenge: $challenge,
    expectedAudience: 'login.example.com',
    expectedContext: 'login:web:' . $locallyKnownFlowId,
);

// Verifier side.
$result = $aotp->verifyWithResult(
    cache: $stateCache,
    factorId: 'user-42:aotp:key-v1',
    challenge: $challenge,
    response: $response,
);

use Infocyph\OTP\GridOTP;

$secret = GridOTP::generateSecret();
$gridOtp = new GridOTP($stateCache, $secret);
$challenge = $gridOtp->issue('user-42:grid:secret-v1');
$response = GridOTP::respond($challenge, $secret);
$result = $gridOtp->verifyWithResult(
    'user-42:grid:secret-v1',
    $challenge,
    $response,
);

use Infocyph\OTP\MobileOTP;

$mobile = new MobileOTP(
    secret: MobileOTP::generateSecret(),
    pin: '5555',
);

$otp = $mobile->generate();
$result = $mobile->verifyWithWindow(
    otp: $submittedOtp,
    cache: $stateCache,
    factorId: 'user-42:mobile:secret-v1',
);

use Infocyph\OTP\Passkey;

$passkey = new Passkey(
    cache: $stateCache,
    rpId: 'example.com',
    allowedOrigins: ['https://example.com'],
);

$ceremony = $passkey->beginRegistration(
    binding: 'user-42:passkey:registration',
    userHandle: $opaqueStableUserHandle,
    username: '[email protected]',
    displayName: 'Alice',
);

$result = $passkey->finishRegistration(
    binding: 'user-42:passkey:registration',
    ceremonyId: $submittedCeremonyId,
    credentialJson: $browserCredentialJson,
);

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

$recovery = new RecoveryCodes(
    new InMemoryRecoveryCodeStore(),
    $separateRecoveryHmacKey,
);
$batch = $recovery->generate('user-42');
$result = $recovery->consume('user-42', $submittedCode);