Bouncy Castle Sha3错误输出?

use*_*004 5 java hash bouncycastle sha-3

我正在开发一个需要执行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());

    return hash;
  }
Run Code Online (Sandbox Code Playgroud)

运行时System.out.println(Main2.sha3(""));,我得到以下输出:

C5D2460186F7233C927E7DB2DCC703C0E500B653CA82273B7BFAD8045D85A470

当我搜索来自维基百科的基本sha3输出:https://en.wikipedia.org/wiki/SHA-3
或NIST标准:http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/ SHA3​​-256_Msg0.pdf ,似乎我应该获得:

a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a

我的代码中有错误吗?充气城堡的输出和NIST之间的任何联系?在充气城堡的实施中会出现错误吗?

谢谢你的时间和问候.

Mr.*_*irl 6

您的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();
    }
}
Run Code Online (Sandbox Code Playgroud)

产量

a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a
Run Code Online (Sandbox Code Playgroud)

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

<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk15on</artifactId>
    <version>1.53</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)


che*_*gpu 1

我猜你的逻辑是错误的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);
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试过你的代码,输出是:

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(""));
    }
}
Run Code Online (Sandbox Code Playgroud)