value ); return new CipherValue($nonce, $cipher); } /** * Decrypts the provided cipher value using a shared key and optional additional authenticated data. * * @param CipherValue $cipherValue The cipher value containing encrypted data and nonce. * @param SharedKey $key The shared key used for decryption. * @param string $additionalData Optional additional authenticated data that was included during encryption. * @throws CipherDecryptionFailedException If decryption fails or the integrity check is invalid. */ public function decrypt(CipherValue $cipherValue, SharedKey $key, string $additionalData = ''): string { $result = sodium_crypto_aead_xchacha20poly1305_ietf_decrypt( $cipherValue->cipher, $additionalData, $cipherValue->nonce, $key->value ); if ($result === false) { throw new CipherDecryptionFailedException('Cipher could not be decrypted', 1762465681); } return $result; } }