gpt4 book ai didi

java - 为什么 BouncyCaSTLe Keccak sha-384 和 MessageDigest sha-384 有不同的输出?

转载 作者:行者123 更新时间:2023-11-29 07:26:53 25 4
gpt4 key购买 nike

散列一个只有一个字节的数组的简单示例:

import java.security.MessageDigest;
import java.util.Arrays;
import org.bouncycastle.jcajce.provider.digest.Keccak;

public class Program {
public static void main(String[ ] args) {
// MessageDigest:
byte[] state = { (byte) 1};
try {
MessageDigest md5 = MessageDigest.getInstance("SHA-384");
byte[] digest = md5.digest(state);
System.out.println("A: " + Arrays.toString(digest));
} catch (Exception e) {}

// BouncyCastle Keccak:
Keccak.Digest384 keccak = new Keccak.Digest384();
keccak.update(state);
try {
byte[] digest = new byte[48];
keccak.digest(digest, 0, 48);
System.out.println("B: " + Arrays.toString(digest));
} catch (Exception e) {}
}
}

这个程序的输出如下:

A: [-115, 44, -24, 125, -122, -11, 95, -49, -85, 119, 10, 4, 123, 9, 13, -94, 50, 112, -6, 32, 104, 50, -33, -22, 126, 12, -108, 111, -1, 69, 31, -127, -102, -35, 36, 35, 116, -66, 85, 27, 13, 99, 24, -19, 108, 125, 65, -40]

B: [45, -11, 56, -101, -127, 15, 27, 78, 83, 13, 3, -41, 58, -24, 82, -74, 37, -120, -70, 20, 26, 16, 66, 76, 2, 104, -22, -24, -58, 79, -81, 58, -71, 1, -14, -115, -27, 85, 73, -70, -8, 113, -67, -93, -19, 62, -15, 65]

它们不应该相同还是我遗漏了什么?

使用的 BouncyCaSTLe 版本: https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on/1.58

Java: jdk1.8.0_121

最佳答案

“SHA-384”是 SHA2 系列的散列函数,具有 384 位输出。你想要“SHA3-384”。这里有一些示例代码使用您的示例和 Bobulous'示例。

import org.bouncycastle.jcajce.provider.digest.SHA3;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.security.MessageDigest;
import java.security.Security;
import java.util.Arrays;

public class Main {

public static void main(String[] args) throws Exception {
Security.addProvider(new BouncyCastleProvider());
MessageDigest md5 = MessageDigest.getInstance("SHA-384");
byte[] state = new byte[]{1};
byte[] digest = md5.digest(state);
System.out.println("A: " + Arrays.toString(digest));
md5 = MessageDigest.getInstance("SHA3-384");
digest = md5.digest(state);
System.out.println("B: " + Arrays.toString(digest));
SHA3.DigestSHA3 sha3 = new SHA3.DigestSHA3(384);
digest = sha3.digest(state);
System.out.println("C: " + Arrays.toString(digest));
}
}

输出是:

A: [-115, 44, -24, 125, -122, -11, 95, -49, -85, 119, 10, 4, 123, 9, 13, -94, 50, 112, -6, 32, 104, 50, -33, -22, 126, 12, -108, 111, -1, 69, 31, -127, -102, -35, 36, 35, 116, -66, 85, 27, 13, 99, 24, -19, 108, 125, 65, -40]
B: [49, 123, -48, 9, 32, 59, -57, -101, 6, 78, 83, -8, -20, -53, 99, 46, 5, 19, -121, 44, -98, -59, -64, -90, -108, -106, -82, -8, -106, 113, -81, 33, -19, -110, -93, -78, 85, 72, 12, 90, 9, 118, -22, 73, 37, -118, -64, -114]
C: [49, 123, -48, 9, 32, 59, -57, -101, 6, 78, 83, -8, -20, -53, 99, 46, 5, 19, -121, 44, -98, -59, -64, -90, -108, -106, -82, -8, -106, 113, -81, 33, -19, -110, -93, -78, 85, 72, 12, 90, 9, 118, -22, 73, 37, -118, -64, -114]

关于java - 为什么 BouncyCaSTLe Keccak sha-384 和 MessageDigest sha-384 有不同的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50660299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com