From bfb998f7cea223c0b74033661fe44078b09d44a8 Mon Sep 17 00:00:00 2001 From: JensFalk Date: Mon, 21 Sep 2026 18:08:42 +0200 Subject: [PATCH] Fehlende ALTCHA-Bibliotheksdateien ergaenzt --- .../Vendor/AltchaOrg/Altcha/V1/Obfuscator.php | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 fp_altcha_spamschutz/src/Vendor/AltchaOrg/Altcha/V1/Obfuscator.php diff --git a/fp_altcha_spamschutz/src/Vendor/AltchaOrg/Altcha/V1/Obfuscator.php b/fp_altcha_spamschutz/src/Vendor/AltchaOrg/Altcha/V1/Obfuscator.php new file mode 100644 index 0000000..5d71c30 --- /dev/null +++ b/fp_altcha_spamschutz/src/Vendor/AltchaOrg/Altcha/V1/Obfuscator.php @@ -0,0 +1,64 @@ +maxNumber] inclusive is chosen. + * + * @return string Base64-encoded bytes of ciphertext followed by the 16‑byte GCM authentication tag ($ciphertext . $tag). + */ + public function obfuscateData(string $raw, string $key = '', ?int $counter = null): string + { + $cipher = 'AES-256-GCM'; + $keyHash = $this->hasher->hash(Algorithm::SHA256, $key); + + $ivLength = openssl_cipher_iv_length($cipher); + + if (false === $ivLength) { + throw new \RuntimeException('Getting cipher iv length failed.'); + } + + $iv = ''; // AES‑GCM initialization vector (IV), typically 12 bytes for AES-256-GCM + $num = $counter ?? $this->randomInt(); + + // Fill IV from the counter, one byte at a time (little‑endian) + for ($i = 0; $i < $ivLength; $i++) { + $iv .= \chr(abs($num % 256)); + $num = intdiv($num, 256); + } + + $encryptedData = openssl_encrypt($raw, $cipher, $keyHash, \OPENSSL_RAW_DATA, $iv, $tag); + + if (!$encryptedData) { + throw new \RuntimeException('Data encryption failed.'); + } + + return base64_encode($encryptedData . $tag); + } + + private function randomInt(): int + { + return random_int(0, $this->maxNumber); + } +}