Class HashCalculator

java.lang.Object
com.pnfsoftware.jeb.util.encoding.HashCalculator

public class HashCalculator extends Object
Memory and CPU efficient way to compute message digests.

This class supersedes Hash.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Compute an Adler-32 checksum.
    static final int
    Compute a CRC-32 checksum.
    static final int
    Compute an MD5 digest.
    static final int
    Compute a SHA-1 digest.
    static final int
    Compute a SHA-256 digest.
  • Constructor Summary

    Constructors
    Constructor
    Description
    HashCalculator(InputStream in, int flags)
    Create a calculator for the requested checksum and digest algorithms.
  • Method Summary

    Modifier and Type
    Method
    Description
    static final Long
    Compute an Adler-32 checksum for an input stream.
    boolean
    Compute the requested digests and stream size.
    static final Long
    Compute a CRC-32 checksum for an input stream.
    long
    Get the computed Adler-32 checksum.
    long
    Get the computed CRC-32 checksum.
    byte[]
    Get the computed MD5 digest.
    byte[]
    Get the computed SHA-1 digest.
    byte[]
    Get the computed SHA-256 digest.
    long
    Get the number of bytes consumed during computation.
    static final byte[]
    Compute an MD5 digest for an input stream.
    static final byte[]
    Compute a SHA-1 digest for an input stream.
    static final byte[]
    Compute a SHA-256 digest for an input stream.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • HashCalculator

      public HashCalculator(InputStream in, int flags)
      Create a calculator for the requested checksum and digest algorithms.
      Parameters:
      in - input stream to consume during compute()
      flags - bit mask composed of CRC32, ADLER32, MD5, SHA1, and SHA256
  • Method Details

    • crc32

      public static final Long crc32(InputStream in)
      Compute a CRC-32 checksum for an input stream.
      Parameters:
      in - input stream to consume
      Returns:
      checksum value, or null if reading failed
    • adler32

      public static final Long adler32(InputStream in)
      Compute an Adler-32 checksum for an input stream.
      Parameters:
      in - input stream to consume
      Returns:
      checksum value, or null if reading failed
    • md5

      public static final byte[] md5(InputStream in)
      Compute an MD5 digest for an input stream.
      Parameters:
      in - input stream to consume
      Returns:
      digest bytes, or null if reading failed or the digest is unavailable
    • sha1

      public static final byte[] sha1(InputStream in)
      Compute a SHA-1 digest for an input stream.
      Parameters:
      in - input stream to consume
      Returns:
      digest bytes, or null if reading failed or the digest is unavailable
    • sha256

      public static final byte[] sha256(InputStream in)
      Compute a SHA-256 digest for an input stream.
      Parameters:
      in - input stream to consume
      Returns:
      digest bytes, or null if reading failed or the digest is unavailable
    • getSize

      public long getSize()
      Get the number of bytes consumed during computation.
      Returns:
      stream size in bytes
    • getCrc32

      public long getCrc32()
      Get the computed CRC-32 checksum.
      Returns:
      CRC-32 value
    • getAdler32

      public long getAdler32()
      Get the computed Adler-32 checksum.
      Returns:
      Adler-32 value
    • getMd5

      public byte[] getMd5()
      Get the computed MD5 digest.
      Returns:
      digest bytes
    • getSha1

      public byte[] getSha1()
      Get the computed SHA-1 digest.
      Returns:
      digest bytes
    • getSha256

      public byte[] getSha256()
      Get the computed SHA-256 digest.
      Returns:
      digest bytes
    • compute

      public boolean compute() throws IOException
      Compute the requested digests and stream size.
      Returns:
      true on success, false if one of the requested digest algorithm is not available
      Throws:
      IOException - on stream IO error