PySide6.QtCore.QMessageAuthenticationCode¶
- class QMessageAuthenticationCode¶
QMessageAuthenticationCode
类提供了一种生成基于哈希的消息认证码的方法。更多…概要¶
方法¶
def
__init__()
def
addData()
def
reset()
def
result()
def
resultView()
def
setKey()
def
swap()
静态函数¶
def
hash()
注意
本文档可能包含从C++自动翻译到Python的代码片段。我们始终欢迎对代码片段翻译的贡献。如果您发现翻译问题,您也可以通过在我们的https:/bugreports.qt.io/projects/PYSIDE上创建工单来告知我们。
详细描述¶
警告
本节包含从C++自动翻译到Python的代码片段,可能包含错误。
使用
QMessageAuthenticationCode
类来生成基于哈希的消息认证码(HMACs)。该类支持来自QCryptographicHash
的所有加密哈希算法(另请参阅Algorithm
)。要生成消息认证码,请将合适的哈希算法和密钥传递给构造函数。然后通过调用
addData()
一次或多次来处理消息数据。在完整消息处理完毕后,通过result()
函数获取最终的认证码: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"
对于上述简单情况,您也可以使用静态
hash()
函数:QMessageAuthenticationCode.hash(message, key, QCryptographicHash.Sha256).toHex()
构造一个对象,该对象可用于使用
method
方法和key
密钥从数据创建加密哈希。注意
在Qt 6.6之前的版本中,此函数将其参数作为
QByteArray
,而不是QByteArrayView
。如果您遇到编译错误,那是因为您的代码传递了可以隐式转换为QByteArray
的对象,但不能转换为QByteArrayView
。将相应的参数包装在QByteArray{~~~}
中,以使转换显式。这与旧版本的Qt向后兼容。- addData(data)¶
- Parameters:
数据 –
QByteArrayView
将
data
添加到消息中。注意
在Qt 6.6之前的版本中,此函数将其参数作为
QByteArray
,而不是QByteArrayView
。如果您遇到编译错误,那是因为您的代码传递了可以隐式转换为QByteArray
的对象,但不能转换为QByteArrayView
。将相应的参数包装在QByteArray{~~~}
中,以使转换显式。这与旧版本的Qt向后兼容。另请参阅
- addData(device)
- Parameters:
设备 –
QIODevice
- Return type:
布尔
从打开的
QIODevice
device
中读取数据直到结束,并将其添加到消息中。如果读取成功,则返回true
。注意
device
必须已经打开。- addData(data, length)
- Parameters:
data – 字符串
length – int
这是一个重载函数。
将
data
的前length
个字符添加到消息中。- static hash(message, key, method)¶
- Parameters:
message –
QByteArrayView
key –
QByteArrayView
method –
算法
- Return type:
返回使用密钥
key
和方法method
对消息message
的认证码。注意
在Qt 6.6之前的版本中,此函数将其参数作为
QByteArray
,而不是QByteArrayView
。如果您遇到编译错误,那是因为您的代码传递了可以隐式转换为QByteArray
的对象,但不能转换为QByteArrayView
。将相应的参数包装在QByteArray{~~~}
中,以使转换显式。这与旧版本的Qt向后兼容。另请参阅
hashInto()
- reset()¶
重置消息数据。调用此函数不会影响密钥。
- result()¶
- Return type:
返回最终的认证代码。
另请参阅
- resultView()¶
- Return type:
QByteArrayView
返回最终的哈希值。
请注意,返回的视图仅在
QMessageAuthenticationCode
对象未被其他方式修改时保持有效。另请参阅
- setKey(key)¶
- Parameters:
key –
QByteArrayView
设置密钥
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向后兼容。- swap(other)¶
- Parameters:
交换消息认证码
other
与此消息认证码。此操作非常快速且永远不会失败。