Add QCryptographicHash::supportsAlgorithm() to check supported algorithm

Adds QCH::supportsAlgorithm() method which returns whether the selected
algorithm is supported and we guarantee to get a result when generating
hashes. OpenSSL will be responsible for providing us this information.
Returns TRUE if OpenSSL is not used as a provider.

[ChangeLog][QtCore][QCryptographicHash] Add supportsAlgorithm() method
that can be used to query OpenSSL and check whether the selected
algorithm is supported.

Change-Id: I0d94e02b8c70beb79520150fab6c32bdd1da3fca
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
(cherry picked from commit 0657b0734ef78cbaeb5f9d800df79647790d3163)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Jan Grulich 2022-11-02 09:43:50 +01:00 committed by Qt Cherry-pick Bot
parent 844f338a11
commit 1a4eca9e88
2 changed files with 36 additions and 0 deletions

View File

@ -217,6 +217,7 @@ public:
void addData(QByteArrayView bytes) noexcept; void addData(QByteArrayView bytes) noexcept;
void finalize() noexcept; void finalize() noexcept;
QByteArrayView resultView() const noexcept { return result.toByteArrayView(); } QByteArrayView resultView() const noexcept { return result.toByteArrayView(); }
static bool supportsAlgorithm(QCryptographicHash::Algorithm method);
const QCryptographicHash::Algorithm method; const QCryptographicHash::Algorithm method;
@ -865,6 +866,40 @@ int QCryptographicHash::hashLength(QCryptographicHash::Algorithm method)
return hashLengthInternal(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 QT_END_NAMESPACE
#ifndef QT_NO_QOBJECT #ifndef QT_NO_QOBJECT

View File

@ -91,6 +91,7 @@ public:
#endif #endif
static QByteArray hash(QByteArrayView data, Algorithm method); static QByteArray hash(QByteArrayView data, Algorithm method);
static int hashLength(Algorithm method); static int hashLength(Algorithm method);
static bool supportsAlgorithm(Algorithm method);
private: private:
Q_DISABLE_COPY(QCryptographicHash) Q_DISABLE_COPY(QCryptographicHash)
QCryptographicHashPrivate *d; QCryptographicHashPrivate *d;