QCryptographicHash Class

The QCryptographicHash class provides a way to generate cryptographic hashes. More...

Header: #include <QCryptographicHash>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

Note: All functions in this class are reentrant.

Public Types

enum Algorithm { Md4, Md5, Sha1, Sha224, Sha256, …, Blake2s_256 }

Public Functions

QCryptographicHash(QCryptographicHash::Algorithm method)
(since 6.5) QCryptographicHash(QCryptographicHash &&other)
~QCryptographicHash()
void addData(QByteArrayView bytes)
bool addData(QIODevice *device)
(since 6.5) QCryptographicHash::Algorithm algorithm() const
void reset()
QByteArray result() const
(since 6.3) QByteArrayView resultView() const
(since 6.5) void swap(QCryptographicHash &other)
(since 6.5) QCryptographicHash &operator=(QCryptographicHash &&other)

Static Public Members

QByteArray hash(QByteArrayView data, QCryptographicHash::Algorithm method)
(since 6.8) QByteArrayView hashInto(QSpan<char> buffer, QByteArrayView data, QCryptographicHash::Algorithm method)
(since 6.8) QByteArrayView hashInto(QSpan<char> buffer, QSpan<const QByteArrayView> data, QCryptographicHash::Algorithm method)
(since 6.8) QByteArrayView hashInto(QSpan<std::byte> buffer, QByteArrayView data, QCryptographicHash::Algorithm method)
(since 6.8) QByteArrayView hashInto(QSpan<std::byte> buffer, QSpan<const QByteArrayView> data, QCryptographicHash::Algorithm method)
(since 6.8) QByteArrayView hashInto(QSpan<uchar> buffer, QByteArrayView data, QCryptographicHash::Algorithm method)
(since 6.8) QByteArrayView hashInto(QSpan<uchar> buffer, QSpan<const QByteArrayView> data, QCryptographicHash::Algorithm method)
int hashLength(QCryptographicHash::Algorithm method)
(since 6.5) bool supportsAlgorithm(QCryptographicHash::Algorithm method)

Detailed Description

QCryptographicHash can be used to generate cryptographic hashes of binary or text data.

Refer to the documentation of the QCryptographicHash::Algorithm enum for a list of the supported algorithms.

Member Type Documentation

enum QCryptographicHash::Algorithm

Note: In Qt versions before 5.9, when asked to generate a SHA3 hash sum, QCryptographicHash actually calculated Keccak. If you need compatibility with SHA-3 hashes produced by those versions of Qt, use the Keccak_ enumerators. Alternatively, if source compatibility is required, define the macro QT_SHA3_KECCAK_COMPAT.

ConstantValueDescription
QCryptographicHash::Md40Generate an MD4 hash sum
QCryptographicHash::Md51Generate an MD5 hash sum
QCryptographicHash::Sha12Generate an SHA-1 hash sum
QCryptographicHash::Sha2243Generate an SHA-224 hash sum (SHA-2). Introduced in Qt 5.0
QCryptographicHash::Sha2564Generate an SHA-256 hash sum (SHA-2). Introduced in Qt 5.0
QCryptographicHash::Sha3845Generate an SHA-384 hash sum (SHA-2). Introduced in Qt 5.0
QCryptographicHash::Sha5126Generate an SHA-512 hash sum (SHA-2). Introduced in Qt 5.0
QCryptographicHash::Sha3_224RealSha3_224Generate an SHA3-224 hash sum. Introduced in Qt 5.1
QCryptographicHash::Sha3_256RealSha3_256Generate an SHA3-256 hash sum. Introduced in Qt 5.1
QCryptographicHash::Sha3_384RealSha3_384Generate an SHA3-384 hash sum. Introduced in Qt 5.1
QCryptographicHash::Sha3_512RealSha3_512Generate an SHA3-512 hash sum. Introduced in Qt 5.1
QCryptographicHash::Keccak_2247Generate a Keccak-224 hash sum. Introduced in Qt 5.9.2
QCryptographicHash::Keccak_2568Generate a Keccak-256 hash sum. Introduced in Qt 5.9.2
QCryptographicHash::Keccak_3849Generate a Keccak-384 hash sum. Introduced in Qt 5.9.2
QCryptographicHash::Keccak_51210Generate a Keccak-512 hash sum. Introduced in Qt 5.9.2
QCryptographicHash::Blake2b_16015Generate a BLAKE2b-160 hash sum. Introduced in Qt 6.0
QCryptographicHash::Blake2b_25616Generate a BLAKE2b-256 hash sum. Introduced in Qt 6.0
QCryptographicHash::Blake2b_38417Generate a BLAKE2b-384 hash sum. Introduced in Qt 6.0
QCryptographicHash::Blake2b_51218Generate a BLAKE2b-512 hash sum. Introduced in Qt 6.0
QCryptographicHash::Blake2s_12819Generate a BLAKE2s-128 hash sum. Introduced in Qt 6.0
QCryptographicHash::Blake2s_16020Generate a BLAKE2s-160 hash sum. Introduced in Qt 6.0
QCryptographicHash::Blake2s_22421Generate a BLAKE2s-224 hash sum. Introduced in Qt 6.0
QCryptographicHash::Blake2s_25622Generate a BLAKE2s-256 hash sum. Introduced in Qt 6.0

Member Function Documentation

[static noexcept, since 6.8] QByteArrayView QCryptographicHash::hashInto(QSpan<char> buffer, QByteArrayView data, QCryptographicHash::Algorithm method)

[static noexcept, since 6.8] QByteArrayView QCryptographicHash::hashInto(QSpan<char> buffer, QSpan<const QByteArrayView> data, QCryptographicHash::Algorithm method)

[static noexcept, since 6.8] QByteArrayView QCryptographicHash::hashInto(QSpan<std::byte> buffer, QByteArrayView data, QCryptographicHash::Algorithm method)

[static noexcept, since 6.8] QByteArrayView QCryptographicHash::hashInto(QSpan<std::byte> buffer, QSpan<const QByteArrayView> data, QCryptographicHash::Algorithm method)

[static noexcept, since 6.8] QByteArrayView QCryptographicHash::hashInto(QSpan<uchar> buffer, QByteArrayView data, QCryptographicHash::Algorithm method)

[static noexcept, since 6.8] QByteArrayView QCryptographicHash::hashInto(QSpan<uchar> buffer, QSpan<const QByteArrayView> data, QCryptographicHash::Algorithm method)

Returns the hash of data using method, using buffer to store the result.

If data is a span, adds all the byte array views to the hash, in the order given.

The return value will be a sub-span of buffer, unless buffer is of insufficient size, in which case a null QByteArrayView is returned.

This function was introduced in Qt 6.8.

See also hash().

[explicit] QCryptographicHash::QCryptographicHash(QCryptographicHash::Algorithm method)

Constructs an object that can be used to create a cryptographic hash from data using method.

[noexcept, since 6.5] QCryptographicHash::QCryptographicHash(QCryptographicHash &&other)

Move-constructs a new QCryptographicHash from other.

Note: The moved-from object other is placed in a partially-formed state, in which the only valid operations are destruction and assignment of a new value.

This function was introduced in Qt 6.5.

[noexcept] QCryptographicHash::~QCryptographicHash()

Destroys the object.

[noexcept] void QCryptographicHash::addData(QByteArrayView bytes)

Adds the characters in bytes to the cryptographic hash.

Note: In Qt versions prior to 6.3, this function took QByteArray, not QByteArrayView.

bool QCryptographicHash::addData(QIODevice *device)

Reads the data from the open QIODevice device until it ends and hashes it. Returns true if reading was successful.

[noexcept, since 6.5] QCryptographicHash::Algorithm QCryptographicHash::algorithm() const

Returns the algorithm used to generate the cryptographic hash.

This function was introduced in Qt 6.5.

[static] QByteArray QCryptographicHash::hash(QByteArrayView data, QCryptographicHash::Algorithm method)

Returns the hash of data using method.

Note: In Qt versions prior to 6.3, this function took QByteArray, not QByteArrayView.

See also hashInto().

[static] int QCryptographicHash::hashLength(QCryptographicHash::Algorithm method)

Returns the size of the output of the selected hash method in bytes.

[noexcept] void QCryptographicHash::reset()

Resets the object.

QByteArray QCryptographicHash::result() const

Returns the final hash value.

See also resultView() and QByteArray::toHex().

[noexcept, since 6.3] QByteArrayView QCryptographicHash::resultView() const

Returns the final hash value.

Note that the returned view remains valid only as long as the QCryptographicHash object is not modified by other means.

This function was introduced in Qt 6.3.

See also result().

[static, since 6.5] bool QCryptographicHash::supportsAlgorithm(QCryptographicHash::Algorithm method)

Returns whether the selected algorithm method is supported and if result() will return a value when the method is used.

Note: OpenSSL will be responsible for providing this information when used as a provider, otherwise true will be returned as the non-OpenSSL implementation doesn't have any restrictions. We return false if we fail to query OpenSSL.

This function was introduced in Qt 6.5.

[noexcept, since 6.5] void QCryptographicHash::swap(QCryptographicHash &other)

Swaps cryptographic hash other with this cryptographic hash. This operation is very fast and never fails.

This function was introduced in Qt 6.5.

[noexcept, since 6.5] QCryptographicHash &QCryptographicHash::operator=(QCryptographicHash &&other)

Move-assigns other to this QCryptographicHash instance.

Note: The moved-from object other is placed in a partially-formed state, in which the only valid operations are destruction and assignment of a new value.

This function was introduced in Qt 6.5.

© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.