com.fitbur.bouncycastle.crypto.test.HashCommitmentTest Maven / Gradle / Ivy
package com.fitbur.bouncycastle.crypto.test;
import java.security.SecureRandom;
import com.fitbur.bouncycastle.crypto.Commitment;
import com.fitbur.bouncycastle.crypto.Committer;
import com.fitbur.bouncycastle.crypto.DataLengthException;
import com.fitbur.bouncycastle.crypto.com.fitburmitments.GeneralHashCommitter;
import com.fitbur.bouncycastle.crypto.com.fitburmitments.HashCommitter;
import com.fitbur.bouncycastle.crypto.digests.SHA1Digest;
import com.fitbur.bouncycastle.crypto.digests.SHA256Digest;
import com.fitbur.bouncycastle.util.Arrays;
import com.fitbur.bouncycastle.util.encoders.Hex;
import com.fitbur.bouncycastle.util.test.SimpleTest;
public class HashCommitmentTest
extends SimpleTest
{
public String getName()
{
return "HashCommitmentTest";
}
public void performBasicTest()
throws Exception
{
byte[] data = Hex.com.fitburcode("4e6f77206973207468652074696d6520666f7220616c6c20");
Committer com.fitburmitter = new HashCommitter(new SHA256Digest(), new SecureRandom());
Commitment c = com.fitburmitter.com.fitburmit(data);
com.fitburmitter = new HashCommitter(new SHA256Digest(), new SecureRandom());
if (!com.fitburmitter.isRevealed(c, data))
{
fail("com.fitburmitment failed to validate");
}
com.fitburmitter = new HashCommitter(new SHA1Digest(), new SecureRandom());
if (com.fitburmitter.isRevealed(c, data))
{
fail("com.fitburmitment validated!!");
}
try
{
com.fitburmitter.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 = com.fitburmitter.com.fitburmit(new byte[33]);
}
catch (DataLengthException e)
{
if (!e.getMessage().equals("Message to be com.fitburmitted to too large for digest."))
{
fail("exception thrown but wrong message");
}
}
}
public void performGeneralTest()
throws Exception
{
byte[] data = Hex.com.fitburcode("4e6f77206973207468652074696d6520666f7220616c6c20");
Committer com.fitburmitter = new GeneralHashCommitter(new SHA256Digest(), new SecureRandom());
Commitment c = com.fitburmitter.com.fitburmit(data);
com.fitburmitter = new GeneralHashCommitter(new SHA256Digest(), new SecureRandom());
if (!com.fitburmitter.isRevealed(c, data))
{
fail("general com.fitburmitment failed to validate");
}
com.fitburmitter = new GeneralHashCommitter(new SHA1Digest(), new SecureRandom());
if (com.fitburmitter.isRevealed(c, data))
{
fail("general com.fitburmitment validated!!");
}
c = com.fitburmitter.com.fitburmit(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 (com.fitburmitter.isRevealed(c, newData))
{
fail("general com.fitburmitment validated!!");
}
try
{
com.fitburmitter.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 = com.fitburmitter.com.fitburmit(new byte[33]);
}
catch (DataLengthException e)
{
if (!e.getMessage().equals("Message to be com.fitburmitted 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