PHP code example of oire / iridium

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

    

oire / iridium example snippets


use Oire\Iridium\Base64;
use Oire\Iridium\Exception\Base64Exception;

$text = "The quick brown fox jumps over the lazy dog";
$encoded = Base64::encode($text);
echo $encoded.PHP_EOL;

$encoded = Base64::encode($text, true);
echo $encoded.PHP_EOL;

$encoded = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw";

try {
    $decoded = Base64::decode($encoded);
} catch(Base64Exception $e) {
    // Handle errors
}

echo $decoded.PHP_EOL;

use Oire\Iridium\Key\SharedKey;

$sharedKey = SharedKey::create();
$key = $sharedKey->getKey();
// Save the key instead
echo $key . PHP_EOL;

use Oire\Iridium\Crypt;
use Oire\Iridium\Key\SharedKey;

$data = 'Mischief managed!';
$sharedKey = new SharedKey($key);
$encrypted = Crypt::encrypt($data, $sharedKey);

$decrypted = Crypt::decrypt($encrypted, $sharedKey);

use Oire\Iridium\Exception\PasswordException;
use Oire\Iridium\Key\SharedKey;
use Oire\Iridium\Password;

// You should have $key somewhere in an environment variable
$sharedKey = new SharedKey($key);

try {
    $storeMe = Password::lock($_POST['password'], $sharedKey);
} catch (PasswordException $e) {
    // Handle errors
}

try {
    $isPasswordValid = Password::check($_POST['password'], $hashFromDatabase, $sharedKey);
} catch (PasswordException $e) {
    // Handle errors. Something went wrong: most often it's a wrong or corrupted key
}

if ($isPasswordValid) {
    // OK
} else {
    // Wrong password
}

if (Password::needsRehash($hashFromDatabase, $sharedKey)) {
    // Re-lock the password with the same key
    $newHash = Password::lock($_POST['password'], $sharedKey);
    // Store $newHash in the database
}

use Oire\Iridium\Storage\PdoTokenStorage;

$storage = new PdoTokenStorage($pdoConnection);
// Or with a custom table name:
$storage = new PdoTokenStorage($pdoConnection, 'my_tokens_table');

use Oire\Iridium\SplitToken;
use Oire\Iridium\Storage\PdoTokenStorage;

// You should have set your $dbConnection first as a PDO instance
$storage = new PdoTokenStorage($dbConnection);
$splitToken =  SplitToken::create(
        storage: $storage,
        expirationTime: time() + 1800,
        userId: 123,
        tokenType: 3,
        additionalInfo: '{"some": "data"}'
    )
    ->persist();

use Oire\Iridium\Exception\InvalidTokenException;
use Oire\Iridium\SplitToken;

try {
    $splitToken = SplitToken::fromString($token, $storage);
} catch (InvalidTokenException $e) {
    // Something went wrong with the token: either it is invalid, not found or has been tampered with
}

if ($splitToken->isExpired()) {
    // The token is correct but expired
}

// Given that $splitToken contains a valid token
$splitToken = $splitToken->revokeToken(true);

$deletedTokens = SplitToken::clearExpiredTokens($storage);
shell
docker compose build
docker compose up -d
docker compose exec php composer install
docker compose exec php vendor/bin/phpunit
shell
docker compose exec php vendor/bin/psalm
shell
docker compose build
docker compose up -d
docker compose exec php composer install
docker compose exec php vendor/bin/phpunit
docker compose exec php vendor/bin/psalm
docker compose exec php vendor/bin/php-cs-fixer fix
docker compose down