IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

QMessageAuthenticationCode Class

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

All functions in this class are reentrant.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

QMessageAuthenticationCode Class

  • Header: QMessageAuthenticationCode

  • CMake:

    find_package(Qt6 REQUIRED COMPONENTS Core)

    target_link_libraries(mytarget PRIVATE Qt6::Core)

  • qmake: QT += core

  • Group: QMessageAuthenticationCode is part of tools

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::Algorithm).

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:

 
Sélectionnez
    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:

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

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

See Also

Member Function Documentation

 

[explicit] QMessageAuthenticationCode::QMessageAuthenticationCode(QCryptographicHash::Algorithm method, QByteArrayView key = {})

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

In Qt versions prior to 6.6, this function took its arguments as QByteArray, not QByteArrayView. If you experience compile errors, it's because your code is passing objects that are implicitly convertible to QByteArray, but not QByteArrayView. Wrap the corresponding argument in QByteArray{~~~} to make the cast explicit. This is backwards-compatible with old Qt versions.

[since 6.6] QMessageAuthenticationCode::QMessageAuthenticationCode(QMessageAuthenticationCode &&other)

Move-constructs a new QMessageAuthenticationCode from other.

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

This function was introduced in Qt 6.6.

QMessageAuthenticationCode::~QMessageAuthenticationCode()

Destroys the object.

void QMessageAuthenticationCode::addData(QByteArrayView data)

Adds data to the message.

In Qt versions prior to 6.6, this function took its arguments as QByteArray, not QByteArrayView. If you experience compile errors, it's because your code is passing objects that are implicitly convertible to QByteArray, but not QByteArrayView. Wrap the corresponding argument in QByteArray{~~~} to make the cast explicit. This is backwards-compatible with old Qt versions.

See Also

See also resultView(), result()

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

This is an overloaded function.

Adds the first length chars of data to the message.

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.

device must be already opened.

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

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

In Qt versions prior to 6.6, this function took its arguments as QByteArray, not QByteArrayView. If you experience compile errors, it's because your code is passing objects that are implicitly convertible to QByteArray, but not QByteArrayView. Wrap the corresponding argument in QByteArray{~~~} to make the cast explicit. This is backwards-compatible with old Qt versions.

void QMessageAuthenticationCode::reset()

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

QByteArray QMessageAuthenticationCode::result() const

Returns the final authentication code.

See Also

[since 6.6] QByteArrayView QMessageAuthenticationCode::resultView() const

Returns the final hash value.

Note that the returned view remains valid only as long as the QMessageAuthenticationCode object is not modified by other means.

This function was introduced in Qt 6.6.

See Also

See also result()

void QMessageAuthenticationCode::setKey(QByteArrayView key)

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

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

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

Prefer to pass initial keys as the constructor argument:

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

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

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

In Qt versions prior to 6.6, this function took its arguments as QByteArray, not QByteArrayView. If you experience compile errors, it's because your code is passing objects that are implicitly convertible to QByteArray, but not QByteArrayView. Wrap the corresponding argument in QByteArray{~~~} to make the cast explicit. This is backwards-compatible with old Qt versions.

[since 6.6] void QMessageAuthenticationCode::swap(QMessageAuthenticationCode &other)

Swaps message authentication code other with this message authentication code. This operation is very fast and never fails.

This function was introduced in Qt 6.6.

[since 6.6] QMessageAuthenticationCode &QMessageAuthenticationCode::operator=(QMessageAuthenticationCode &&other)

Move-assigns other to this QMessageAuthenticationCode instance.

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

This function was introduced in Qt 6.6.

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+