5

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

を実行するSystem.out.println(Main2.sha3(""));と、次の出力が得られます。

C5D2460186F7233C927E7DB2DCC703C0E500B653CA82273B7BFAD8045D85A470

wikipedia: https://en.wikipedia.org/wiki/SHA-3
または NIST 標準: http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/から基本的な sha3 出力を検索すると、SHA3​​-256_Msg0.pdf 、取得する必要があるようです:

a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a

私のコードに間違いはありますか?バウンシーキャッスルの出力とNISTの出力との間のリンクはありますか? 弾む城の実装に間違いはありますか?

お時間をいただきありがとうございます。

4

4 に答える 4

1

の論理が間違っているMain2.toStringと思います。そして、16 進文字列Main2.toStringに変換するべきです。byte[]

1 つの実装を次に示します。

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(""));
    }
}
于 2015-10-19T11:15:08.183 に答える
1

byteArray を String に変換するカスタム メソッドを作成する必要はありません。組み込み関数を使用します。

public static String hashPassword(String password) {

    DigestSHA3 md = new DigestSHA3(256);
    md.update(password.getBytes());
    byte[] digest = md.digest();
    return Hex.toHexString(digest);
}

Mavenエントリは

    <!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on -->
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcprov-jdk15on</artifactId>
        <version>1.56</version>
    </dependency>
于 2017-03-06T07:11:00.250 に答える