QHashSeed Struct | Qt Core | Qt 6.11.0

- Qt 6.11
- Qt Core
- C++ Classes
- QHashSeed
- Qt 6.11.0 Reference Documentation

# QHashSeed Struct

Header: | `#include <QHashSeed>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Core)` `target_link_libraries(mytarget PRIVATE Qt6::Core)`
qmake: | `QT += core`
Since: | Qt 6.2

- List of all members, including inherited members

## Public Functions

| QHashSeed(size_t data = 0)
| operator size_t() const

## Static Public Members

QHashSeed | globalSeed()
void | resetRandomGlobalSeed()
void | setDeterministicGlobalSeed()

## Detailed Description

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 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 and QRandomGenerator.

## Member Function Documentation

### `[constexpr]` QHashSeed::QHashSeed(size_t data = 0)

Constructs a new QHashSeed object using data as the seed.

### `[static noexcept]` QHashSeed QHashSeed::globalSeed()

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.

Note: This function is thread-safe.

### `[static]` void QHashSeed::resetRandomGlobalSeed()

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.

Note: This function is thread-safe.

### `[static]` void QHashSeed::setDeterministicGlobalSeed()

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().

Note: This function is thread-safe.

### `[constexpr noexcept]` QHashSeed::operator size_t() const

Converts the returned hash seed into a `size_t`.