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 , const QByteArray & key = QByteArray())
~QMessageAuthenticationCode ()
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 )

靜態公共成員

QByteArray hash (const QByteArray & message , const QByteArray & key , QCryptographicHash::Algorithm method )

詳細描述

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 () 和 addData () functions. Result can be acquired by 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"
					

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

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

另請參閱 QCryptographicHash .

成員函數文檔編製

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

QMessageAuthenticationCode:: ~QMessageAuthenticationCode ()

銷毀對象。

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

Adds the first length chars of data to the message.

void QMessageAuthenticationCode:: addData (const QByteArray & data )

此函數重載 addData()。

bool QMessageAuthenticationCode:: addData ( QIODevice * device )

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

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

另請參閱 QByteArray::toHex ().

void QMessageAuthenticationCode:: setKey (const QByteArray & key )

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