For my particular PHP (8.2) Web Application I am storing keys and secrets in an .ini
file outside of root in a folder /private
.
I have been requested to encrypt the data in the ini file (reason stating that the php code is accessing the key details without any security measures).
$cfg = parse_ini_file('../../private/config.ini');
$my_secret_key_123 = $cfg['secret_key_123'];
$my_secret_key_345 = $cfg['secret_key_345'];
I was thinking I could just encrypt all of the data using sodium
:
$key = sodium_crypto_secretbox_keygen();
$nonce = random_bytes( SODIUM_CRYPTO_SECRETBOX_NONCEBYTES );
$encrypted_result = sodium_crypto_secretbox( 'secret_key_123', $nonce, $key );
$encoded_secret_key_123 = base64_encode( $nonce . $encrypted_result );
However, if I put that encoded secret key in my .ini
file and call it, I will need to have the $key
and $nonce
used to decrypt it:
$decoded = base64_decode($encoded_secret_key_123, false);
$nonce = mb_substr($decoded, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit');
$encrypted_result = mb_substr($decoded, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit');
$plaintext = sodium_crypto_secretbox_open($encrypted_result, $nonce, $key);
So the question arises...where and how would I store the key sodium_crypto_secretbox_keygen()
without also encrypting that?
Perhaps this is simply overkill and I should just encode my .ini
secrets with simple base64
?