adasd
All checks were successful
Deploy / deploy (push) Successful in 58s

This commit is contained in:
2026-07-30 00:04:51 +02:00
parent 76591569b6
commit f6cb211859
10 changed files with 261 additions and 22 deletions

View File

@@ -5,6 +5,7 @@ namespace App;
final class Crypto
{
private const ENCRYPTED_PREFIX = 'enc:';
private string $key;
public function __construct(Config $config)
@@ -44,7 +45,7 @@ final class Crypto
}
$nonce = random_bytes(SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES);
$cipher = sodium_crypto_aead_xchacha20poly1305_ietf_encrypt($plaintext, '', $nonce, $this->key);
return base64_encode($nonce . $cipher);
return self::ENCRYPTED_PREFIX . base64_encode($nonce . $cipher);
}
public function decrypt(?string $blob): string
@@ -52,7 +53,8 @@ final class Crypto
if ($blob === null || $blob === '') {
return '';
}
$raw = base64_decode($blob, true);
$payload = str_starts_with($blob, self::ENCRYPTED_PREFIX) ? substr($blob, strlen(self::ENCRYPTED_PREFIX)) : $blob;
$raw = base64_decode($payload, true);
if ($raw === false || strlen($raw) <= SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES) {
return '';
}
@@ -65,4 +67,19 @@ final class Crypto
return '';
}
}
public static function looksEncrypted(?string $blob): bool
{
if ($blob === null || $blob === '') {
return false;
}
$payload = str_starts_with($blob, self::ENCRYPTED_PREFIX) ? substr($blob, strlen(self::ENCRYPTED_PREFIX)) : $blob;
$raw = base64_decode($payload, true);
if ($raw === false) {
return false;
}
return strlen($raw) > SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES;
}
}