PHP code example of ahtishamashraf / astra512-ffi

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

    

ahtishamashraf / astra512-ffi example snippets


use Astra512\Astra512;
use Astra512\Key;

$astra = Astra512::create(getenv('ASTRA512_LIB') ?: null);
$key = Key::generate();                   // 32 random bytes
$aad = "app:v1";
$pt  = "hello";

[$ct, $tag] = $astra->encrypt($key, $aad, $pt);
$out = $astra->decrypt($key, $aad, $ct, $tag);

use Astra512\Astra512;
use Astra512\Password;

$astra = Astra512::create(getenv('ASTRA512_LIB') ?: null);
$password = "correct horse battery staple";
[$salt, $key] = Password::deriveKey($password);   // returns [16-byte salt, 32-byte key]

[$ct, $tag] = $astra->encrypt($key, "app:v1", "secret");

// To decrypt later, re-derive the key with the saved salt:
$key2 = Password::deriveKeyWithSalt($password, $salt);
$out  = $astra->decrypt($key2, "app:v1", $ct, $tag);