6.0.0-rc4Source

QCryptographicHash Class

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

Header#include <QCryptographicHash>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Types

enumAlgorithm

Public Functions

QCryptographicHash(Algorithm method)
QCryptographicHash(QCryptographicHash &&other)
~QCryptographicHash()
QCryptographicHash &operator=(QCryptographicHash &&other)
voidreset()
Algorithmalgorithm() const
voidaddData(QByteArrayView data)
voidaddData(const char *data, qsizetype length)
booladdData(QIODevice *device)
voidaddDataWithoutCopy(QByteArrayView data)
voidswap(QCryptographicHash &other)
QByteArrayresult() const
QByteArrayViewresultView() const

Static Public Members

QByteArrayhash(QByteArrayView data, Algorithm method)
QByteArrayViewhashInto(QSpan<char> buffer, QByteArrayView data, Algorithm method)
QByteArrayViewhashInto(QSpan<unsigned char> buffer, QByteArrayView data, Algorithm method)
QByteArrayViewhashInto(QSpan<std::byte> buffer, QByteArrayView data, Algorithm method)
inthashLength(Algorithm method)
boolsupportsAlgorithm(Algorithm method)

Detailed Description

QCryptographicHash computes a message digest of the data added to it with addData(). Call result() to read the digest. More data may be added afterwards, and result() then returns the digest of all the data so far. For a single buffer, use the static hash() function.

QCryptographicHash h(QCryptographicHash::Sha256);
h.addData(QByteArrayView("hello "));
h.addData(QByteArrayView("world"));
QByteArray digest = h.result().toHex();

See also QMessageAuthenticationCode.

Differences from Qt

Algorithms are implemented in the library itself. Keccak_* and Sha3_* are the original Keccak and the standardized SHA-3 padding respectively. The BLAKE2 algorithms are unkeyed; use QMessageAuthenticationCode for a keyed hash. There is no hashInto().

Member Type Documentation

enum QCryptographicHash::Algorithm

Selects the hash algorithm.

ConstantDescription
QCryptographicHash::Md4Message-Digest Algorithm 4. 16-byte digest. Not secure.
QCryptographicHash::Md5Message-Digest Algorithm 5. 16-byte digest. Not secure.
QCryptographicHash::Sha1Secure Hash Algorithm 1. 20-byte digest. Not secure.
QCryptographicHash::Sha224SHA-2 with a 28-byte digest.
QCryptographicHash::Sha256SHA-2 with a 32-byte digest.
QCryptographicHash::Sha384SHA-2 with a 48-byte digest.
QCryptographicHash::Sha512SHA-2 with a 64-byte digest.
QCryptographicHash::Keccak_224Keccak with a 28-byte digest, using the original Keccak padding.
QCryptographicHash::Keccak_256Keccak with a 32-byte digest, using the original Keccak padding.
QCryptographicHash::Keccak_384Keccak with a 48-byte digest, using the original Keccak padding.
QCryptographicHash::Keccak_512Keccak with a 64-byte digest, using the original Keccak padding.
QCryptographicHash::Sha3_224SHA-3 with a 28-byte digest.
QCryptographicHash::Sha3_256SHA-3 with a 32-byte digest.
QCryptographicHash::Sha3_384SHA-3 with a 48-byte digest.
QCryptographicHash::Sha3_512SHA-3 with a 64-byte digest.
QCryptographicHash::Blake2b_160BLAKE2b (RFC 7693) with a 20-byte digest.
QCryptographicHash::Blake2b_256BLAKE2b with a 32-byte digest.
QCryptographicHash::Blake2b_384BLAKE2b with a 48-byte digest.
QCryptographicHash::Blake2b_512BLAKE2b with a 64-byte digest.
QCryptographicHash::Blake2s_128BLAKE2s (RFC 7693) with a 16-byte digest.
QCryptographicHash::Blake2s_160BLAKE2s with a 20-byte digest.
QCryptographicHash::Blake2s_224BLAKE2s with a 28-byte digest.
QCryptographicHash::Blake2s_256BLAKE2s with a 32-byte digest.
QCryptographicHash::NumAlgorithmsThe number of algorithms; not an algorithm.

See also hashLength().

Member Function Documentation

[explicit] QCryptographicHash::QCryptographicHash(Algorithm method)

Constructs an object that can compute hashes with the algorithm method.

See also hash().

QCryptographicHash::QCryptographicHash(QCryptographicHash &&other)

Move-constructs an object from other. other must not be used afterwards except to be assigned to or destroyed.

QCryptographicHash::~QCryptographicHash()

Destroys the object.

void QCryptographicHash::addData(QByteArrayView data)

Adds data to the data being hashed.

See also result().

void QCryptographicHash::addData(const char *data, qsizetype length)

This is an overloaded function.

Adds the length bytes at data to the data being hashed.

bool QCryptographicHash::addData(QIODevice *device)

This is an overloaded function.

Reads from device until it is exhausted and adds the data. Returns true if all the data was read; returns false if device is null, not open or not readable, or if reading stopped before the end.

void QCryptographicHash::addDataWithoutCopy(QByteArrayView data)

Adds data to the hash. Nt processes the data immediately, so this is the same as addData().

This function was introduced in 6.8.

Algorithm QCryptographicHash::algorithm() const

Returns the algorithm that the object was constructed with.

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

Returns the digest of data computed with method.

See also addData() and result().

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

Computes the digest of data with method and writes it to buffer. Returns a view of the digest inside buffer, or a null view if buffer is smaller than hashLength() of method or the digest could not be computed.

This function was introduced in 6.8.

See also hash() and hashLength().

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

This is an overloaded function.

Writes the digest into buffer, a span of unsigned char; the result is computed over data with method.

This function was introduced in 6.8.

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

This is an overloaded function.

Writes the digest into buffer, a span of std::byte; the result is computed over data with method.

This function was introduced in 6.8.

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

Returns the size in bytes of the digest that method produces, or 0 if method is not supported.

See also supportsAlgorithm().

QCryptographicHash & QCryptographicHash::operator=(QCryptographicHash &&other)

Move-assigns other to this object and returns a reference to this object. other must not be used afterwards except to be assigned to or destroyed.

void QCryptographicHash::reset()

Resets the object, discarding all data added so far.

See also addData().

QByteArray QCryptographicHash::result() const

Returns the digest of the data added so far. The object stays usable, and more data may be added afterwards.

See also resultView() and reset().

QByteArrayView QCryptographicHash::resultView() const

Returns the digest of the data added so far as a view. The view is valid until the next call to resultView() or until the object is modified or destroyed.

See also result().

[static] bool QCryptographicHash::supportsAlgorithm(Algorithm method)

Returns true if method is supported; otherwise returns false.

See also hashLength().

void QCryptographicHash::swap(QCryptographicHash &other)

Swaps this hash with other.

This function was introduced in 5.10.