本页内容

QCryptographicHash Class

QCryptographicHash 类提供了一种生成加密哈希值的方法。更多内容...

头文件: #include <QCryptographicHash>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

注意:该类中的所有函数均为可重入的。

公共类型

enum Algorithm { Md4, Md5, Sha1, Sha224, Sha256, …, Blake2s_256 }

公共函数

QCryptographicHash(QCryptographicHash::Algorithm method)
(since 6.5) QCryptographicHash(QCryptographicHash &&other)
~QCryptographicHash()
void addData(QByteArrayView bytes)
bool addData(QIODevice *device)
(since 6.5) QCryptographicHash::Algorithm algorithm() const
void reset()
QByteArray result() const
(since 6.3) QByteArrayView resultView() const
(since 6.5) void swap(QCryptographicHash &other)
(since 6.5) QCryptographicHash &operator=(QCryptographicHash &&other)

静态公共成员

QByteArray hash(QByteArrayView data, QCryptographicHash::Algorithm method)
(since 6.8) QByteArrayView hashInto(QSpan<char> buffer, QByteArrayView data, QCryptographicHash::Algorithm method)
(since 6.8) QByteArrayView hashInto(QSpan<char> buffer, QSpan<const QByteArrayView> data, QCryptographicHash::Algorithm method)
(since 6.8) QByteArrayView hashInto(QSpan<std::byte> buffer, QByteArrayView data, QCryptographicHash::Algorithm method)
(since 6.8) QByteArrayView hashInto(QSpan<std::byte> buffer, QSpan<const QByteArrayView> data, QCryptographicHash::Algorithm method)
(since 6.8) QByteArrayView hashInto(QSpan<uchar> buffer, QByteArrayView data, QCryptographicHash::Algorithm method)
(since 6.8) QByteArrayView hashInto(QSpan<uchar> buffer, QSpan<const QByteArrayView> data, QCryptographicHash::Algorithm method)
int hashLength(QCryptographicHash::Algorithm method)
(since 6.5) bool supportsAlgorithm(QCryptographicHash::Algorithm method)

详细说明

QCryptographicHash 可用于生成二进制或文本数据的加密哈希值。

有关支持的算法列表,请参阅QCryptographicHash::Algorithm 枚举的文档。

成员类型文档

enum QCryptographicHash::Algorithm

注意:在 Qt 5.9 之前的版本中,当要求生成 SHA3 哈希值时,QCryptographicHash 实际上计算的是 Keccak。如果您需要与这些 Qt 版本生成的 SHA-3 哈希值保持兼容,请使用Keccak_ 中的枚举器。此外,如果需要源代码兼容性,请定义宏QT_SHA3_KECCAK_COMPAT 。

常量值描述
QCryptographicHash::Md40生成 MD4 哈希值
QCryptographicHash::Md51生成 MD5 哈希值
QCryptographicHash::Sha12生成 SHA-1 哈希值
QCryptographicHash::Sha2243生成 SHA-224 哈希值(SHA-2)。自 Qt 5.0 起引入
QCryptographicHash::Sha2564生成 SHA-256 哈希值(SHA-2)。引入于 Qt 5.0
QCryptographicHash::Sha3845生成 SHA-384 哈希值(SHA-2)。自 Qt 5.0 起引入
QCryptographicHash::Sha5126生成 SHA-512 哈希值(SHA-2)。自 Qt 5.0 起引入
QCryptographicHash::Sha3_224RealSha3_224生成 SHA3-224 哈希值。自 Qt 5.1 起引入
QCryptographicHash::Sha3_256RealSha3_256生成 SHA3-256 哈希值。引入于 Qt 5.1
QCryptographicHash::Sha3_384RealSha3_384生成 SHA3-384 哈希值。自 Qt 5.1 起引入
QCryptographicHash::Sha3_512RealSha3_512生成 SHA3-512 哈希值。自 Qt 5.1 起引入
QCryptographicHash::Keccak_2247生成 Keccak-224 哈希值。自 Qt 5.9.2 起引入
QCryptographicHash::Keccak_2568生成 Keccak-256 哈希值。自 Qt 5.9.2 起引入
QCryptographicHash::Keccak_3849生成 Keccak-384 哈希值。自 Qt 5.9.2 起引入
QCryptographicHash::Keccak_51210生成 Keccak-512 哈希值。自 Qt 5.9.2 起引入
QCryptographicHash::Blake2b_16015生成 BLAKE2b-160 哈希值。自 Qt 6.0 起引入
QCryptographicHash::Blake2b_25616生成 BLAKE2b-256 哈希值。自 Qt 6.0 起引入
QCryptographicHash::Blake2b_38417生成 BLAKE2b-384 哈希值。自 Qt 6.0 起引入
QCryptographicHash::Blake2b_51218生成 BLAKE2b-512 哈希值。引入于 Qt 6.0
QCryptographicHash::Blake2s_12819生成 BLAKE2s-128 哈希值。自 Qt 6.0 起引入
QCryptographicHash::Blake2s_16020生成 BLAKE2s-160 哈希值。自 Qt 6.0 起引入
QCryptographicHash::Blake2s_22421生成 BLAKE2s-224 哈希值。自 Qt 6.0 起引入
QCryptographicHash::Blake2s_25622生成 BLAKE2s-256 哈希值。首次引入于 Qt 6.0

成员函数文档

[explicit] QCryptographicHash::QCryptographicHash(QCryptographicHash::Algorithm method)

构建一个对象,该对象可用于通过调用method 方法,根据数据生成加密哈希值。

[noexcept, since 6.5] QCryptographicHash::QCryptographicHash(QCryptographicHash &&other)

从 `other` 通过 `move` 构造一个新的 `QCryptographicHash`。

注意: 被移动的对象 other 将处于一种部分构建状态,在此状态下,唯一有效的操作是销毁和赋值。

该函数在 Qt 6.5 中引入。

[noexcept] QCryptographicHash::~QCryptographicHash()

销毁该对象。

[noexcept] void QCryptographicHash::addData(QByteArrayView bytes)

将bytes 中的字符添加到加密哈希中。

注意:在 Qt 6.3 之前的版本中,此函数接受的是 `QByteArray`,而不是 `QByteArrayView`。

bool QCryptographicHash::addData(QIODevice *device)

从已打开的QIODevice device 中读取数据,直至数据结束,并对其进行哈希运算。若读取成功,则返回true 。

[noexcept, since 6.5] QCryptographicHash::Algorithm QCryptographicHash::algorithm() const

返回用于生成加密哈希的算法。

该函数于 Qt 6.5 中引入。

[static] QByteArray QCryptographicHash::hash(QByteArrayView data, QCryptographicHash::Algorithm method)

使用method 返回data 的哈希值。

注意:在 Qt 6.3 之前的版本中,此函数接受的是 `QByteArray`,而不是 `QByteArrayView`。

另请参阅 hashInto()。

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

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

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

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

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

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

使用method 返回data 的哈希值,并将结果存储在buffer 中。

如果 `data ` 是一个 `span`,则按给定的顺序将所有字节数组视图添加到哈希中。

返回值将是buffer 的子 span,除非buffer 的大小不足,在这种情况下将返回一个空的QByteArrayView 。

这些函数在 Qt 6.8 中引入。

另请参阅 hash()。

[static] int QCryptographicHash::hashLength(QCryptographicHash::Algorithm method)

返回所选哈希method 的输出大小(以字节为单位)。

[noexcept] void QCryptographicHash::reset()

重置该对象。

QByteArray QCryptographicHash::result() const

返回最终的哈希值。

另请参阅 resultView() 和QByteArray::toHex()。

[noexcept, since 6.3] QByteArrayView QCryptographicHash::resultView() const

返回最终的哈希值。

请注意,只有当QCryptographicHash 对象未通过其他方式被修改时,返回的视图才保持有效。

该函数在 Qt 6.3 中引入。

另请参阅 result()。

[static, since 6.5] bool QCryptographicHash::supportsAlgorithm(QCryptographicHash::Algorithm method)

返回所选算法method 是否受支持,以及在使用method 时,result()是否会返回一个值。

注意: 当OpenSSL 作为提供程序使用时,将由其负责提供此信息;否则将返回true ,因为非 OpenSSL 实现不存在任何限制。若无法查询 OpenSSL,则返回false 。

该函数在 Qt 6.5 中引入。

[noexcept, since 6.5] void QCryptographicHash::swap(QCryptographicHash &other)

将此加密哈希与other 进行交换。该操作速度极快,且绝不会失败。

该函数于 Qt 6.5 版本中引入。

[noexcept, since 6.5] QCryptographicHash &QCryptographicHash::operator=(QCryptographicHash &&other)

将other 通过移动赋值操作赋值给此QCryptographicHash 实例。

注意: 被移动的对象 other 将处于部分初始化状态,在此状态下,唯一有效的操作是销毁和赋予新值。

该函数在 Qt 6.5 中引入。

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