QMessageAuthenticationCode Class

The QMessageAuthenticationCode class provides a way to generate hash-based message authentication codes. More...

Header: #include <QMessageAuthenticationCode>
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 Functions

QMessageAuthenticationCode(QCryptographicHash::Algorithm method, const QByteArray &key = QByteArray())
~QMessageAuthenticationCode()
void addData(const char *data, qsizetype length)
void addData(const QByteArray &data)
bool addData(QIODevice *device)
void reset()
QByteArray result() const
void setKey(const QByteArray &key)

Static Public Members

QByteArray hash(const QByteArray &message, const QByteArray &key, QCryptographicHash::Algorithm method)

Detailed Description

Use the QMessageAuthenticationCode class to generate hash-based message authentication codes (HMACs). The class supports all cryptographic hash algorithms from QCryptographicHash (see also QCryptographicHash::Algorithms).

To generate a message authentication code, pass a suitable hash algorithm and secret key to the constructor. Then process the message data by calling addData() one or more times. After the full message has been processed, get the final authentication code via the result() function:

    QByteArray key = "key";
    QByteArray message = "The quick brown fox jumps over the lazy dog";
    ...
    QMessageAuthenticationCode code(QCryptographicHash::Sha256, key);
    code.addData(message);
    code.result().toHex(); // returns "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8"

For simple cases like above, you can also use the static hash() function:

    QMessageAuthenticationCode::hash(message, key, QCryptographicHash::Sha256).toHex();

Note: The cryptographic strength of the HMAC depends upon the size of the secret key, and the security of the underlying hash function.

See also QCryptographicHash and QCryptographicHash::Algorithms.

Member Function Documentation

[explicit] QMessageAuthenticationCode::QMessageAuthenticationCode(QCryptographicHash::Algorithm method, const QByteArray &key = QByteArray())

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

QMessageAuthenticationCode::~QMessageAuthenticationCode()

Destroys the object.

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

Adds the first length chars of data to the message.

void QMessageAuthenticationCode::addData(const QByteArray &data)

This function overloads addData().

bool QMessageAuthenticationCode::addData(QIODevice *device)

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

Note: device must be already opened.

[static] QByteArray QMessageAuthenticationCode::hash(const QByteArray &message, const QByteArray &key, QCryptographicHash::Algorithm method)

Returns the authentication code for the message message using the key key and the method method.

void QMessageAuthenticationCode::reset()

Resets message data. Calling this method doesn't affect the key.

QByteArray QMessageAuthenticationCode::result() const

Returns the final authentication code.

See also QByteArray::toHex().

void QMessageAuthenticationCode::setKey(const QByteArray &key)

Sets secret key. Calling this method automatically resets the object state.

For optimal performance, call this method only to change the active key, not to set an initial key, as in

QMessageAuthenticationCode mac(method);
mac.setKey(key); // does extra work
use(mac);

Prefer to pass initial keys as the constructor argument:

QMessageAuthenticationCode mac(method, key); // OK, optimal
use(mac);

You can use std::optional to delay construction of a QMessageAuthenticationCode until you know the key:

std::optional<QMessageAuthenticationCode> mac;
~~~
key = ~~~;
mac.emplace(method, key);
use(*mac);

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