QMessageAuthenticationCode#

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

Synopsis#

Functions#

Static functions#

  • def hash (message, key, method)

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

Detailed Description#

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

QMessageAuthenticationCode supports all cryptographic hashes which are supported by QCryptographicHash .

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

key = "key"
message = "The quick brown fox jumps over the lazy dog"            ...

code = QMessageAuthenticationCode(QCryptographicHash.Sha256, key)
code.addData(message)
code.result().toHex() # returns "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8"

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

QMessageAuthenticationCode.hash(message, key, QCryptographicHash.Sha256).toHex()
class PySide6.QtCore.QMessageAuthenticationCode(method[, key={}])#
Parameters:
  • methodAlgorithm

  • keyQByteArrayView

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

Note

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.

PySide6.QtCore.QMessageAuthenticationCode.addData(data)#
Parameters:

dataQByteArrayView

Adds data to the message.

Note

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.

PySide6.QtCore.QMessageAuthenticationCode.addData(device)
Parameters:

devicePySide6.QtCore.QIODevice

Return type:

bool

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.

PySide6.QtCore.QMessageAuthenticationCode.addData(data, length)
Parameters:
  • data – str

  • length – int

This is an overloaded function.

Adds the first length chars of data to the message.

static PySide6.QtCore.QMessageAuthenticationCode.hash(message, key, method)#
Parameters:
  • messageQByteArrayView

  • keyQByteArrayView

  • methodAlgorithm

Return type:

PySide6.QtCore.QByteArray

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

Note

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.

PySide6.QtCore.QMessageAuthenticationCode.reset()#

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

PySide6.QtCore.QMessageAuthenticationCode.result()#
Return type:

PySide6.QtCore.QByteArray

Returns the final authentication code.

PySide6.QtCore.QMessageAuthenticationCode.resultView()#
Return type:

QByteArrayView

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.

See also

result()

PySide6.QtCore.QMessageAuthenticationCode.setKey(key)#
Parameters:

keyQByteArrayView

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);

Note

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.

PySide6.QtCore.QMessageAuthenticationCode.swap(other)#
Parameters:

otherPySide6.QtCore.QMessageAuthenticationCode

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