PHP code example of virgil / sdk

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

    

virgil / sdk example snippets


use Virgil\Sdk\Web\Authorization\CallbackJwtProvider;
use Virgil\Sdk\Web\Authorization\TokenContext;

$authenticatedQueryToServerSide = function (TokenContext $context){
    // Get generated token from server-side
    return "eyJraWQiOiI3MGI0NDdlMzIxZjNhMGZkIiwidHlwIjoiSldUIiwiYWxnIjoiVkVEUzUxMiIsImN0eSI6InZpcmdpbC1qd3Q7dj0xIn0.eyJleHAiOjE1MTg2OTg5MTcsImlzcyI6InZpcmdpbC1iZTAwZTEwZTRlMWY0YmY1OGY5YjRkYzg1ZDc5Yzc3YSIsInN1YiI6ImlkZW50aXR5LUFsaWNlIiwiaWF0IjoxNTE4NjEyNTE3fQ.MFEwDQYJYIZIAWUDBAIDBQAEQP4Yo3yjmt8WWJ5mqs3Yrqc_VzG6nBtrW2KIjP-kxiIJL_7Wv0pqty7PDbDoGhkX8CJa6UOdyn3rBWRvMK7p7Ak";
};

// Setup AccessTokenProvider
$accessTokenProvider = new CallbackJwtProvider($authenticatedQueryToServerSide);

use Virgil\Crypto\VirgilCrypto;
use Virgil\Sdk\Web\Authorization\JwtGenerator;

// App Key (you got this Key at Virgil Dashboard)
$privateKeyStr = "MC4CAQAwBQYDK2VwBCIEIH2RKUdXkK/3tfVWO2AJahOhCYG2hCEHg4mPJEAuvmj7";
$appKeyData = base64_decode($privateKeyStr);

// VirgilCrypto imports a private key into a necessary format
$crypto = new VirgilCrypto();
$privateKey = $crypto->importPrivateKey($appKeyData);

// use your App Credentials you got at Virgil Dashboard:
$appId = "be00e10e4e1f4bf58f9b4dc85d79c77a"; // App ID
$appKeyId = "70b447e321f3a0fd";              // App Key ID
$ttl = 3600; // 1 hour (JWT's lifetime)

// setup JWT generator with necessary parameters:
$jwtGenerator = new JwtGenerator($privateKey->getPrivateKey(), $appKeyId, $crypto, $appId, $ttl);

// generate JWT for a user
// remember that you must provide each user with his unique JWT
// each JWT contains unique user's identity (in this case - Alice)
// identity can be any value: name, email, some id etc.
$identity = "Alice";
$token = $jwtGenerator->generateToken($identity);

// as result you get users JWT, it looks like this: "eyJraWQiOiI3MGI0NDdlMzIxZjNhMGZkIiwidHlwIjoiSldUIiwiYWxnIjoiVkVEUzUxMiIsImN0eSI6InZpcmdpbC1qd3Q7dj0xIn0.eyJleHAiOjE1MTg2OTg5MTcsImlzcyI6InZpcmdpbC1iZTAwZTEwZTRlMWY0YmY1OGY5YjRkYzg1ZDc5Yzc3YSIsInN1YiI6ImlkZW50aXR5LUFsaWNlIiwiaWF0IjoxNTE4NjEyNTE3fQ.MFEwDQYJYIZIAWUDBAIDBQAEQP4Yo3yjmt8WWJ5mqs3Yrqc_VzG6nBtrW2KIjP-kxiIJL_7Wv0pqty7PDbDoGhkX8CJa6UOdyn3rBWRvMK7p7Ak"
// you can provide users with JWT at registration or authorization steps
// Send a JWT to client-side
$token->__toString();

use Virgil\Crypto\VirgilCrypto;
use Virgil\Sdk\Verification\VerifierCredentials;
use Virgil\Sdk\Verification\VirgilCardVerifier;
use Virgil\Sdk\Verification\Whitelist;

// initialize Crypto library
$crypto = new VirgilCrypto();

$publicKey = $crypto->importPublicKey("EXPORTED_PUBLIC_KEY");

$yourBackendVerifierCredentials = new VerifierCredentials("YOUR_BACKEND", $publicKey);

$yourBackendWhitelist = new Whitelist([$yourBackendVerifierCredentials]);

$cardVerifier = new VirgilCardVerifier($crypto, true, true, [$yourBackendWhitelist]);

use Virgil\Sdk\CardManager;
use Virgil\Sdk\Verification\VirgilCardVerifier;

$cardVerifier = new VirgilCardVerifier($virgilCrypto, true, true);

// initialize cardManager and specify accessTokenProvider, cardVerifier
$cardManager = new CardManager($virgilCrypto, $accessTokenProvider, $cardVerifier);

use Virgil\Crypto\VirgilCrypto;
use Virgil\Sdk\CardParams;

$crypto = new VirgilCrypto();

// generate a key pair
$keyPair = $crypto->generateKeyPair();

// save Alice private key into key storage
$privateKeyStorage->store($keyPair->getPrivateKey(), "Alice");

// create and publish user's card with identity Alice on the Cards Service
$card = $cardManager->publishCard(
    CardParams::create(
        [
            CardParams::PublicKey  => $keyPair->getPublicKey(),
            CardParams::PrivateKey => $keyPair->getPrivateKey(),
        ]
    )
);

use Virgil\Crypto\Core\VirgilKeys\VirgilPublicKeyCollection;

// prepare a message
$dataToEncrypt = 'Hello, Bob!';

// load a private key from a device storage
$alicePrivateKeyEntry = $privateKeyStorage->load('Alice');

// using cardManager search for Bob's cards on Cards Service
$cards = $cardManager->searchCards('Bob');

$bobRelevantCardsPublicKeys = array_map(
    function (Virgil\Sdk\Card $cards) {
        return $cards->getPublicKey();
    },
    $cards
);

$bobRelevantCardsPublicKeysCollection = new VirgilPublicKeyCollection($bobRelevantCardsPublicKeys);

// sign a message with a private key then encrypt using Bob's public keys
$encryptedData = $crypto->signAndEncrypt(
    $dataToEncrypt,
    $alicePrivateKeyEntry->getPrivateKey(),
    $bobRelevantCardsPublicKeysCollection
);

use Virgil\Crypto\Core\VirgilKeys\VirgilPublicKeyCollection;

// load a private key from a device storage
$bobPrivateKeyEntry = $privateKeyStorage->load('Bob');

// using cardManager search for Alice's cards on Cards Service
$cards = $cardManager->searchCards('Alice');

$aliceRelevantCardsPublicKeys = array_map(
    function (Virgil\Sdk\Card $cards) {
        return $cards->getPublicKey();
    },
    $cards
);

$aliceRelevantCardsPublicKeysCollection = new VirgilPublicKeyCollection($aliceRelevantCardsPublicKeys);

// decrypt with a private key and verify using one of Alice's public keys
$decryptedData = $crypto->decryptAndVerify(
    $encryptedData,
    $bobPrivateKeyEntry->getPrivateKey(),
    $aliceRelevantCardsPublicKeysCollection
);

// using cardManager get a user's card from the Cards Service
$card = $cardManager->getCard("f4bf9f7fcbedaba0392f108c59d8f4a38b3838efb64877380171b54475c2ade8");

// using cardManager search for user's cards on Cards Service
$cards = $cardManager->searchCards("Bob");

// using cardManager revoke user's card on Cards Service by card id
$cardManager->revokeCard("f4bf9f7fcbedaba0392f108c59d8f4a38b3838efb64877380171b54475c2ade8");

use \Virgil\Crypto\VirgilCrypto;

$crypto = new VirgilCrypto();

$keyPair = $crypto->generateKeyPair();

use Virgil\Sdk\Storage\PrivateKeyStorage;
use Virgil\Crypto\VirgilCrypto;

$crypto = new VirgilCrypto();

$keyPair = $crypto->generateKeyPair();
$storage = new PrivateKeyStorage($crypto, '/var/www/storage');

$storage->store($keyPair->getPrivateKey(), 'alicePk');

$alicePk = $storage->load('alicePk');
bash
php -m

PHP Warning:  PHP Startup: Unable to load dynamic library 'vsce_phe_php' (tried: /usr/lib/php7/modules/vsce_phe_php (Error loading shared library /usr/lib/php7/modules/vsce_phe_php: No such file or directory), /usr/lib/php7/modules/vsce_phe_php.so (Error loading shared library vscf_foundation_php.so: No such file or directory (needed by /usr/lib/php7/modules/vsce_phe_php.so))) in Unknown on line 0