Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QCryptographicHash Class Reference

\inmodule QtCore More...

#include <qcryptographichash.h>

+ Collaboration diagram for QCryptographicHash:

Public Types

enum  Algorithm {
  Md4 , Md5 , Sha1 = 2 , Sha224 ,
  Sha256 , Sha384 , Sha512 , Keccak_224 = 7 ,
  Keccak_256 , Keccak_384 , Keccak_512 , RealSha3_224 = 11 ,
  RealSha3_256 , RealSha3_384 , RealSha3_512 , Sha3_224 = RealSha3_224 ,
  Sha3_256 = RealSha3_256 , Sha3_384 = RealSha3_384 , Sha3_512 = RealSha3_512 , Blake2b_160 = 15 ,
  Blake2b_256 , Blake2b_384 , Blake2b_512 , Blake2s_128 ,
  Blake2s_160 , Blake2s_224 , Blake2s_256 , NumAlgorithms
}
 

Public Member Functions

 QCryptographicHash (Algorithm method)
 Constructs an object that can be used to create a cryptographic hash from data using method.
 
 QCryptographicHash (QCryptographicHash &&other) noexcept
 Move-constructs a new QCryptographicHash from other.
 
 ~QCryptographicHash ()
 Destroys the object.
 
void swap (QCryptographicHash &other) noexcept
 Swaps cryptographic hash other with this cryptographic hash.
 
void reset () noexcept
 Resets the object.
 
Algorithm algorithm () const noexcept
 Returns the algorithm used to generate the cryptographic hash.
 
void addData (QByteArrayView data) noexcept
 Adds the characters in bytes to the cryptographic hash.
 
bool addData (QIODevice *device)
 Reads the data from the open QIODevice device until it ends and hashes it.
 
QByteArray result () const
 Returns the final hash value.
 
QByteArrayView resultView () const noexcept
 

Static Public Member Functions

static QByteArray hash (QByteArrayView data, Algorithm method)
 Returns the hash of data using method.
 
static int hashLength (Algorithm method)
 Returns the size of the output of the selected hash method in bytes.
 
static bool supportsAlgorithm (Algorithm method)
 Returns whether the selected algorithm method is supported and if result() will return a value when the method is used.
 

Detailed Description

\inmodule QtCore

The QCryptographicHash class provides a way to generate cryptographic hashes.

Since
4.3

\reentrant

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

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

Definition at line 18 of file qcryptographichash.h.

Member Enumeration Documentation

◆ 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.

\value Md4 Generate an MD4 hash sum \value Md5 Generate an MD5 hash sum \value Sha1 Generate an SHA-1 hash sum \value Sha224 Generate an SHA-224 hash sum (SHA-2). Introduced in Qt 5.0 \value Sha256 Generate an SHA-256 hash sum (SHA-2). Introduced in Qt 5.0 \value Sha384 Generate an SHA-384 hash sum (SHA-2). Introduced in Qt 5.0 \value Sha512 Generate an SHA-512 hash sum (SHA-2). Introduced in Qt 5.0 \value Sha3_224 Generate an SHA3-224 hash sum. Introduced in Qt 5.1 \value Sha3_256 Generate an SHA3-256 hash sum. Introduced in Qt 5.1 \value Sha3_384 Generate an SHA3-384 hash sum. Introduced in Qt 5.1 \value Sha3_512 Generate an SHA3-512 hash sum. Introduced in Qt 5.1 \value Keccak_224 Generate a Keccak-224 hash sum. Introduced in Qt 5.9.2 \value Keccak_256 Generate a Keccak-256 hash sum. Introduced in Qt 5.9.2 \value Keccak_384 Generate a Keccak-384 hash sum. Introduced in Qt 5.9.2 \value Keccak_512 Generate a Keccak-512 hash sum. Introduced in Qt 5.9.2 \value Blake2b_160 Generate a BLAKE2b-160 hash sum. Introduced in Qt 6.0 \value Blake2b_256 Generate a BLAKE2b-256 hash sum. Introduced in Qt 6.0 \value Blake2b_384 Generate a BLAKE2b-384 hash sum. Introduced in Qt 6.0 \value Blake2b_512 Generate a BLAKE2b-512 hash sum. Introduced in Qt 6.0 \value Blake2s_128 Generate a BLAKE2s-128 hash sum. Introduced in Qt 6.0 \value Blake2s_160 Generate a BLAKE2s-160 hash sum. Introduced in Qt 6.0 \value Blake2s_224 Generate a BLAKE2s-224 hash sum. Introduced in Qt 6.0 \value Blake2s_256 Generate a BLAKE2s-256 hash sum. Introduced in Qt 6.0 \omitvalue RealSha3_224 \omitvalue RealSha3_256 \omitvalue RealSha3_384 \omitvalue RealSha3_512 \omitvalue NumAlgorithms

Enumerator
Md4 
Md5 
Sha1 
Sha224 
Sha256 
Sha384 
Sha512 
Keccak_224 
Keccak_256 
Keccak_384 
Keccak_512 
RealSha3_224 
RealSha3_256 
RealSha3_384 
RealSha3_512 
Sha3_224 
Sha3_256 
Sha3_384 
Sha3_512 
Blake2b_160 
Blake2b_256 
Blake2b_384 
Blake2b_512 
Blake2s_128 
Blake2s_160 
Blake2s_224 
Blake2s_256 
NumAlgorithms 

Definition at line 22 of file qcryptographichash.h.

Constructor & Destructor Documentation

◆ QCryptographicHash() [1/2]

QCryptographicHash::QCryptographicHash ( Algorithm method)
explicit

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

Definition at line 490 of file qcryptographichash.cpp.

◆ QCryptographicHash() [2/2]

QCryptographicHash::QCryptographicHash ( QCryptographicHash && other)
inlinenoexcept

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.
Since
6.5

Definition at line 68 of file qcryptographichash.h.

◆ ~QCryptographicHash()

QCryptographicHash::~QCryptographicHash ( )

Destroys the object.

Definition at line 510 of file qcryptographichash.cpp.

Member Function Documentation

◆ addData() [1/2]

void QCryptographicHash::addData ( QByteArrayView bytes)
noexcept

Adds the characters in bytes to the cryptographic hash.

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

Definition at line 822 of file qcryptographichash.cpp.

References d.

Referenced by QQmlEngine::offlineStorageDatabaseFilePath().

+ Here is the caller graph for this function:

◆ addData() [2/2]

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.

Since
5.0

Definition at line 941 of file qcryptographichash.cpp.

References d, and device.

◆ algorithm()

QCryptographicHash::Algorithm QCryptographicHash::algorithm ( ) const
noexcept

Returns the algorithm used to generate the cryptographic hash.

Since
6.5

Definition at line 549 of file qcryptographichash.cpp.

References QCryptographicHashPrivate::method.

◆ hash()

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

Returns the hash of data using method.

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

Definition at line 1157 of file qcryptographichash.cpp.

References hash, and QCryptographicHashPrivate::method.

Referenced by QAuthenticatorPrivate::QAuthenticatorPrivate(), _q_PKCS12_keygen(), QSslCertificate::digest(), QSSGRenderEffect::finalizeShaders(), makeCacheKey(), QtObject::md5(), QNetworkDiskCachePrivate::uniqueFileName(), QQuick3DCustomMaterial::updateSpatialNode(), and QPdfEnginePrivate::writeTail().

+ Here is the caller graph for this function:

◆ hashLength()

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

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

Since
5.12

Definition at line 1170 of file qcryptographichash.cpp.

References hashLengthInternal(), and QCryptographicHashPrivate::method.

Referenced by QPasswordDigestor::deriveKeyPbkdf1(), and QPasswordDigestor::deriveKeyPbkdf2().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ reset()

void QCryptographicHash::reset ( )
noexcept

Resets the object.

Definition at line 539 of file qcryptographichash.cpp.

References QCryptographicHashPrivate::reset().

+ Here is the call graph for this function:

◆ result()

QByteArray QCryptographicHash::result ( ) const

Returns the final hash value.

See also
resultView(), QByteArray::toHex()

Definition at line 969 of file qcryptographichash.cpp.

References QCryptographicHashPrivate::resultView(), and QByteArrayView::toByteArray().

Referenced by QQmlEngine::offlineStorageDatabaseFilePath().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ resultView()

QByteArrayView QCryptographicHash::resultView ( ) const
noexcept
Since
6.3

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.

See also
result()

Definition at line 984 of file qcryptographichash.cpp.

References d.

◆ supportsAlgorithm()

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

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.
Since
6.5

Definition at line 1188 of file qcryptographichash.cpp.

References QCryptographicHashPrivate::method, and QCryptographicHashPrivate::supportsAlgorithm().

+ Here is the call graph for this function:

◆ swap()

void QCryptographicHash::swap ( QCryptographicHash & other)
inlinenoexcept

Swaps cryptographic hash other with this cryptographic hash.

This operation is very fast and never fails.

Since
6.5

Definition at line 72 of file qcryptographichash.h.

References d, other(), and qt_ptr_swap().

+ Here is the call graph for this function:

The documentation for this class was generated from the following files: