hyperledger fabric fabric-sdk-java 计算当前区块的blockhash(6)

    public  static String caculateCurrentBlockhash(BlockInfo blockInfo) throws IOException, IllegalAccessException, InvocationTargetException, InvalidArgumentException, InstantiationException, NoSuchMethodException, CryptoException, ClassNotFoundException {
        CryptoSuite cryptoSuite = CryptoSuite.Factory.getCryptoSuite();
        ByteArrayOutputStream s = new ByteArrayOutputStream();
        DERSequenceGenerator seq = new DERSequenceGenerator(s);
        seq.addObject(new ASN1Integer(blockInfo.getBlockNumber()));
        seq.addObject(new DEROctetString(blockInfo.getPreviousHash()));
        seq.addObject(new DEROctetString(blockInfo.getDataHash()));
        seq.close();
        byte[] hash = cryptoSuite.hash(s.toByteArray());
        return Hex.encodeHexString(hash);
    }

fabric区块中只包含了前一区块hash和datahash, 并没有当前区块hash , 不过查看fabric源码就可以仿照写出计算hash的代码, 上面是我仿照官方代码写的一段自己的计算hash工具, 

觉得还可以的话, 顺手点个赞吧

猜你喜欢

转载自blog.csdn.net/qq_27348837/article/details/107201872