PHP code example of jnativel / sentinel-vault

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

    

jnativel / sentinel-vault example snippets


use SentinelVault\SentinelVault;

// Initialize the vault
$vault = (new SentinelVault())
    ->setMasterKeyB64($_ENV['MASTER_KEY_B64']);

// Generate a DEX key for a user
$dex = $vault->createDexKey(
    'dex:user:123:key:main',
    'user:123:scope:profile',
    ['exp' => '2026-01-01T00:00:00Z', 'status' => 'ACTIVE']
);

// Encrypt and decrypt user data
$cipher = $vault->encryptWithDex($dex['dex_blob'], 'dex:user:123:key:main', 'Sensitive Data');
$plain  = $vault->decryptWithDex($dex['dex_blob'], 'dex:user:123:key:main', $cipher);

$newDexBlob = $vault->rotateDexMasterKey(
    $oldDexBlob,
    'dex:user:123:key:main',
    $_ENV['NEW_MASTER_KEY_B64'],
    'dex:user:123:key:main:v2'
);

$rekeyedDexBlob = $vault->rekeyDexUserKey($dexBlob, 'dex:user:123:key:main');

$meta = $vault->getDexMeta($dexBlob, 'dex:user:123:key:main');
print_r($meta);

   $master = SentinelVault::generateMasterKeyB64();
   putenv("MASTER_KEY_B64=$master");
   

   $dex = $vault->createDexKey("dex:user:1:key:default", "user:1:data");
   

   $cipher = $vault->encryptWithDex($dex['dex_blob'], "dex:user:1:key:default", "hello world");
   

   $new = SentinelVault::generateMasterKeyB64();
   $vault->rotateDexMasterKey($dex['dex_blob'], "dex:user:1:key:default", $new);