gpt4 book ai didi

java - Java 中的 HMAC_Whirlpool

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:53:51 34 4
gpt4 key购买 nike

我必须制作一个 hmac_whirlpool 哈希算法,但我做错了,因为我得到了错误的结果。对于我的单元测试,我从这个站点 https://quickhash.com/ 获取了结果。我只是试图从维基百科 (https://en.wikipedia.org/wiki/Hash-based_message_authentication_code) 中记下伪代码。

import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

import org.apache.commons.codec.binary.Hex;

import jonelo.jacksum.JacksumAPI;
import jonelo.jacksum.algorithm.AbstractChecksum;
private void createMac() throws NoSuchAlgorithmException, MessageOrKeyEmptyException{

if((_formattedKey!=null) && (_message != null)){

byte _MesStringByte[] = _message.getBytes();
byte[] o_pad = new byte[64];
Arrays.fill( o_pad, (byte) 0x5c);
byte[] o_key_pad =new byte[64];
for(int i = 0;i<64;i++){

o_key_pad[i] = (byte) (_formattedKey[i] ^ o_pad[i]);
}

byte[] i_pad = new byte[64];
Arrays.fill( i_pad, (byte) 0x36);
byte[] i_key_pad =new byte[64];
for(int i = 0;i<64;i++){
i_key_pad[i] = (byte) (_formattedKey[i] ^ i_pad[i]);
}

byte[] tempByteArray1 = new byte[i_key_pad.length + _MesStringByte.length];

System.arraycopy(i_key_pad, 0, tempByteArray1, 0, i_key_pad.length);
System.arraycopy(_MesStringByte, 0, tempByteArray1, i_key_pad.length, _MesStringByte.length);

AbstractChecksum _HmacWhirlInstance = JacksumAPI.getChecksumInstance("whirlpool");

_HmacWhirlInstance.update(tempByteArray1);
byte[] tempByteArray2 = _HmacWhirlInstance.getByteArray();


byte[] tempByteArray3 = new byte[tempByteArray2.length + o_key_pad.length];
System.arraycopy(o_key_pad, 0, tempByteArray3, 0, o_key_pad.length);
System.arraycopy(tempByteArray2, 0, tempByteArray3, o_key_pad.length, tempByteArray2.length);


_HmacWhirlInstance.update(tempByteArray3);

_result = _HmacWhirlInstance.getByteArray();

为了获得 _formattedKey,我只是对 key 进行了哈希处理。那是正确的。来自“#!Mein Geheimnis!#”的漩涡散列是正确的。

private void makeKey() throws NoSuchAlgorithmException{
_KeyStringByte = _key.getBytes();

AbstractChecksum _pruefSumme = JacksumAPI.getChecksumInstance("whirlpool");
_pruefSumme.update(_KeyStringByte);

_formattedKey = _pruefSumme.getByteArray();

}

我的单元测试:

public class TestHMACWhirlpool {

@Test
public void TestHMACWhirlpoolAlgorithm() throws Exception{
//TODO Testdaten richtig einbinen
HMACWhirlpool _HMACWhirlpoolInstance = new HMACWhirlpool();
_HMACWhirlpoolInstance.setKey("#!Mein Geheimnis!#");
_HMACWhirlpoolInstance.setMessage("12345678910");
assertEquals("HMAC-Whirlpool","f108cc1d682905748cd94d32965f21ab783d3bece718aee5dff860a4cb340696e0e17478524678a918e74cc3670067f06e0c4fa11343acc52427da25f23e14c6",Hex.encodeHexString(_HMACWhirlpoolInstance.getEncryptedMessage()));

}

}

也许我对二进制文件和字节数组做错了什么。我找不到我的错误。

亲切的问候!

编辑:

I get "ce1075b6aeb5dc3854e71f748a3160f5f7b40f829a2c915e07f0b95a108225d6d610c1c47352c4997c8878d723063476b7e4e4aab9bc88e5b36e469f1facdb44"instead of "f108cc1d682905748cd94d32965f21ab783d3bece718aee5dff860a4cb340696e0e17478524678a918e74cc3670067f06e0c4fa11343acc52427da25f23e14c6"

最佳答案

我没有重置我的 _HmacWhirlInstance!

关于java - Java 中的 HMAC_Whirlpool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37163517/

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