3

我必须制作一个 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()));

    }

}

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

亲切的问候!

编辑:

I get "ce1075b6aeb5dc3854e71f748a3160f5f7b40f829a2c915e07f0b95a108225d6d610c1c47352c4997c8878d723063476b7e4e4aab9bc88e5b36e469f1facdb44" instead of "f108cc1d682905748cd94d32965f21ab783d3bece718aee5dff860a4cb340696e0e17478524678a918e74cc3670067f06e0c4fa11343acc52427da25f23e14c6"

4

1 回答 1

1

我没有重置我的 _HmacWhirlInstance!

于 2016-05-14T18:22:54.553 回答