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
QHashSeed Class Reference

\inmodule QtCore More...

#include <qhashfunctions.h>

+ Collaboration diagram for QHashSeed:

Public Member Functions

constexpr QHashSeed (size_t d=0)
 Constructs a new QHashSeed object using data as the seed.
 
constexpr operator size_t () const noexcept
 Converts the returned hash seed into a size_t.
 

Static Public Member Functions

static Q_CORE_EXPORT QHashSeed globalSeed () noexcept
 \threadsafe
 
static Q_CORE_EXPORT void setDeterministicGlobalSeed ()
 \threadsafe
 
static Q_CORE_EXPORT void resetRandomGlobalSeed ()
 \threadsafe
 

Detailed Description

\inmodule QtCore

Since
6.2

The QHashSeed class is used to convey the QHash seed. This is used internally by QHash and provides three static member functions to allow users to obtain the hash and to reset it.

QHash and the qHash() functions implement what is called as "salted hash". The intent is that different applications and different instances of the same application will produce different hashing values for the same input, thus causing the ordering of elements in QHash to be unpredictable by external observers. This improves the applications' resilience against attacks that attempt to force hashing tables into degenerate mode.

Most applications will not need to deal directly with the hash seed, as QHash will do so when needed. However, applications may wish to use this for their own purposes in the same way as QHash does: as an application-global random value (but see \l QRandomGenerator too). Note that the global hash seed may change during the application's lifetime, if the resetRandomGlobalSeed() function is called. Users of the global hash need to store the value they are using and not rely on getting it again.

This class also implements functionality to set the hash seed to a deterministic value, which the qHash() functions will take to mean that they should use a fixed hashing function on their data too. This functionality is only meant to be used in debugging applications. This behavior can also be controlled by setting the QT_HASH_SEED environment variable to the value zero (any other value is ignored).

See also
QHash, QRandomGenerator

Definition at line 41 of file qhashfunctions.h.

Constructor & Destructor Documentation

◆ QHashSeed()

QHashSeed::QHashSeed ( size_t d = 0)
inlineconstexpr

Constructs a new QHashSeed object using data as the seed.

Definition at line 43 of file qhashfunctions.h.

Member Function Documentation

◆ globalSeed()

QHashSeed QHashSeed::globalSeed ( )
staticnoexcept

\threadsafe

Returns the current global QHash seed. The value returned by this function will be zero if setDeterministicGlobalSeed() has been called or if the {QT_HASH_SEED} environment variable is set to zero.

Definition at line 1216 of file qhash.cpp.

References qt_qhash_seed.

Referenced by QHashPrivate::Data< Node >::Data().

+ Here is the caller graph for this function:

◆ operator size_t()

QHashSeed::operator size_t ( ) const
inlineconstexprnoexcept

Converts the returned hash seed into a size_t.

Definition at line 44 of file qhashfunctions.h.

◆ resetRandomGlobalSeed()

void QHashSeed::resetRandomGlobalSeed ( )
static

\threadsafe

Reseeds the Qt hashing seed to a new, random value. Calling this function is not necessary, but long-running applications may want to do so after a long period of time in which information about its hash may have been exposed to potential attackers.

If the environment variable QT_HASH_SEED is set to zero, calling this function will result in a no-op.

Qt never calls this function during the execution of the application, but unless the QT_HASH_SEED variable is set to 0, the hash seed returned by globalSeed() will be a random value as if this function had been called.

Definition at line 1250 of file qhash.cpp.

References qt_qhash_seed.

◆ setDeterministicGlobalSeed()

void QHashSeed::setDeterministicGlobalSeed ( )
static

\threadsafe

Forces the Qt hash seed to a deterministic value (zero) and asks the qHash() functions to use a pre-determined hashing function. This mode is only useful for debugging and should not be used in production code.

Regular operation can be restored by calling resetRandomGlobalSeed().

Definition at line 1230 of file qhash.cpp.

References qt_qhash_seed.

Referenced by collectJson(), main(), and runUic().

+ Here is the caller graph for this function:

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