QCryptographicHash::Sha3_256 Qt5.4 和 Qt5.8 不同

QCryptographicHash::Sha3_256 differs in Qt5.4 and Qt5.8

我用QCryptographicHash::hash(data, QCryptographicHash::Sha3_256).toHex() 密码编码函数 123456 我得到两个不同的结果:

Qt5.4: c888c9ce9e098d5864d3ded6ebcc140a12142263bace3a23a36f9905f12bd64a 
Qt5.8: d7190eb194ff9494625514b6d178c87f99c5973e28c398969d2233f2960a573e

在 Qt 文档中说:

Note: In Qt versions before 5.9, when asked to generate a SHA3 hash sum, QCryptographicHash actually calculated Keccak. If you need compatibility with SHA-3 hashes produced by those versions of Qt, use the Keccak_ enumerators. Alternatively, if source compatibility is required, define the macro QT_SHA3_KECCAK_COMPAT.

但我使用 github 5.8 分支的 Qt5.8 源代码!不是 5.9。我做错了什么?

您可以使用 this one to calculate the SHA3_256 hash of your password and similarly this one 等在线工具来计算 Keccak-256 哈希值。它显示了您在问题中显示的确切结果。

所以 seems correct: the behavior written up in that documentation was at some point introduced into the 5.8 branch. You can see for yourself in the git history of the file qcryptographichash.cpp in the branch called 5.8 where this happened. You can also see that that was done after the tag v5.8.0 was applied.

因此,如果您切换回标记为 v5.8.0 的版本,您将获得与 5.4.

中相同的行为

更新以回复您的评论。

文档中描述的机制,即定义宏 QT_SHA3_KECCAK_COMPAT 以保持与旧版本的向后兼容性,未向后移植到 5.8。显示 5.85.9 分支之间的相关差异(我减少了输出):

git difftool -y -x "diff -y -W 72" 5.8 5.9 -- qcryptographichash.h

    enum Algorithm {                        enum Algorithm {
#ifndef QT_CRYPTOGRAPHICHASH_ONL        #ifndef QT_CRYPTOGRAPHICHASH_ONL
        Md4,                                    Md4,
        Md5,                                    Md5,
#endif                                  #endif
        Sha1 = 2,                               Sha1 = 2,
#ifndef QT_CRYPTOGRAPHICHASH_ONL        #ifndef QT_CRYPTOGRAPHICHASH_ONL
        Sha224,                                 Sha224,
        Sha256,                                 Sha256,
        Sha384,                                 Sha384,
        Sha512,                                 Sha512,
        Sha3_224,                  |
        Sha3_256,                  |            Keccak_224 = 7,
        Sha3_384,                  |            Keccak_256,
        Sha3_512                   |            Keccak_384,
                                   >            Keccak_512,
                                   >            RealSha3_224 = 11,
                                   >            RealSha3_256,
                                   >            RealSha3_384,
                                   >            RealSha3_512,
                                   >    #  ifndef QT_SHA3_KECCAK_COMPAT
                                   >            Sha3_224 = RealSha3_224,
                                   >            Sha3_256 = RealSha3_256,
                                   >            Sha3_384 = RealSha3_384,
                                   >            Sha3_512 = RealSha3_512
                                   >    #  else
                                   >            Sha3_224 = Keccak_224,
                                   >            Sha3_256 = Keccak_256,
                                   >            Sha3_384 = Keccak_384,
                                   >            Sha3_512 = Keccak_512
                                   >    #  endif
#endif                                  #endif
    };                                      };

如你所见,宏QT_SHA3_KECCAK_COMPAT只在5.9中有意义。