问题描述
此单元测试失败:
公共无效testDigest()抛出抛出:NoSuchAlgorithmException {
字符串hashExpected =150a14ed5bea6cc731cf86c41566ac427a8db48ef1b9fd626664b3bfbb99071fa4c922f33dde38719b8c8354e2b7ab9d77e0e67fc12843920a712e73d558e197 ;
MessageDigest md = new MessageDigest();
String hashActual = new String(md.digest(hi));
Assert.assertEquals(hashExpected,hashActual);
$ b下面是我的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()抛出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);
返回retValue;
}
$ c
$ b测试失败,原因如下:
junit.framework。 ComparisonFailure:预期:其中150a14ed5bea6cc731cf86c41566ac427a8db48ef1b9fd626664b3bfbb99071fa4c922f33dde38719b8c8354e2b7ab9d77e0e67fc12843920a712e73d558e197>但:其中
I [êlÇ1φÄf¬Bz'Žñ¹ýbfd³¿»™¤ÉO =Þ8q>ŒƒTâ·«wàæÁ(C
q.sÕXá
我有一种感觉,当我将字节[]摘要转换为字符串时,我没有使用正确的编码方案。
解决方案您期望的值是一个十六进制编码的值。原始字节,这是行不通的。
您应该尽可能使用标准的Java Crypto API,而不是BouncyCastle特定的API。
请尝试以下操作(Hex库来自):
Security.addProvider(new BouncyCastleProvider());
String data =hello world;
MessageDigest mda = MessageDigest.getInstance(SHA-512,BC);
byte [] digesta = mda.digest(data.getBytes());
MessageDigest mdb = Me ssageDigest.getInstance(SHA-512,BC);
byte [] digestb = mdb.digest(data.getBytes());
System.out.println(MessageDigest.isEqual(digesta,digestb));
System.out.println(Hex.encodeHex(digesta));
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:
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�´Žñ¹ýbfd³¿»™¤É"ó=Þ8q›ŒƒTâ·«�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.
解决方案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.
Try the following (the Hex library comes from 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));
这篇关于如何使用弹性城堡在Java中创建SHA512摘要字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!