diff --git a/src/corelib/tools/qcryptographichash.cpp b/src/corelib/tools/qcryptographichash.cpp index 6044665d28f..8210b7716b6 100644 --- a/src/corelib/tools/qcryptographichash.cpp +++ b/src/corelib/tools/qcryptographichash.cpp @@ -217,6 +217,7 @@ public: void addData(QByteArrayView bytes) noexcept; void finalize() noexcept; QByteArrayView resultView() const noexcept { return result.toByteArrayView(); } + static bool supportsAlgorithm(QCryptographicHash::Algorithm method); const QCryptographicHash::Algorithm method; @@ -865,6 +866,40 @@ int QCryptographicHash::hashLength(QCryptographicHash::Algorithm method) return hashLengthInternal(method); } +/*! + Returns whether the selected algorithm \a method is supported and if + result() will return a value when the \a method is used. + + \note OpenSSL will be responsible for providing this information when + used as a provider, otherwise \c true will be returned as the non-OpenSSL + implementation doesn't have any restrictions. + We return \c false if we fail to query OpenSSL. + + \since 6.5 +*/ + + +bool QCryptographicHash::supportsAlgorithm(QCryptographicHash::Algorithm method) +{ + return QCryptographicHashPrivate::supportsAlgorithm(method); +} + +bool QCryptographicHashPrivate::supportsAlgorithm(QCryptographicHash::Algorithm method) +{ +#ifdef USING_OPENSSL30 + OSSL_PROVIDER_load(nullptr, "legacy"); + OSSL_PROVIDER_load(nullptr, "default"); + + const char *restriction = "-fips"; + EVP_MD_ptr algorithm = EVP_MD_ptr(EVP_MD_fetch(nullptr, methodToName(method), restriction)); + + return algorithm != nullptr; +#else + Q_UNUSED(method); + return true; +#endif +} + QT_END_NAMESPACE #ifndef QT_NO_QOBJECT diff --git a/src/corelib/tools/qcryptographichash.h b/src/corelib/tools/qcryptographichash.h index 5f9104fe9f0..9bf473dd4b0 100644 --- a/src/corelib/tools/qcryptographichash.h +++ b/src/corelib/tools/qcryptographichash.h @@ -91,6 +91,7 @@ public: #endif static QByteArray hash(QByteArrayView data, Algorithm method); static int hashLength(Algorithm method); + static bool supportsAlgorithm(Algorithm method); private: Q_DISABLE_COPY(QCryptographicHash) QCryptographicHashPrivate *d;