PySide6.QtCore.QMessageAuthenticationCode¶
- class QMessageAuthenticationCode¶
- The - QMessageAuthenticationCodeclass provides a way to generate hash-based message authentication codes. More…- Synopsis¶- Methods¶- def - __init__()
- def - addData()
- def - reset()
- def - result()
- def - resultView()
- def - setKey()
- def - swap()
 - Static functions¶- def - hash()
 - 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. - Use the - QMessageAuthenticationCodeclass to generate hash-based message authentication codes (HMACs). The class supports all cryptographic hash algorithms from- QCryptographicHash(see also- 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:- 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" - 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 - Constructs an object that can be used to create a cryptographic hash from data using method - methodand 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.- addData(data)¶
- Parameters:
- data – - QByteArrayView
 
 - Adds - datato 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.- See also - addData(device)
- Parameters:
- device – - QIODevice
- Return type:
- bool 
 
 - Reads the data from the open - QIODevice- deviceuntil it ends and adds it to message. Returns- trueif reading was successful.- Note - devicemust be already opened.- addData(data, length)
- Parameters:
- data – str 
- length – int 
 
 
 - This is an overloaded function. - Adds the first - lengthchars of- datato the message.- static hash(message, key, method)¶
- Parameters:
- message – - QByteArrayView
- key – - QByteArrayView
- method – - Algorithm
 
- Return type:
 
 - Returns the authentication code for the message - messageusing the key- keyand 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.- See also - hashInto()- reset()¶
 - Resets message data. Calling this function doesn’t affect the key. - result()¶
- Return type:
 
 - Returns the final authentication code. - See also - resultView()¶
- Return type:
- QByteArrayView
 
 - Returns the final hash value. - Note that the returned view remains valid only as long as the - QMessageAuthenticationCodeobject is not modified by other means.- See also - setKey(key)¶
- Parameters:
- key – - QByteArrayView
 
 - 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 - 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 - QMessageAuthenticationCodeuntil 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.- swap(other)¶
- Parameters:
- other – - QMessageAuthenticationCode
 
 - Swaps this message authentication code with - other. This operation is very fast and never fails.