QMessageAuthenticationCode Class
QMessageAuthenticationCode 类提供了一种生成基于散列的消息验证码的方法。更多
Header: | #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 。
成员函数文档
[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)
使用密钥key 和方法method 返回信息(message 或QSpan 的重载,messageParts 的连接)的验证码。
返回值将是buffer 的子跨度,除非buffer 的大小不够,在这种情况下,将返回空值QByteArrayView 。
此函数在 Qt 6.8 中引入。
另请参阅 hash()。
[explicit]
QMessageAuthenticationCode::QMessageAuthenticationCode(QCryptographicHash::Algorithm method, QByteArrayView key = {})
构造一个对象,用于使用method 方法和密钥key 从数据创建加密哈希值。
注意: 在 6.6 之前的 Qt XML 版本中,该函数的参数是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 添加到消息中。
注意: 在 6.6 之前的 Qt XML 版本中,该函数的参数是QByteArray ,而不是QByteArrayView 。如果您遇到编译错误,那是因为您的代码传递了可隐式转换为QByteArray 的对象,而不是QByteArrayView 。将相应的参数用QByteArray{~~~}
包起来,使转换显式化。这与旧版本的 Qt 向后兼容。
另请参见 resultView() 和result()。
bool QMessageAuthenticationCode::addData(QIODevice *device)
从打开的QIODevice device 中读取数据,直到结束并将其添加到消息中。如果读取成功,则返回true
。
注意: device 必须已经打开。
void QMessageAuthenticationCode::addData(const char *data, qsizetype length)
这是一个重载函数。
将length 的前几个字符data 添加到信息中。
[static]
QByteArray QMessageAuthenticationCode::hash(QByteArrayView message, QByteArrayView key, QCryptographicHash::Algorithm method)
使用密钥key 和方法method 返回消息message 的验证码。
注意: 在 6.6 之前的 Qt XML 版本中,该函数的参数是QByteArray ,而不是QByteArrayView 。如果您遇到编译错误,那是因为您的代码传递了可隐式转换为QByteArray 的对象,而不是QByteArrayView 。将相应的参数用QByteArray{~~~}
包起来,使转换显式化。这与旧版本的 Qt 向后兼容。
另请参见 hashInto()。
[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);
注意: 在 6.6 之前的 Qt XML 版本中,该函数的参数为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 实例。
注意: moved-from 对象other 处于部分形成状态,在这种状态下,唯一有效的操作是销毁和赋值一个新对象。
此函数在 Qt 6.6 中引入。
© 2025 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.