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

注意: 在 5.9 之前的 Qt 版本中,当被要求生成 SHA3 哈希和时,QCryptographicHash 实际上计算的是 Keccak。如果需要与这些版本的 Qt XML 生成的 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 中引入

成员函数文档

[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 是一个跨距,则会按指定顺序将所有字节数组视图添加到哈希值中。

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

此函数在 Qt 6.8 中引入。

另请参阅 hash()。

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

构造一个对象,可用于使用method 从数据创建加密哈希值。

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

移动-从other 构建一个新的 QCryptographicHash。

注意: moved-from 对象other 处于部分形成状态,其中唯一有效的操作是销毁和赋值。

此函数在 Qt 6.5 中引入。

[noexcept] QCryptographicHash::~QCryptographicHash()

销毁对象。

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

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

注意: 在 6.3 之前的 Qt 版本中,此函数使用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 的哈希值。

注意: 在 6.3 之前的 Qt 版本中,此函数使用QByteArray ,而非QByteArrayView

另请参阅 hashInto()。

[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 是否受支持,如果result() 将在使用method 时返回值。

注意: 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 实例。

注意: moved-from 对象other 处于部分形成状态,其中唯一有效的操作是销毁和赋新值。

此函数在 Qt 6.5 中引入。

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