env secrets 加密中的简单且无缝的冗余策略。

v0.7.2 2023-06-13 00:11 UTC

This package is auto-updated.

Last update: 2024-09-13 02:48:56 UTC


README

使用此库以实现环境变量加密中的简单且无缝的冗余。

一种强大且精炼的加密策略方法,赋予创新力量。

envPHP旨在增强PHP应用程序的安全性并改进数据处理。它包括处理加密环境变量的功能、生成一次性密码(OTP)以及安全存储和检索敏感数据。

envPHP有助于保护应用程序数据并提高应用程序内部的整体安全性。它利用PHP的内置函数和一些标准加密实践来实现其目标。

增强PHP应用程序安全性

获得一系列强大工具,用于管理加密环境变量、生成一次性密码以及安全存储和检索数据。

通过利用PHP的内置函数和行业标准加密实践,envPHP使用简单的API增强了您的应用程序,以帮助安全地保护数据。

今天尝试envPHP,体验环境变量零依赖加密的不同之处。

要求

  • PHP >= 7.4

该库的一些功能可能与PHP的早期版本不兼容。请在安装envPHP之前检查您的PHP版本,以避免任何兼容性问题。

envPHP在系统中的文件和目录中运行。因此,请确保

文件创建:在某些情况下,envPHP可能需要创建新文件。确保PHP在必要目录中具有文件创建权限。

读写访问:envPHP需要读取和写入将包含.env文件(的)目录,以初始化和操作环境文件。

服务器配置:如果您在Web服务器环境中使用envPHP,请确保服务器允许PHP读取和写入文件、执行必要的系统级操作,并且已启用OpenSSL。

开始之前,请确保满足这些要求,以享受envPHP的流畅体验。

安装

要安装envPHP,请运行以下命令

composer require teamchizkiyahu/envphp

用法

要有效地使用envPHP

//// 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);

请注意,当前envPHP的此实现中使用了hash_pbkdf2,默认要求的迭代次数设置为1,000,000,可以在Consts脚本中调整。

贡献

请考虑在GitHub上报告问题、提交拉取请求、参与讨论和/或提供反馈。

如果您关心我们所做的工作,请考虑联系我们赞助。

请考虑支持我们的方法,通过简化实现流畅的主权来推动下一代无依赖和框架友好的包架构,该架构提炼了愿景、创造力和实用性。

我们希望培养和倡导加强本地和远程社区的工具。

我们的目标是成为一个非营利性集体,寻求在本地社区和远程分布式社区中赋予应用程序开发者权力。

许可

本项目许可协议为Artistic License 2.0。