Example usage for org.bouncycastle.crypto.digests SHA256Digest getEncodedState

List of usage examples for org.bouncycastle.crypto.digests SHA256Digest getEncodedState

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.digests SHA256Digest getEncodedState.

Prototype

public byte[] getEncodedState() 

Source Link

Usage

From source file:co.rsk.mine.MinerServerImpl.java

License:Open Source License

public static byte[] compressCoinbase(byte[] bitcoinMergedMiningCoinbaseTransactionSerialized,
        boolean lastOccurrence) {
    List<Byte> coinBaseTransactionSerializedAsList = java.util.Arrays
            .asList(ArrayUtils.toObject(bitcoinMergedMiningCoinbaseTransactionSerialized));
    List<Byte> tagAsList = java.util.Arrays.asList(ArrayUtils.toObject(RskMiningConstants.RSK_TAG));

    int rskTagPosition;
    if (lastOccurrence) {
        rskTagPosition = Collections.lastIndexOfSubList(coinBaseTransactionSerializedAsList, tagAsList);
    } else {//from  w  ww  .ja v  a  2 s  .  c  o  m
        rskTagPosition = Collections.indexOfSubList(coinBaseTransactionSerializedAsList, tagAsList);
    }

    int remainingByteCount = bitcoinMergedMiningCoinbaseTransactionSerialized.length - rskTagPosition
            - RskMiningConstants.RSK_TAG.length - RskMiningConstants.BLOCK_HEADER_HASH_SIZE;
    if (remainingByteCount > RskMiningConstants.MAX_BYTES_AFTER_MERGED_MINING_HASH) {
        throw new IllegalArgumentException("More than 128 bytes after RSK tag");
    }
    int sha256Blocks = rskTagPosition / 64;
    int bytesToHash = sha256Blocks * 64;
    SHA256Digest digest = new SHA256Digest();
    digest.update(bitcoinMergedMiningCoinbaseTransactionSerialized, 0, bytesToHash);
    byte[] hashedContent = digest.getEncodedState();
    byte[] trimmedHashedContent = new byte[RskMiningConstants.MIDSTATE_SIZE_TRIMMED];
    System.arraycopy(hashedContent, 8, trimmedHashedContent, 0, RskMiningConstants.MIDSTATE_SIZE_TRIMMED);
    byte[] unHashedContent = new byte[bitcoinMergedMiningCoinbaseTransactionSerialized.length - bytesToHash];
    System.arraycopy(bitcoinMergedMiningCoinbaseTransactionSerialized, bytesToHash, unHashedContent, 0,
            unHashedContent.length);
    return Arrays.concatenate(trimmedHashedContent, unHashedContent);
}