Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

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
Since: Qt 5.1

Note: All functions in this class are reentrant.

Public Functions

QMessageAuthenticationCode(QCryptographicHash::Algorithm method, const QByteArray &key = QByteArray())
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

QMessageAuthenticationCode supports all cryptographic hashes which are supported by QCryptographicHash.

To generate message authentication code, pass hash algorithm QCryptographicHash::Algorithm to constructor, then set key and message by setKey() and addData() functions. Result can be acquired by result() function.

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

Alternatively, this effect can be achieved by providing message, key and method to hash() method.

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

See also QCryptographicHash.

Member Function Documentation

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.


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.

Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded