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());
}
Aggregations