在 Java 中计算 SHA 3 哈希
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30109958/
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
Calculating SHA 3 Hash in Java
提问by gye
I have been using CryptoJS (i.e. CryptoJS.algo.SHA3.creat()
) library to SHA-3 hash on the front end. (see http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha3.js)
我一直在使用 CryptoJS(即CryptoJS.algo.SHA3.creat()
)库在前端进行 SHA-3 散列。(见http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha3.js)
I was wondering if there are any Java library equivalence? But so far I haven't found any. There are not so many Java SHA-3 examples either.
我想知道是否有任何 Java 库等价物?但到目前为止,我还没有找到任何。也没有那么多 Java SHA-3 示例。
sun.security.Provider
has SHA-3, but it's not visible under Eclipse. Also, I am not sure whether this sun's SHA-3 is same as the CryptoJS's SHA-3.
sun.security.Provider
有 SHA-3,但它在 Eclipse 下不可见。另外,我不确定这个太阳的 SHA-3 是否与 CryptoJS 的 SHA-3 相同。
Could anyone please provide some examples?
任何人都可以提供一些例子吗?
采纳答案by gye
The common Java reference implementation for crypto and crypto support is probably BouncyCastle. It can be a big library to bring in, which is why we often reach into sun.security (rightly or wrongly.)
加密和加密支持的通用 Java 参考实现可能是BouncyCastle。它可以是一个大图书馆,这就是为什么我们经常接触 sun.security (正确或错误)。
Anyway, BouncyCastleseems to offer org.bouncycastle.jcajce.provider.digest.SHA3.DigestSHA3
无论如何,BouncyCastle似乎提供org.bouncycastle.jcajce.provider.digest.SHA3.DigestSHA3
回答by pratik deshai
回答by Guillaume Husta
Thanks to @jdv for his answer. I'm adding more information to have a quick start and an example.
感谢@jdv 的回答。我正在添加更多信息以快速入门和示例。
First, add the BouncyCastle maven dependency, or get it on Maven Central:
首先,添加 BouncyCastle Maven 依赖项,或者在Maven Central上获取它:
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.56</version>
</dependency>
Then we can test it :
然后我们可以测试一下:
import org.bouncycastle.jcajce.provider.digest.SHA3;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;
@Test
public void testSha3() throws Exception {
String input = "Hello world !";
SHA3.DigestSHA3 digestSHA3 = new SHA3.Digest512();
byte[] digest = digestSHA3.digest(input.getBytes());
System.out.println("SHA3-512 = " + Hex.toHexString(digest));
}
You'll get this result (512 bits or 64 bytes in hexadecimal) :
你会得到这个结果(512 位或 64 字节的十六进制):
SHA3-512 = e9a4fd120d684537d57f314d51213ce5acef8ff6974e2b7599674ef0f0a3cf111f0d26ed602db946739da448210fb76a7621d7a8f07728372b10a975f36d8e37
SHA3-512 = e9a4fd120d684537d57f314d51213ce5acef8ff6974e2b7599674ef0f0a3cf111f0d26ed602db946739da448210fb707a7d3707210fb707a7d3707a7d868f707a868f707a7d860
You can compare it with this result : https://duckduckgo.com/?q=sha3+%22Hello+world+!%22&ia=answer
您可以将其与此结果进行比较:https: //duckduckgo.com/?q=sha3+%22Hello+world+!%22&ia=answer