All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.testifyproject.bouncycastle.crypto.test.HashCommitmentTest Maven / Gradle / Ivy

The newest version!
package org.testifyproject.bouncycastle.crypto.test;

import java.security.SecureRandom;

import org.testifyproject.bouncycastle.crypto.Commitment;
import org.testifyproject.bouncycastle.crypto.Committer;
import org.testifyproject.bouncycastle.crypto.DataLengthException;
import org.testifyproject.bouncycastle.crypto.org.testifyproject.testifyprojectmitments.GeneralHashCommitter;
import org.testifyproject.bouncycastle.crypto.org.testifyproject.testifyprojectmitments.HashCommitter;
import org.testifyproject.bouncycastle.crypto.digests.SHA1Digest;
import org.testifyproject.bouncycastle.crypto.digests.SHA256Digest;
import org.testifyproject.bouncycastle.util.Arrays;
import org.testifyproject.bouncycastle.util.encoders.Hex;
import org.testifyproject.bouncycastle.util.test.SimpleTest;

public class HashCommitmentTest
    extends SimpleTest
{
    public String getName()
    {
        return "HashCommitmentTest";
    }

    public void performBasicTest()
        throws Exception
    {
        byte[] data = Hex.decode("4e6f77206973207468652074696d6520666f7220616c6c20");

        Committer org.testifyproject.testifyprojectmitter = new HashCommitter(new SHA256Digest(), new SecureRandom());

        Commitment c = org.testifyproject.testifyprojectmitter.org.testifyproject.testifyprojectmit(data);

        org.testifyproject.testifyprojectmitter = new HashCommitter(new SHA256Digest(), new SecureRandom());

        if (!org.testifyproject.testifyprojectmitter.isRevealed(c, data))
        {
            fail("org.testifyproject.testifyprojectmitment failed to validate");
        }

        org.testifyproject.testifyprojectmitter = new HashCommitter(new SHA1Digest(), new SecureRandom());

        if (org.testifyproject.testifyprojectmitter.isRevealed(c, data))
        {
            fail("org.testifyproject.testifyprojectmitment validated!!");
        }

        try
        {
            org.testifyproject.testifyprojectmitter.isRevealed(c, new byte[data.length + 1]);
        }
        catch (Exception e)
        {
            if (!e.getMessage().equals("Message and witness secret lengths do not match."))
            {
                fail("exception thrown but wrong message");
            }
        }

        // SHA1 has a block size of 512 bits, try a message that's too big

        try
        {
            c = org.testifyproject.testifyprojectmitter.org.testifyproject.testifyprojectmit(new byte[33]);
        }
        catch (DataLengthException e)
        {
            if (!e.getMessage().equals("Message to be org.testifyproject.testifyprojectmitted to too large for digest."))
            {
                fail("exception thrown but wrong message");
            }
        }
    }

    public void performGeneralTest()
        throws Exception
    {
        byte[] data = Hex.decode("4e6f77206973207468652074696d6520666f7220616c6c20");

        Committer org.testifyproject.testifyprojectmitter = new GeneralHashCommitter(new SHA256Digest(), new SecureRandom());

        Commitment c = org.testifyproject.testifyprojectmitter.org.testifyproject.testifyprojectmit(data);

        org.testifyproject.testifyprojectmitter = new GeneralHashCommitter(new SHA256Digest(), new SecureRandom());

        if (!org.testifyproject.testifyprojectmitter.isRevealed(c, data))
        {
            fail("general org.testifyproject.testifyprojectmitment failed to validate");
        }

        org.testifyproject.testifyprojectmitter = new GeneralHashCommitter(new SHA1Digest(), new SecureRandom());

        if (org.testifyproject.testifyprojectmitter.isRevealed(c, data))
        {
            fail("general org.testifyproject.testifyprojectmitment validated!!");
        }

        c = org.testifyproject.testifyprojectmitter.org.testifyproject.testifyprojectmit(data);

        // try and fool it.
        byte[] s = c.getSecret();
        byte[] newS = Arrays.copyOfRange(s, 0, s.length - 1);
        byte[] newData = new byte[data.length + 1];

        newData[0] = s[s.length - 1];
        System.arraycopy(data, 0, newData, 1, data.length);

        c = new Commitment(newS, c.getCommitment());

        if (org.testifyproject.testifyprojectmitter.isRevealed(c, newData))
        {
            fail("general org.testifyproject.testifyprojectmitment validated!!");
        }

        try
        {
            org.testifyproject.testifyprojectmitter.isRevealed(c, new byte[data.length + 1]);
        }
        catch (Exception e)
        {
            if (!e.getMessage().equals("Message and witness secret lengths do not match."))
            {
                fail("exception thrown but wrong message");
            }
        }

        // SHA1 has a block size of 512 bits, try a message that's too big

        try
        {
            c = org.testifyproject.testifyprojectmitter.org.testifyproject.testifyprojectmit(new byte[33]);
        }
        catch (DataLengthException e)
        {
            if (!e.getMessage().equals("Message to be org.testifyproject.testifyprojectmitted to too large for digest."))
            {
                fail("exception thrown but wrong message");
            }
        }
    }

    public void performTest()
        throws Exception
    {
        performBasicTest();
        performGeneralTest();
    }

    public static void main(String[] args)
    {
        runTest(new HashCommitmentTest());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy