I have to make a hmac_whirlpool hash algorithm but I do something wrong because I get the worng result. For my unit test I took the results from this site https://quickhash.com/. I just tryed to write down the pseudocode from wikipedia(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();
To get the _formattedKey I just hashed the key. That works correct. The whirlpool hash from "#!Mein Geheimnis!#" is correct.
private void makeKey() throws NoSuchAlgorithmException{
_KeyStringByte = _key.getBytes();
AbstractChecksum _pruefSumme = JacksumAPI.getChecksumInstance("whirlpool");
_pruefSumme.update(_KeyStringByte);
_formattedKey = _pruefSumme.getByteArray();
}
My Unit test:
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()));
}
}
Maybe I do something wrong with the binarys and the byte array. I can't find my mistake.
Kindly Regards!
EDIT:
I get "ce1075b6aeb5dc3854e71f748a3160f5f7b40f829a2c915e07f0b95a108225d6d610c1c47352c4997c8878d723063476b7e4e4aab9bc88e5b36e469f1facdb44" instead of "f108cc1d682905748cd94d32965f21ab783d3bece718aee5dff860a4cb340696e0e17478524678a918e74cc3670067f06e0c4fa11343acc52427da25f23e14c6"
I didnt reset my _HmacWhirlInstance!