PHP code example of teamchizkiyahu / envphp

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

    

teamchizkiyahu / envphp example snippets



//// Setup env file with necessary encryption keys ////

// Instantiate TinyEnvPHP
use TCENVPHP\Modules\Api\TinyEnvPHP;

$envPHP = new TinyEnvPHP();

// Initialize Random OTP & Salt
$key = $envPHP->initOtp();
$otp = $key['votp'];
$salt = $key['vsalt'];

// Set key for hash value
$hkey = 'hashed_id';
// Obtain a Hash value of the OTP
$hashed_id = $envPHP->setHash($otp);
// Add to $key array
$key[$hkey] = $hashed_id;


/*
One approach to efficiently and securely prepare as a string 
the OTP, Salt, & Hash are scrambled and encrypted with AES 
for persistently storing the keys to access the encrypted env keys that are used for encryption of env secrets
*/
$encryptedKeyArray = $envPHP->secStore($key);
// One example to store the $key array
update_option('encryptedKeyArray', $encryptedKeyArray);

// Intialize env file with zero knowledge of encrypted RSA public and private keys, hash, and scramble key
global $root_dir;
$envPHP->initEnvFile($root_dir, $otp, $salt, true);
$envPHP->memoryWipe($key, $otp, $salt, $hkey, $hashed_id, $encryptedKeyArray);

    



//// Store secret in env *example* ////
use TCENVPHP\Modules\Api\TinyEnvPHP;

if (isset($_POST['secret']) && isset($_POST['secret_i'])) {

// Retrieve encrypted key array value as a string
$encryptedKeyArray = get_option('encryptedKeyArray');

$envPHP = new TinyEnvPHP();

/* 
Descramble and decrypt key string back into an array of the original values 
given by initOtp() with hashed_id appended to the array via setHash()
that were previously passed to secStore()
*/
$key = $envPHP->secRecall($encryptedKeyArray);
$otp = $key['votp'];
$salt = $key['vsalt'];
$hashed_id = $key['hashed_id'];

// Verify the integrity of OTP with the Hash Value that was generated with the otp
$hashVerify = $envPHP->verifyHash($otp, $hashed_id);
if ($hashVerify) {

    // prepare secrets to be stored in the env file
    $envStore = [
            'SECRET_API_KEY' => $post['secret'],
            'SECRET_API_KEY_I' => $post['secret_i'],
        ];

    // utilizes encrypted keys to encrypt the envStore secrets and store them persistently in the env file that was previously created via initEnvFile()
    global $root_dir;
    $envPHP->wInitEnv($root_dir, $envStore, $otp, $salt);
    $envPHP->memoryWipe($envStore);
    }
    $envPHP->memoryWipe($post['secret'],  $post['secret_i'], $encryptedKeyArray, $key, $otp, $salt, $hashed_id, $hashVerify);
}
    




//// Retrieve env values for use ////
use TCENVPHP\Modules\Api\TinyEnvPHP;

// Instantiate TinyEnvPHP
$envPHP = new TinyEnvPHP();

// Retrieve encrypted key array value as a string
$encryptedKeyArray = get_option('encryptedKeyArray');

// One approach to efficiently and securely decrypt the OTP, Salt, & Hash with descrambling and AES decryption 
$key = $envPHP->secRecall($encryptedKeyArray);
$otp = $key['votp'];
$salt = $key['vsalt'];
$hashed_id = $key['hashed_id'];

// Verify the integrity of OTP with the Hash Value that was generated with the otp
$hashVerify = $envPHP->verifyHash($otp, $hashed_id);
if ($hashVerify) {

    global $root_dir;
    // Retrieves an env secret and decrypts value
    $decryptedSecret = $envPHP->getInitEnvValue($root_dir, 'SECRET_API_KEY', $otp, $salt);
    $decryptedSecret_i = $envPHP->getInitEnvValue($root_dir, 'SECRET_API_KEY_I', $otp, $salt);

    $keyData = compact('decryptedSecret', 'decryptedSecret_i');

    $envPHP->memoryWipe($decryptedSecret, $decryptedSecret_i);

    // Return env secrets decrypted and ready for use as an array
    return $keyData;
}

$envPHP->memoryWipe($encryptedKeyArray, $key, $otp, $salt, $hashed_id, $hashVerify);