Class V2Util

java.lang.Object
org.klomp.snark.v2.V2Util

public class V2Util extends Object
Merkle utilities
Since:
0.9.71
  • Field Details

  • Constructor Details

    • V2Util

      public V2Util()
  • Method Details

    • isValidPieceLength

      public static boolean isValidPieceLength(int plen)
      16K min and power of two Storage.MAX_PIECE_SIZE is checked elsewhere but do it here also.
    • rectify

      public static int rectify(long flen)
      Parameters:
      flen - the file length
      Returns:
      next power of two of flen, or 0 if flen is 0
    • rectify

      public static int rectify(long flen, int plen)
      Parameters:
      flen - the file length
      plen - piece length, 16K minimum, power of two, or zero
      Returns:
      plen, or next power of two of flen if smaller, or 0 if flen is 0
    • checkMerkle

      public static boolean checkMerkle(MerkleHash root, List<MerkleHash> pieces, int plen)
      Parameters:
      pieces - two minimum
      plen - piece length, 16K minimum, power of two
    • calculateMerkleRoot

      public static MerkleHash calculateMerkleRoot(List<MerkleHash> pieces, int plen)
      Parameters:
      pieces - two minimum
      plen - piece length, 16K minimum, power of two
    • calculateMerkleRoot

      public static MerkleHash calculateMerkleRoot(File f) throws IOException
      Returns:
      the root hash
      Throws:
      IOException
    • calculateMerkleRoot

      public static MerkleHash calculateMerkleRoot(byte[] b, int plen) throws IOException
      Parameters:
      plen - piece length, for pieces, MUST be the full padded piece length
      Returns:
      the root hash
      Throws:
      IOException
    • calculateMerkleRoot

      public static MerkleHash calculateMerkleRoot(InputStream in, int plen) throws IOException
      Warn, will return bad value for files if plen is greater than the next power of two of the input size
      Parameters:
      plen - piece length, for files, MUST be rectified to next power of two with rectify(); for pieces, MUST be the full padded piece length
      Returns:
      the root hash
      Throws:
      IOException
    • calculateMerklePieces

      public static List<MerkleHash> calculateMerklePieces(File f, int plen) throws IOException
      Parameters:
      plen - will use only if smaller than file size
      Returns:
      the piece hashes
      Throws:
      IOException
    • calculateMerklePieces

      public static List<MerkleHash> calculateMerklePieces(InputStream in, int plen) throws IOException
      Warn, will return bad value for files if plen is greater than the next power of two of the input size
      Parameters:
      plen - piece length, for files, MUST be rectified to next power of two with rectify(); for pieces, MUST be the full padded piece length
      Returns:
      the piece hashes
      Throws:
      IOException
    • main

      public static void main(String[] args) throws Exception
      Throws:
      Exception