HMAC_Whirlpool в Java
Я должен сделать алгоритм хеширования 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, я просто хешировал ключ. Это работает правильно. Хэш-вихрь из "#!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()));
}
}
Может быть, я что-то не так делаю с binarys и байтовым массивом. Я не могу найти свою ошибку.
Добрые пожелания!
РЕДАКТИРОВАТЬ:
Я получаю "ce1075b6aeb5dc3854e71f748a3160f5f7b40f829a2c915e07f0b95a108225d6d610c1c47352c4997c8878d723063476b7e4e4aab9bc88e5b36e469f1facdb44" вместо "f108cc1d682905748cd94d32965f21ab783d3bece718aee5dff860a4cb340696e0e17478524678a918e74cc3670067f06e0c4fa11343acc52427da25f23e14c6"