added corrected key import from config

This commit is contained in:
tracer 2022-09-22 18:54:23 +02:00
parent b2115a97a8
commit 790176964d
1 changed files with 4 additions and 2 deletions

View File

@ -20,11 +20,13 @@ class EncryptionController
*/
function safeEncrypt(string $message, string $key): string
{
$binKey = sodium_hex2bin(string: $key);
$nonce = random_bytes(length: SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$cipher = base64_encode(string: $nonce . sodium_crypto_secretbox(message: $message, nonce: $nonce, key: $key));
$cipher = base64_encode(string: $nonce . sodium_crypto_secretbox(message: $message, nonce: $nonce, key: $binKey));
sodium_memzero(string: $message);
sodium_memzero(string: $key);
sodium_memzero(string: $binKey);
return $cipher;
}
@ -55,7 +57,7 @@ class EncryptionController
$plain = sodium_crypto_secretbox_open(ciphertext: $ciphertext, nonce: $nonce, key: $binKey);
if ($plain === false) {
throw new Exception(message: 'The message was tampered with in transit');
throw new Exception(message: ' Incorrect key.');
}
sodium_memzero(string: $ciphertext);
sodium_memzero(string: $key);