Class Integrity


  • public class Integrity
    extends java.lang.Object
    Provide operations for calculating, storing, and verifying data integrity. This class operates on chunks of byte arrays calculating hashes for each one.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_CHUNK_SIZE
      Default chunk size for integrity.
      static int MAX_CHUNK_SIZE
      Maximum chunk size for data integrity.
    • Constructor Summary

      Constructors 
      Constructor Description
      Integrity​(boolean integrity, byte[] key, int chunkSize, IHashProvider provider, int hashSize)
      Instantiate an object to be used for applying and verifying hash signatures for each of the data chunks.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static byte[] calculateHash​(IHashProvider provider, byte[] buffer, int offset, int count, byte[] key, byte[] includeData)
      Calculate hash of the data provided.
      byte[][] generateHashes​(byte[] buffer, byte[] includeData)
      Generate a hash signatures for each data chunk.
      int getChunkSize()
      Get the chunk size.
      long getHashDataLength​(long count, int hashOffset)
      Return the number of bytes that all hash signatures occupy for each chunk size
      byte[][] getHashes​(byte[] buffer)
      Get the hashes for each data chunk.
      byte[] getKey()
      Get the hash key.
      static long getTotalHashDataLength​(EncryptionMode mode, long length, int chunkSize, int hashOffset, int hashLength)
      Get the total number of bytes for all hash signatures for data of a specific length.
      boolean useIntegrity()
      Get the integrity enabled option.
      void verifyHashes​(byte[][] hashes, byte[] buffer, byte[] includeHeaderData)
      Verify the buffer chunks against the hash signatures.
      • Methods inherited from class java.lang.Object

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

      • MAX_CHUNK_SIZE

        public static final int MAX_CHUNK_SIZE
        Maximum chunk size for data integrity.
        See Also:
        Constant Field Values
      • DEFAULT_CHUNK_SIZE

        public static final int DEFAULT_CHUNK_SIZE
        Default chunk size for integrity.
        See Also:
        Constant Field Values
    • Constructor Detail

      • Integrity

        public Integrity​(boolean integrity,
                         byte[] key,
                         int chunkSize,
                         IHashProvider provider,
                         int hashSize)
        Instantiate an object to be used for applying and verifying hash signatures for each of the data chunks.
        Parameters:
        integrity - True to enable integrity check.
        key - The key to use for hashing.
        chunkSize - The chunk size. Use 0 to use default. Use a positive number to specify integrity chunks.
        provider - Hash implementation provider.
        hashSize - The hash size.
        Throws:
        IntegrityException - Thrown if the data are corrupt or tampered with.
        SecurityException - Thrown when security has failed
    • Method Detail

      • calculateHash

        public static byte[] calculateHash​(IHashProvider provider,
                                           byte[] buffer,
                                           int offset,
                                           int count,
                                           byte[] key,
                                           byte[] includeData)
        Calculate hash of the data provided.
        Parameters:
        provider - Hash implementation provider.
        buffer - Data to calculate the hash.
        offset - Offset of the buffer that the hashing calculation will start from
        count - Length of the buffer that will be used to calculate the hash.
        key - Key that will be used
        includeData - Additional data to be included in the calculation.
        Returns:
        The hash.
        Throws:
        IntegrityException - Thrown if the data are corrupt or tampered with.
      • getTotalHashDataLength

        public static long getTotalHashDataLength​(EncryptionMode mode,
                                                  long length,
                                                  int chunkSize,
                                                  int hashOffset,
                                                  int hashLength)
        Get the total number of bytes for all hash signatures for data of a specific length.
        Parameters:
        mode - The encryption mode
        length - The length of the data.
        chunkSize - The byte size of the stream chunk that will be used to calculate the hash. The length should be fixed value except for the last chunk which might be lesser since we don't use padding
        hashOffset - The hash key length that will be used as an offset.
        hashLength - The hash length.
        Returns:
        The total hash data length
      • getHashDataLength

        public long getHashDataLength​(long count,
                                      int hashOffset)
        Return the number of bytes that all hash signatures occupy for each chunk size
        Parameters:
        count - Actual length of the real data int the base stream including header and hash signatures.
        hashOffset - The hash key length
        Returns:
        The number of bytes all hash signatures occupy
      • getChunkSize

        public int getChunkSize()
        Get the chunk size.
        Returns:
        The chunk size.
      • getKey

        public byte[] getKey()
        Get the hash key.
        Returns:
        The hash key.
      • useIntegrity

        public boolean useIntegrity()
        Get the integrity enabled option.
        Returns:
        True if integrity is enabled.
      • generateHashes

        public byte[][] generateHashes​(byte[] buffer,
                                       byte[] includeData)
        Generate a hash signatures for each data chunk.
        Parameters:
        buffer - The buffer containing the data chunks.
        includeData - Additional data to be included in the hash generation.
        Returns:
        The hash signatures.
        Throws:
        IntegrityException - Thrown if the data are corrupt or tampered with.
      • getHashes

        public byte[][] getHashes​(byte[] buffer)
        Get the hashes for each data chunk.
        Parameters:
        buffer - The buffer that contains the data chunks.
        Returns:
        The hash signatures.
      • verifyHashes

        public void verifyHashes​(byte[][] hashes,
                                 byte[] buffer,
                                 byte[] includeHeaderData)
        Verify the buffer chunks against the hash signatures.
        Parameters:
        hashes - The hashes to verify.
        buffer - The buffer that contains the chunks to verify the hashes.
        includeHeaderData - The header data to include
        Throws:
        IntegrityException - Thrown if the data are corrupt or tampered with.