【问题标题】:Whirlpool hash function doesn't return the expected hashWhirlpool 哈希函数不返回预期的哈希值
【发布时间】:2020-01-16 12:30:41
【问题描述】:

根据https://www.online-convert.com/result/952ea2f0-6d2a-4027-aebf-8309b3888ffb,Whirlpool 哈希函数生成的“test”的哈希为:

B913D5BBB8E461C2C5961CBE0EDCDADFD29F068225CEB37DA6DEFCF89849368F8C6C2EB6A4C4AC75775D032A0ECFDFE8550573062B653FE92FC7B8FB3B7BE8D6

现在,在以下代码行中,我尝试在 Java 中实现相同的功能:

import gnu.crypto.hash.HashFactory;
import gnu.crypto.hash.IMessageDigest;
import gnu.crypto.util.Util;

import java.nio.charset.Charset;

public class Main {
    public static void main(String[] args) {
        byte[] input = "test".getBytes(Charset.forName("UTF-8"));
        IMessageDigest md = HashFactory.getInstance("whirlpool");
        md.update(input, 0, input.length);
        byte[] digest = md.digest();

        System.out.println("expected: B913D5BBB8E461C2C5961CBE0EDCDADFD29F068225CEB37DA6DEFCF89849368F8C6C2EB6A4C4AC75775D032A0ECFDFE8550573062B653FE92FC7B8FB3B7BE8D6");
        System.out.println("real:     " + Util.toString(digest));
    }
}

输出如下:

expected: B913D5BBB8E461C2C5961CBE0EDCDADFD29F068225CEB37DA6DEFCF89849368F8C6C2EB6A4C4AC75775D032A0ECFDFE8550573062B653FE92FC7B8FB3B7BE8D6
real:     E6B4AA087751B4428171777F1893BA585404C7E0171787720EBA0D8BCCD710DC2C42F874C572BFAE4CEDABF50F2C80BF923805D4E31C504B86CA3BC59265E7DD

使用空字符串(类似于 selfTest)它返回预期的字符串。我正在使用来自https://www.gnu.org/software/gnu-crypto/ 的 gnu 加密库 2.0.1。

有没有人暗示为什么真正的哈希与预期的不匹配?

【问题讨论】:

  • 我使用 BouncyCastle 尝试了相同的输入,结果与 GNU Crypto 匹配。您确定“预期”值正确吗?
  • 我将hash.online-convert.com/whirlpool-generator 与“test”一起使用,不带引号作为输入,得到结果“B91...”。有趣的是,你也得到了“E6B...”。
  • 是的,GNU crypto 和 BouncyCastle 都在生成 E6B,,,
  • 使用 openssl: echo -n "test" | openssl dgst -whirlpool (stdin)= b913d5bbb8e461c2c5961cbe0edcdadfd29f068225ceb37da6defcf89849368f8c6c2eb6a4c4ac75775d032a0ecfdfe8550573062b653fe92fc7b8fb3b7spand6
  • 惠而浦经过多次修改。我想首先要问的是,您正在使用或打算使用哪个版本的算法?在线转换器、OpenSSL 和 Crypto++ 使用为 ISO 标准化提交的算法版本 3。 OpenSSL 和 Crypto++ 使用 "test" 并到达 B913D5BBB8E461C2...。也许使用 ISO 测试向量之一会更容易/更好。

标签: java cryptography whirlpool


【解决方案1】:

我在 Whirlpool 和散列中四处走动,最终使用了 Bouncy Castle。如果我运行以下代码,最后一行给出了 B913 的预期结果....

public void bouncyCastle() {
    WhirlpoolDigest messageDigest = new WhirlpoolDigest();

    final String stringToHash = "test";
    messageDigest.reset();
    final byte[] bytes = stringToHash.getBytes();
    messageDigest.update(bytes, 0, bytes.length);

    byte[] hash = new byte[messageDigest.getDigestSize()];

    messageDigest.doFinal(hash, 0);

    System.out.println(Hex.toHexString(hash).toUpperCase());
}

这个网站是一个很好的健全性检查https://md5decrypt.net/en/Whirlpool/#answer

这是我添加到项目中的 maven 依赖项。

    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcprov-ext-jdk15on</artifactId>
        <version>1.64</version>
    </dependency>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    • 2013-10-18
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    相关资源
    最近更新 更多