summaryrefslogtreecommitdiff
path: root/classes/Crypt.php
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2025-04-08 13:52:00 +0300
committerAndrew Dolgov <fox@fakecake.org>2025-04-08 13:52:00 +0300
commit17b4e98249462a1feb71586d10cd5293d9487ab8 (patch)
tree8f3d8afbbf73ae7d59791a3347a557c194f691d0 /classes/Crypt.php
parent597971f2382c273106b6205b35f7810a4ed78476 (diff)
spaces to tabs
Diffstat (limited to 'classes/Crypt.php')
-rw-r--r--classes/Crypt.php34
1 files changed, 17 insertions, 17 deletions
diff --git a/classes/Crypt.php b/classes/Crypt.php
index d832e6530..2b3a7b788 100644
--- a/classes/Crypt.php
+++ b/classes/Crypt.php
@@ -1,15 +1,15 @@
<?php
class Crypt {
- /** the only algo supported at the moment */
+ /** the only algo supported at the moment */
private const ENCRYPT_ALGO = 'xchacha20poly1305_ietf';
- /** currently only generates keys using sodium_crypto_aead_chacha20poly1305_keygen() i.e. one supported Crypt::ENCRYPT_ALGO
- * @return string random 256-bit (for ChaCha20-Poly1305) binary string
- */
- static function generate_key() : string {
- return sodium_crypto_aead_chacha20poly1305_keygen();
- }
+ /** currently only generates keys using sodium_crypto_aead_chacha20poly1305_keygen() i.e. one supported Crypt::ENCRYPT_ALGO
+ * @return string random 256-bit (for ChaCha20-Poly1305) binary string
+ */
+ static function generate_key() : string {
+ return sodium_crypto_aead_chacha20poly1305_keygen();
+ }
/** encrypts provided ciphertext using Config::ENCRYPTION_KEY into an encrypted object
*
@@ -18,8 +18,8 @@ class Crypt {
static function encrypt_string(string $ciphertext) : array {
$key = Config::get(Config::ENCRYPTION_KEY);
- if (!$key)
- throw new Exception("Crypt::encrypt_string() failed to encrypt - key is not available");
+ if (!$key)
+ throw new Exception("Crypt::encrypt_string() failed to encrypt - key is not available");
$nonce = \random_bytes(\SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES);
@@ -47,16 +47,16 @@ class Crypt {
static function decrypt_string(array $encrypted_data) : string {
$key = Config::get(Config::ENCRYPTION_KEY);
- if (!$key)
- throw new Exception("Crypt::decrypt_string() failed to decrypt - key is not available");
+ if (!$key)
+ throw new Exception("Crypt::decrypt_string() failed to decrypt - key is not available");
- // only one is supported for the time being
- switch ($encrypted_data['algo']) {
- case self::ENCRYPT_ALGO:
- return sodium_crypto_aead_xchacha20poly1305_ietf_decrypt($encrypted_data['payload'], '', $encrypted_data['nonce'], hex2bin($key));
- }
+ // only one is supported for the time being
+ switch ($encrypted_data['algo']) {
+ case self::ENCRYPT_ALGO:
+ return sodium_crypto_aead_xchacha20poly1305_ietf_decrypt($encrypted_data['payload'], '', $encrypted_data['nonce'], hex2bin($key));
+ }
- throw new Exception('Crypt::decrypt_string() failed to decrypt passed encrypted data object, unsupported algo: ' . $encrypted_data['algo']);
+ throw new Exception('Crypt::decrypt_string() failed to decrypt passed encrypted data object, unsupported algo: ' . $encrypted_data['algo']);
}
} \ No newline at end of file