QMessageAuthenticationCode Class
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 = {}) | |
(since 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 |
(since 6.6) QByteArrayView | resultView() const |
| void | setKey(QByteArrayView key) |
(since 6.6) void | swap(QMessageAuthenticationCode &other) |
(since 6.6) QMessageAuthenticationCode & | operator=(QMessageAuthenticationCode &&other) |
静态公共成员
| QByteArray | hash(QByteArrayView message, QByteArrayView key, QCryptographicHash::Algorithm method) |
(since 6.8) QByteArrayView | hashInto(QSpan<char> buffer, QByteArrayView message, QByteArrayView key, QCryptographicHash::Algorithm method) |
(since 6.8) QByteArrayView | hashInto(QSpan<char> buffer, QSpan<const QByteArrayView> messageParts, QByteArrayView key, QCryptographicHash::Algorithm method) |
(since 6.8) QByteArrayView | hashInto(QSpan<std::byte> buffer, QByteArrayView message, QByteArrayView key, QCryptographicHash::Algorithm method) |
(since 6.8) QByteArrayView | hashInto(QSpan<std::byte> buffer, QSpan<const QByteArrayView> messageParts, QByteArrayView key, QCryptographicHash::Algorithm method) |
(since 6.8) QByteArrayView | hashInto(QSpan<uchar> buffer, QByteArrayView message, QByteArrayView key, QCryptographicHash::Algorithm method) |
(since 6.8) QByteArrayView | hashInto(QSpan<uchar> buffer, QSpan<const QByteArrayView> messageParts, QByteArrayView key, QCryptographicHash::Algorithm method) |
详细说明
使用 QMessageAuthenticationCode 类生成基于哈希的消息认证码 (HMAC)。该类支持QCryptographicHash 上的所有加密哈希算法(另请参阅QCryptographicHash::Algorithm )。
要生成消息认证码,请向构造函数传递合适的哈希算法和密钥。然后通过调用addData() 一次或多次来处理消息数据。在完整消息处理完毕后,可通过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"对于如上所述的简单情况,您也可以使用静态函数hash():
QMessageAuthenticationCode::hash(message, key, QCryptographicHash::Sha256).toHex();注意: HMAC 的加密强度 取决于密钥的大小以及底层哈希函数的安全性。
另请参阅 QCryptographicHash 和QCryptographicHash::Algorithm 。
成员函数文档
[explicit] QMessageAuthenticationCode::QMessageAuthenticationCode(QCryptographicHash::Algorithm method, QByteArrayView key = {})
构建一个对象,该对象可用于通过方法 `method ` 和密钥 `key` 从数据中生成加密哈希。
注意:在 Qt 6.6 之前的版本中,此函数的参数类型为 `QByteArray`,而非 `QByteArrayView`。若您遇到编译错误,是因为您的代码传递了可隐式转换为 `QByteArray` 但无法隐式转换为 `QByteArrayView` 的对象。请将相应的参数用 `QByteArray{~~~} ` 包装,以显式进行类型转换。此做法与旧版 Qt 向后兼容。
[noexcept, since 6.6] QMessageAuthenticationCode::QMessageAuthenticationCode(QMessageAuthenticationCode &&other)
从 `other` 移动构造一个新的 `QMessageAuthenticationCode`。
注意: 被移动的对象 other 将处于一种不完整的状态,在此状态下,唯一有效的操作是销毁该对象或将其赋值给新对象。
该函数于 Qt 6.6 中引入。
[noexcept] QMessageAuthenticationCode::~QMessageAuthenticationCode()
销毁该对象。
[noexcept] void QMessageAuthenticationCode::addData(QByteArrayView data)
将data 添加到消息中。
注意:在 Qt 6.6 之前的版本中,该函数的参数类型为QByteArray ,而非QByteArrayView 。若遇到编译错误,是因为您的代码传递的对象虽可隐式转换为QByteArray ,但无法隐式转换为QByteArrayView 。请将相应的参数用QByteArray{~~~} 包装,以显式进行类型转换。此方法与旧版 Qt 向后兼容。
另请参阅 resultView() 和result()。
bool QMessageAuthenticationCode::addData(QIODevice *device)
从已打开的QIODevice device 中读取数据,直至数据结束,并将数据添加到message中。若读取成功,则返回true 。
注意: device 必须已打开。
void QMessageAuthenticationCode::addData(const char *data, qsizetype length)
将data 中的前length 个字符添加到消息中。
这是一个重载函数。
[static] QByteArray QMessageAuthenticationCode::hash(QByteArrayView message, QByteArrayView key, QCryptographicHash::Algorithm method)
使用密钥key 和方法method ,返回消息message 的认证码。
注意:在 Qt 6.6 之前的版本中,此函数的参数格式为QByteArray ,而非QByteArrayView 。若遇到编译错误,是因为您的代码传递了可隐式转换为QByteArray 但无法隐式转换为QByteArrayView 的对象。请将相应的参数包装在QByteArray{~~~} 中,以显式进行类型转换。此做法与旧版 Qt 向后兼容。
另请参阅 hashInto()。
[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)
返回该消息的认证码(message ;对于QSpan 的重载方法,则是messageParts 的拼接结果),使用密钥key 和方法method 。
返回值将是一个buffer 的子跨度,除非buffer 的大小不足,在这种情况下将返回一个空的QByteArrayView 。
这些函数在 Qt 6.8 中引入。
另请参阅 hash()。
[noexcept] void QMessageAuthenticationCode::reset()
重置消息数据。调用此函数不会影响密钥。
QByteArray QMessageAuthenticationCode::result() const
返回最终的认证码。
另请参阅 resultView() 和QByteArray::toHex()。
[noexcept, since 6.6] QByteArrayView QMessageAuthenticationCode::resultView() const
返回最终的哈希值。
请注意,只有在QMessageAuthenticationCode 对象未通过其他方式被修改的情况下,返回的视图才保持有效。
此函数在 Qt 6.6 中引入。
另请参阅 result()。
[noexcept] void QMessageAuthenticationCode::setKey(QByteArrayView key)
设置密钥key 。调用此函数会自动重置对象状态。
为了获得最佳性能,请仅在需要更改当前键时调用此函数,而非用于设置初始键,例如
QMessageAuthenticationCode mac(method);
mac.setKey(key); // does extra work
use(mac);建议将初始键作为构造函数参数传递:
QMessageAuthenticationCode mac(method, key); // OK, optimal
use(mac);您可以使用 std::optional 来延迟QMessageAuthenticationCode 的构建,直到确定密钥为止:
std::optional<QMessageAuthenticationCode> mac;
~~~
key = ~~~;
mac.emplace(method, key);
use(*mac);注意:在 Qt 6.6 之前的版本中,该函数将参数视为QByteArray ,而非QByteArrayView 。若遇到编译错误,是因为您的代码传递了可隐式转换为QByteArray 但无法转换为QByteArrayView 的对象。请将相应的参数包装在QByteArray{~~~} 中,以显式进行类型转换。此做法与旧版 Qt 向后兼容。
[noexcept, since 6.6] void QMessageAuthenticationCode::swap(QMessageAuthenticationCode &other)
将此消息认证码与other 互换。此操作速度极快,且绝不会失败。
该函数自 Qt 6.6 起引入。
[noexcept, since 6.6] QMessageAuthenticationCode &QMessageAuthenticationCode::operator=(QMessageAuthenticationCode &&other)
将other 通过移动赋值操作赋值给此QMessageAuthenticationCode 实例。
注意: 被移动的对象 other 将处于部分初始化状态,在此状态下,唯一有效的操作是销毁该对象或将其赋值给新对象。
该函数在 Qt 6.6 中引入。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.