PHP code example of jazor / totp-for-php

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

    

jazor / totp-for-php example snippets


echo TOTP::generate('Y5C4TFC5Q6OZHMXS7NOEDO5AYUP5XWMK', time());

// Seed for HMAC-SHA1 - 20 bytes
$seed = Base32::decode("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ");
// Seed for HMAC-SHA256 - 32 bytes
$seed32 = Base32::decode("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZA====");
// Seed for HMAC-SHA512 - 64 bytes
$seed64 = Base32::decode("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNA=");
$T0 = 0;
$X = 30;
$testTime = [59, 1111111109, 1111111111, 1234567890, 2000000000, 20000000000];

ini_set('date.timezone', 'UTC');
for ($t = 0; $t < count($testTime); $t++) {
    echo sprintf(
        "time: %s, date: %s, code: %s, alg: SHA1\r\n",
        $testTime[$t],
        date('Y-m-d H:i:s', $testTime[$t]),
        TOTP::compute($seed, $testTime[$t], 'sha1', 8, $X, $T0));


    echo sprintf(
        "time: %s, date: %s, code: %s, alg: SHA256\r\n",
        $testTime[$t],
        date('Y-m-d H:i:s', $testTime[$t]),
        TOTP::compute($seed32, $testTime[$t], 'sha256', 8, $X, $T0));

    echo sprintf(
        "time: %s, date: %s, code: %s, alg: SHA512\r\n",
        $testTime[$t],
        date('Y-m-d H:i:s', $testTime[$t]),
        TOTP::compute($seed64, $testTime[$t], 'sha512', 8, $X, $T0));
}

$tests = 'foobar';
for ($i = 1; $i <= mb_strlen($tests, 'utf-8'); $i++){
    $str = mb_substr($tests, 0, $i, 'utf-8');
    $encoded = Base32::encode($str);
    $decoded = Base32::decode($encoded);
    $equal = $decoded === $str ? 'yes' : 'no';
    echo "{$str} => {$encoded} => {$decoded} => {$equal}\r\n";
}

for ($i = 1; $i <= mb_strlen($tests, 'utf-8'); $i++){
    $str = mb_substr($tests, 0, $i, 'utf-8');
    $encoded = Base32::encode($str, Base32::base32_encode_lookup_table_hex);
    $decoded = Base32::decode($encoded, Base32::base32_decode_lookup_table_hex);
    $equal = $decoded === $str ? 'yes' : 'no';
    echo "{$str} => {$encoded} => {$decoded} => {$equal}\r\n";
}