Repository navigation
Conversation
UnsupportedCipherException was documented on every cipher method but never thrown. Without defuse/php-encryption, which is only suggested, new Crypt() and the Crypto cipher failed with "Class Defuse\Crypto\Key not found", and Crypto::isSupported() itself fatalled instead of returning false. Sodium::isSupported() returned true unconditionally. Crypto::isSupported() now checks that the library is installed and Sodium::isSupported() that the sodium functions exist. encrypt(), decrypt() and generateKey() of all ciphers throw UnsupportedCipherException when the cipher is not supported. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary of Changes
CipherInterfaceandCryptdocumentUnsupportedCipherExceptionon every method, but nothing ever throws it, and twoisSupported()implementations are wrong:defuse/php-encryptionis only inrequire-dev/suggest, yetCipher\Cryptois the default cipher ofCrypt. Without the library,new Crypt()and everyCryptomethod fail withError: Class "Defuse\Crypto\Key" not found, andCrypto::isSupported()itself fatals (it referencesDefuse\Crypto\RuntimeTests) instead of returningfalse.Sodium::isSupported()returnstrueunconditionally, although ext-sodium is optional.This PR:
Crypto::isSupported()returnsfalsewhendefuse/php-encryptionis not installed (before running the library's self-test).Sodium::isSupported()returnsfunction_exists('sodium_crypto_box'), which is also true whenparagonie/sodium_compatprovides the functions.encrypt(),decrypt()andgenerateKey()ofCrypto,SodiumandOpenSSLthrowUnsupportedCipherExceptionwhenstatic::isSupported()isfalse. This coversnew Crypt()without arguments, which generates aCryptokey in the constructor.Behaviour change: in an environment where defuse's runtime self-test fails,
Cryptonow throwsUnsupportedCipherExceptionup front instead ofEncryptionException/DecryptionException/InvalidKeyException("Cannot safely …"). All of them implementCryptExceptionInterface.The alternative of moving
defuse/php-encryptiontorequireis not part of this PR; it would make the package depend on it even for users of the other ciphers. The removal of the unusedParagonIE\Sodium\Compatimport is left to a separate PR.Testing Instructions
New
Tests/Cipher/UnsupportedCipherTest.php:isSupported()returnsfalsemust throwUnsupportedCipherExceptionfromencrypt(),decrypt()andgenerateKey()(fails before: the methods ran or failed differently);testCryptoWithoutDefuseLibraryrunsTests/Cipher/stubs/crypto-without-defuse.phpin a separate PHP process that can only autoload the package's own classes, and expectsCrypto::isSupported() === falseandUnsupportedCipherExceptionfrom theCryptomethods andnew Crypt()(fails before withError);Sodium::isSupported()matchesfunction_exists('sodium_crypto_box').Documentation Changes Required
docs/overview.md("Handling failures"):UnsupportedCipherExceptionis now thrown; a table explains when each cipher'sisSupported()is true, replacing the note aboutSodium::isSupported()always returningtrue.🤖 Generated with Claude Code