我正在处理一个需要执行sha3-256哈希的项目。由于B儿GY城堡在其最新更新中实现了Sha3,我计划使用它们的实现。这是我的代码:
public static String sha3(final String input) {
String hash = "";
final SHA3.DigestSHA3 md = new SHA3.DigestSHA3(256);
md.update(input.getBytes());
hash = Main2.toString(md.digest());
return hash;
}在运行System.out.println(Main2.sha3(""));时,我得到以下输出:
C5D2460186F7233C927E7DB2DCC703C0E500B653CA82273B7BFAD8045D85A470
当我搜索基本的sha3输出时: wikipedia:https://en.wikipedia.org/wiki/SHA-3
or NIST standards: [http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA3-256\_Msg0.pdf](http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA3-256_Msg0.pdf) , it seems I should obtain:a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a
我的代码有错误吗?弹跳城堡的输出和NIST的有联系吗?弹跳城堡的实施会不会有错误?
谢谢你的时间和问候。
发布于 2015-10-19 12:15:46
您的SHA3应正确计算。
您的问题中的代码有问题:
Main2.toString(String)以下散列并将字节转换为十六进制字符串:
import java.security.MessageDigest;
import org.bouncycastle.jcajce.provider.digest.SHA3.DigestSHA3;
import org.bouncycastle.jcajce.provider.digest.SHA3.Digest256;
public class TestSha3 {
public static void main(String[] args) {
System.out.println(sha3(""));
}
public static String sha3(final String input) {
final DigestSHA3 sha3 = new Digest256();
sha3.update(input.getBytes());
return TestSha3.hashToString(sha3);
}
public static String hashToString(MessageDigest hash) {
return hashToString(hash.digest());
}
public static String hashToString(byte[] hash) {
StringBuffer buff = new StringBuffer();
for (byte b : hash) {
buff.append(String.format("%02x", b & 0xFF));
}
return buff.toString();
}
}输出
a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a我在我的Maven构建中使用了以下工件
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.53</version>
</dependency>发布于 2016-05-17 07:09:40
如果您使用的是bouncy城堡1.50库(输入: SHA3-256("")),则bouncy库的输出与NIST输出不匹配。
使用bouncy城堡1.54版本输出的bouncy等于NIST示例值( https://en.wikipedia.org/wiki/SHA-3中的示例值)
谢谢
发布于 2015-10-19 11:15:08
我想Main2.toString.的逻辑是错误的,,Main2.toString应将byte[]转换为十六进制字符串。
以下是一个实现:
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}我已经尝试过您的代码,输出是:
A7FFC6F8BF1ED76651C14756A061D662F580FF4DE43B49FA82D80A4B80F8434A
/**
* Created by chenzhongpu on 19/10/2015.
*/
import org.bouncycastle.jcajce.provider.digest.*;
public class TestSHA3 {
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
public static String sha3(final String input){
String hash = "";
SHA3.DigestSHA3 md = new SHA3.DigestSHA3(256);
md.update(input.getBytes());
hash = bytesToHex(md.digest());
return hash;
}
public static void main(String[] args) {
System.out.println(sha3(""));
}
}https://stackoverflow.com/questions/33211926
复制相似问题