remove throw, catching exceptions in place

This commit is contained in:
tracer 2022-09-29 19:22:33 +02:00
parent 0268262e98
commit 19576dd6b7
1 changed files with 34 additions and 30 deletions

View File

@ -15,11 +15,10 @@ class EncryptionController
* @param string $message - message to encrypt * @param string $message - message to encrypt
* @param string $key - encryption key * @param string $key - encryption key
* @return string * @return string
* @throws SodiumException
* @throws Exception
*/ */
function safeEncrypt(string $message, string $key): string function safeEncrypt(string $message, string $key): string
{ {
try {
$binKey = sodium_hex2bin(string: $key); $binKey = sodium_hex2bin(string: $key);
$nonce = random_bytes(length: SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); $nonce = random_bytes(length: SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
@ -28,6 +27,9 @@ class EncryptionController
sodium_memzero(string: $key); sodium_memzero(string: $key);
sodium_memzero(string: $binKey); sodium_memzero(string: $binKey);
return $cipher; return $cipher;
} catch (Exception|SodiumException $e) {
die($e->getMessage() . PHP_EOL);
}
} }
/** /**
@ -36,11 +38,10 @@ class EncryptionController
* @param string $encrypted - message encrypted with safeEncrypt() * @param string $encrypted - message encrypted with safeEncrypt()
* @param string $key - encryption key * @param string $key - encryption key
* @return string * @return string
* @throws SodiumException
* @throws Exception
*/ */
function safeDecrypt(string $encrypted, string $key): string function safeDecrypt(string $encrypted, string $key): string
{ {
try {
$binKey = sodium_hex2bin(string: $key); $binKey = sodium_hex2bin(string: $key);
$decoded = base64_decode(string: $encrypted); $decoded = base64_decode(string: $encrypted);
@ -62,6 +63,9 @@ class EncryptionController
sodium_memzero(string: $ciphertext); sodium_memzero(string: $ciphertext);
sodium_memzero(string: $key); sodium_memzero(string: $key);
return $plain; return $plain;
} catch(Exception|SodiumException $e) {
die($e->getMessage());
}
} }
} }