如何使用充气城堡在 Java 中创建 SHA512 摘要字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2208374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:45:30  来源:igfitidea点击:

How can I create an SHA512 digest string in Java using bouncy castle?

javahashbouncycastlesha512

提问by Lee Warner

This unit test is failing:

此单元测试失败:

    public void testDigest() throws NoSuchAlgorithmException {
    String hashExpected = "150a14ed5bea6cc731cf86c41566ac427a8db48ef1b9fd626664b3bfbb99071fa4c922f33dde38719b8c8354e2b7ab9d77e0e67fc12843920a712e73d558e197";
    MessageDigest md = new MessageDigest();
    String hashActual = new String(md.digest("hi"));
    Assert.assertEquals(hashExpected, hashActual);
}

Below is my implementation of my MessageDigest class:

下面是我的 MessageDigest 类的实现:


import java.io.IOException;
import java.io.InputStream;
import java.security.NoSuchAlgorithmException;
import java.security.Security;

import org.bouncycastle.crypto.Digest; import org.bouncycastle.crypto.digests.SHA512Digest; import org.bouncycastle.crypto.io.DigestInputStream; import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class MessageDigest { private Digest messageDigest;

public MessageDigest() throws NoSuchAlgorithmException {
    Security.addProvider(new BouncyCastleProvider());
    messageDigest = new SHA512Digest();
}

public byte[] digest(String message) {
    byte[] retValue = new byte[messageDigest.getDigestSize()];
    messageDigest.update(message.getBytes(), 0, message.length());
    messageDigest.doFinal(retValue, 0);
    return retValue;
}

}

The test fails with the following reason:

测试失败,原因如下:


junit.framework.ComparisonFailure: expected:<150a14ed5bea6cc731cf86c41566ac427a8db48ef1b9fd626664b3bfbb99071fa4c922f33dde38719b8c8354e2b7ab9d77e0e67fc12843920a712e73d558e197> but was:<
í[êl?1???f?Bz?′??1ybfd3???¤é"ó=T8q???Ta·??wà?á(C'
q.s?Xá

I have a feeling I'm not using the right encoding scheme when I convert my byte[] digest into a string. Any help would be appreciated.

当我将 byte[] 摘要转换为字符串时,我有一种感觉,我没有使用正确的编码方案。任何帮助,将不胜感激。

采纳答案by Kevin

The value you're expecting is a Hex-encoded value. You're creating a String based on the raw bytes, which won't work.

您期望的值是十六进制编码的值。您正在根据原始字节创建一个字符串,但这是行不通的。

You should use the standard Java Crypto API whenever possible instead of BouncyCastle specific APIs.

您应该尽可能使用标准的 Java Crypto API,而不是 BouncyCastle 特定的 API。

Try the following (the Hex library comes from commons-codec):

尝试以下操作(十六进制库来自commons-codec):

Security.addProvider(new BouncyCastleProvider());

String data = "hello world";

MessageDigest mda = MessageDigest.getInstance("SHA-512", "BC");
byte [] digesta = mda.digest(data.getBytes());

MessageDigest mdb = MessageDigest.getInstance("SHA-512", "BC");
byte [] digestb = mdb.digest(data.getBytes());

System.out.println(MessageDigest.isEqual(digesta, digestb));

System.out.println(Hex.encodeHex(digesta));

回答by Chris Jester-Young

Yes, you need to turn your byte array into a hex string. :-) Look into Apache Commons Codec, especially the Hexclass.

是的,您需要将字节数组转换为十六进制字符串。:-) 查看Apache Commons Codec,尤其是Hex类。

回答by jarnbjo

Just an addition to Kevin's answer: Since Java 5, you can use String.format("%0128x", new BigInteger(1, digesta))instead of commons-codec to format the byte array as a 128 digit hex encoded number with leading zeros.

只是凯文回答的补充:从 Java 5 开始,您可以使用String.format("%0128x", new BigInteger(1, digesta))代替 commons-codec 将字节数组格式化为带有前导零的 128 位十六进制编码数字。

回答by Diego

Since BouncyCastle 1.49 there is a handful toHexStringmethod in the Hexclass. For example:

从 BouncyCastle 1.49 开始,该类中有一些toHexString方法Hex。例如:

Hex.toHexString(digest);

will return you the hash digest as a Java Stringin a hexadecimal format.

将以String十六进制格式将哈希摘要作为 Java 返回给您。

For reference see BouncyCastle javadocor grepcode.

如需参考,请参阅BouncyCastle javadocgrepcode