PHP code example of ixnode / php-vault

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

    

ixnode / php-vault example snippets




xnode\PhpVault\PHPVault;

/* Path to private key and .env.enc */
$pathToPrivateKey = __DIR__.'/.keys/private.key';
$pathToEncryptedEnv = __DIR__.'/.env.enc';

/* - Initiate PHPVault Core.
 * - Load private key.
 * - Load the encrypted env file.
 */
$phpVault = new PHPVault();
$phpVault->loadPrivateKeyFromFile($pathToPrivateKey);
$phpVault->importEncryptedEnvFile($pathToEncryptedEnv);

/* Usage */
$dbUser = getenv('PHPVAULT_DB_USER');
$dbPass = getenv('PHPVAULT_DB_PASS');
$dbHost = getenv('PHPVAULT_DB_HOST');
$dbName = getenv('PHPVAULT_DB_NAME');



xnode\PhpVault\PHPVault;

/* Path to private key and .env.enc */
$pathToEncryptedEnv = __DIR__.'/.env.enc';

/* - Initiate PHPVault Core and use the PRIVATE_KEY environment variable.
 * - Load the encrypted env file.
 */
$phpVault = new PHPVault();
$phpVault->importEncryptedEnvFile($pathToEncryptedEnv);

/* Usage */
$dbUser = getenv('PHPVAULT_DB_USER');
$dbPass = getenv('PHPVAULT_DB_PASS');
$dbHost = getenv('PHPVAULT_DB_HOST');
$dbName = getenv('PHPVAULT_DB_NAME');
bash
$ composer 
bash
$ vendor/bin/php-vault generate-keys --persist

The key pair is written to folder ".keys"

Never add the private key to the repository!
bash
# Create file .env.enc
$ vendor/bin/php-vault set .env.enc DB_USER secret.user "DB Configs" --public-key --create
# Adds values to .env.enc
$ vendor/bin/php-vault set .env.enc DB_PASS secret.pass --public-key
$ vendor/bin/php-vault set .env.enc DB_HOST secret.host --public-key
$ vendor/bin/php-vault set .env.enc DB_NAME secret.name --public-key
bash
$ vendor/bin/php-vault display .env --display-decrypted
+---------+-------------+-------------+
| Key     | Value       | Description |
+---------+-------------+-------------+
| DB_USER | secret.user | DB Configs  |
| DB_PASS | secret.pass |             |
| DB_HOST | secret.host |             |
| DB_NAME | secret.name |             |
+---------+-------------+-------------+
bash
$ git clone https://github.com/ixnode/php-vault.git && cd php-vault
$ composer install