Example usage for org.bouncycastle.crypto.commitments HashCommitter isRevealed

List of usage examples for org.bouncycastle.crypto.commitments HashCommitter isRevealed

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.commitments HashCommitter isRevealed.

Prototype

public boolean isRevealed(Commitment commitment, byte[] message) 

Source Link

Document

Return true if the passed in commitment represents a commitment to the passed in message.

Usage

From source file:com.vvote.verifierlibrary.utils.crypto.CryptoUtils.java

License:Open Source License

/**
 * Perform a verification on a hash commitment using bouncy castle. This
 * implementation uses the HashCommitter from bouncy castle and the
 * isRevealed function/*w ww  .j  av  a2 s .c  o  m*/
 * 
 * @param commitment
 * @param witness
 * @param randomValue
 * @return whether the commitment check is successful
 * @throws CommitException
 */
private static boolean bouncyCastleVerifyHashCommitment(byte[] commitment, byte[] witness, byte[] randomValue)
        throws CommitException {

    logger.debug("Verifying hash commitment using Bouncy castle implementation");

    // initialise a hash committer
    HashCommitter hashCommitter = new HashCommitter(new SHA256Digest(), new SecureRandom(witness));

    MessageDigest md = null;

    try {
        logger.debug("Initialising message digest");
        // initialise the message digest
        md = MessageDigest.getInstance(CryptoConstants.Commitments.COMMITMENT_HASH_ALGORITHM);

        // ensure the random value is the correct length
        if (randomValue.length > CryptoConstants.Commitments.RANDOM_VALUE_MAXIMUM_LENGTH) {
            logger.debug("Hashing random value to the correct length");
            md.reset();
            randomValue = md.digest(randomValue);
        }

        // initialise a new Commitment
        Commitment comm = new Commitment(witness, commitment);

        // check whether the given random value opens the commitment
        if (!hashCommitter.isRevealed(comm, randomValue)) {
            logger.error("Bouncy castle hash commitment verification failed");
            return false;
        }

    } catch (NoSuchAlgorithmException e) {
        logger.error("Could not initialise the message digest with the specified algorithm: {}",
                CryptoConstants.Commitments.COMMITMENT_HASH_ALGORITHM, e);
        throw new CommitException("Could not initialise the message digest with the specified algorithm: "
                + CryptoConstants.Commitments.COMMITMENT_HASH_ALGORITHM, e);
    }

    return true;
}