Bouncy Castle Sha3输出错误? [英] Bouncy Castle Sha3 wrong output?

查看:313
本文介绍了Bouncy Castle Sha3输出错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要执行 sha3-256 哈希的JAVA项目。
由于Bouncy Castle在其最新更新中实施了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());

返回散列;
}

运行 System.out.println(Main2。我得到以下输出:


C5D2460186F7233C927E7DB2DCC703C0E500B653CA82273B7BFAD8045D85A470


当我搜索fot基本sha3输出:
wikipedia: https://en.wikipedia.org/wiki/SHA-3


NIST标准: http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/ SHA3​​-256_Msg0.pdf
,看来我应该获得:


a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a

我的代码中有任何错误吗?弹性城堡的输出与NIST之间的任何联系?在充气城堡的实施过程中是否会出现错误?



感谢您的时间和问候。

解决方案

您的SHA3应该正确计算。



您的问题中的代码存在问题:




  • 您尚未提供 Main2.toString(String)



以下散列并将字节转换为十六进制字符串:

$ p $ import $ java.security.MessageDigest ;

导入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();

(byte b:hash){
buff.append(String.format(%02x,b& 0xFF));
}

return buff.toString();




$ h $输出

  a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a 






我在我的Maven构建中使用了以下工件

 < dependency> 
< groupId> org.bouncycastle< / groupId>
< artifactId> bcprov-jdk15on< / artifactId>
< version> 1.53< / version>
< /依赖关系>


I'm working on a JAVA project that needs to perform a sha3-256 hash. Since Bouncy Castle implemented Sha3 in its latest update, I plan to use their implementation. Here is my code:

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

When running System.out.println(Main2.sha3(""));, I get the following output:

C5D2460186F7233C927E7DB2DCC703C0E500B653CA82273B7BFAD8045D85A470

When I search fot basic sha3 outputs from: wikipedia: https://en.wikipedia.org/wiki/SHA-3
or NIST standards: http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA3-256_Msg0.pdf , it seems I should obtain:

a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a

Is there any mistake in my code? Any link between bouncy castle's output and NIST's? Would there be a mistake in bouncy castle's implementation?

Thanks for your time and regards.

解决方案

Your SHA3 should be computed correctly.

You have an issue with the code in your question:

  • You have not provided Main2.toString(String)

The following hashes and transforms the bytes into a hexadecimal 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();
    }
}

Output

a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a


I used the following artifact in my Maven build

<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk15on</artifactId>
    <version>1.53</version>
</dependency>

这篇关于Bouncy Castle Sha3输出错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆