Search in sources :

Example 6 with CryptoSuite

use of org.hyperledger.fabric.sdk.security.CryptoSuite in project fabric-sdk-java by hyperledger.

the class SDKUtils method calculateBlockHash.

/**
 * used asn1 and get hash
 *
 * @param blockNumber
 * @param previousHash
 * @param dataHash
 * @return byte[]
 * @throws IOException
 * @throws InvalidArgumentException
 */
public static byte[] calculateBlockHash(HFClient client, long blockNumber, byte[] previousHash, byte[] dataHash) throws IOException, InvalidArgumentException {
    if (previousHash == null) {
        throw new InvalidArgumentException("previousHash parameter is null.");
    }
    if (dataHash == null) {
        throw new InvalidArgumentException("dataHash parameter is null.");
    }
    if (null == client) {
        throw new InvalidArgumentException("client parameter is null.");
    }
    CryptoSuite cryptoSuite = client.getCryptoSuite();
    if (null == client) {
        throw new InvalidArgumentException("Client crypto suite has not  been set.");
    }
    ByteArrayOutputStream s = new ByteArrayOutputStream();
    DERSequenceGenerator seq = new DERSequenceGenerator(s);
    seq.addObject(new ASN1Integer(blockNumber));
    seq.addObject(new DEROctetString(previousHash));
    seq.addObject(new DEROctetString(dataHash));
    seq.close();
    return cryptoSuite.hash(s.toByteArray());
}
Also used : InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) CryptoSuite(org.hyperledger.fabric.sdk.security.CryptoSuite) DERSequenceGenerator(org.bouncycastle.asn1.DERSequenceGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) DEROctetString(org.bouncycastle.asn1.DEROctetString)

Aggregations

CryptoSuite (org.hyperledger.fabric.sdk.security.CryptoSuite)6 Test (org.junit.Test)4 Channel (org.hyperledger.fabric.sdk.Channel)2 User (org.hyperledger.fabric.sdk.User)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)1 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)1 DEROctetString (org.bouncycastle.asn1.DEROctetString)1 DERSequenceGenerator (org.bouncycastle.asn1.DERSequenceGenerator)1 InvalidArgumentException (org.hyperledger.fabric.sdk.exception.InvalidArgumentException)1 HFCAClient (org.hyperledger.fabric_ca.sdk.HFCAClient)1 MockHFCAClient (org.hyperledger.fabric_ca.sdk.MockHFCAClient)1