QMessageAuthenticationCode 類

QMessageAuthenticationCode 類提供生成基於哈希的消息身份驗證代碼的辦法。 更多...

頭: #include <QMessageAuthenticationCode>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

注意: 此類的所有函數 可重入 .

公共函數

QMessageAuthenticationCode (QCryptographicHash::Algorithm method , QByteArrayView key = {})
(從 6.6 起) QMessageAuthenticationCode (QMessageAuthenticationCode && other )
~QMessageAuthenticationCode ()
void addData (QByteArrayView data )
bool addData (QIODevice * device )
void addData (const char * data , qsizetype length )
void reset ()
QByteArray result () const
(從 6.6 起) QByteArrayView resultView () const
void setKey (QByteArrayView key )
(從 6.6 起) void swap (QMessageAuthenticationCode & other )
(從 6.6 起) QMessageAuthenticationCode & operator= (QMessageAuthenticationCode && other )

靜態公共成員

QByteArray hash (QByteArrayView message , QByteArrayView key , QCryptographicHash::Algorithm method )
(從 6.8 起) QByteArrayView hashInto (QSpan<char> buffer , QByteArrayView message , QByteArrayView key , QCryptographicHash::Algorithm method )
(從 6.8 起) QByteArrayView hashInto (QSpan<char> buffer , QSpan<const QByteArrayView> messageParts , QByteArrayView key , QCryptographicHash::Algorithm method )
(從 6.8 起) QByteArrayView hashInto (QSpan<std::byte> buffer , QByteArrayView message , QByteArrayView key , QCryptographicHash::Algorithm method )
(從 6.8 起) QByteArrayView hashInto (QSpan<std::byte> buffer , QSpan<const QByteArrayView> messageParts , QByteArrayView key , QCryptographicHash::Algorithm method )
(從 6.8 起) QByteArrayView hashInto (QSpan<uchar> buffer , QByteArrayView message , QByteArrayView key , QCryptographicHash::Algorithm method )
(從 6.8 起) QByteArrayView hashInto (QSpan<uchar> buffer , QSpan<const QByteArrayView> messageParts , QByteArrayView key , QCryptographicHash::Algorithm method )

詳細描述

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 () 函數:

    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 () 函數:

    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.

另請參閱 QCryptographicHash and QCryptographicHash::Algorithm .

成員函數文檔編製

[static noexcept, since 6.8] QByteArrayView QMessageAuthenticationCode:: hashInto ( QSpan < char > buffer , QByteArrayView message , QByteArrayView key , QCryptographicHash::Algorithm method )

[static noexcept, since 6.8] QByteArrayView QMessageAuthenticationCode:: hashInto ( QSpan < char > buffer , QSpan <const QByteArrayView > messageParts , QByteArrayView key , QCryptographicHash::Algorithm method )

[static noexcept, since 6.8] QByteArrayView QMessageAuthenticationCode:: hashInto ( QSpan < std::byte > buffer , QByteArrayView message , QByteArrayView key , QCryptographicHash::Algorithm method )

[static noexcept, since 6.8] QByteArrayView QMessageAuthenticationCode:: hashInto ( QSpan < std::byte > buffer , QSpan <const QByteArrayView > messageParts , QByteArrayView key , QCryptographicHash::Algorithm method )

[static noexcept, since 6.8] QByteArrayView QMessageAuthenticationCode:: hashInto ( QSpan < uchar > buffer , QByteArrayView message , QByteArrayView key , QCryptographicHash::Algorithm method )

[static noexcept, since 6.8] QByteArrayView QMessageAuthenticationCode:: hashInto ( QSpan < uchar > buffer , QSpan <const QByteArrayView > messageParts , QByteArrayView key , QCryptographicHash::Algorithm method )

Returns the authentication code for the message ( message or, for the QSpan overloads, the concatenation of messageParts ) using the key key and the method method .

The return value will be a sub-span of buffer , unless buffer is of insufficient size, in which case a null QByteArrayView 被返迴。

這些函數在 Qt 6.8 引入。

另請參閱 hash ().

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

[noexcept, 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.

該函數在 Qt 6.6 引入。

[noexcept] QMessageAuthenticationCode:: ~QMessageAuthenticationCode ()

銷毀對象。

[noexcept] void QMessageAuthenticationCode:: addData ( QByteArrayView data )

添加 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.

另請參閱 resultView () 和 result ().

bool QMessageAuthenticationCode:: addData ( QIODevice * device )

讀取數據,從打開 QIODevice device until it ends and adds it to message. Returns true 若讀取是成功的。

注意: device must be already opened.

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

Adds the first length chars of data to the message.

這是重載函數。

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

另請參閱 hashInto ().

[noexcept] void QMessageAuthenticationCode:: reset ()

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

QByteArray QMessageAuthenticationCode:: result () const

Returns the final authentication code.

另請參閱 resultView () 和 QByteArray::toHex ().

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

返迴最終的哈希值。

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

該函數在 Qt 6.6 引入。

另請參閱 result ().

[noexcept] 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

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

注意: 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.

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

Swaps this message authentication code with other 。此操作很快且從不失敗。

該函數在 Qt 6.6 引入。

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

移動賦值 other 到此 QMessageAuthenticationCode 實例。

注意: 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.

該函數在 Qt 6.6 引入。