如何使用bouncy castle在Java中創建SHA512摘要字符串?

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


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;

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

public class MessageDigest { private Digest messageDigest;

公共類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.

當我將字節[]摘要轉換為字符串時,我感覺我沒有使用正確的編碼方案。如有任何幫助,我們將不勝感激。

4 个解决方案

#1


34  

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):

試試以下(十六進制庫來自通用編解碼器):

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));

#2


22  

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.

除了Kevin的回答之外,您還可以使用String。格式化(“%0128x”,新的BigInteger(1, digest)),而不是普通的codec,將字節數組格式化為帶前導零的128位十六進制編碼數。

#3


2  

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

是的,您需要將字節數組轉換為十六進制字符串。查看Apache Commons Codec,特別是十六進制類。

#4


2  

Since BouncyCastle 1.49 there is a handful toHexString method in the Hex class. For example:

從BouncyCastle 1.49開始,在十六進制類中有一些toHexString方法。例如:

Hex.toHexString(digest);

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

將以十六進制格式的Java字符串形式返回散列摘要。

For reference see BouncyCastle javadoc or grepcode.

參見BouncyCastle javadoc或grepcode。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/02/05/7300242fa0b61109bfb544ac973de896.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com