Class SalmonIntegrity

java.lang.Object
com.mku.salmon.integrity.SalmonIntegrity

public class SalmonIntegrity extends 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 final int
    Default chunk size for integrity.
    static final int
    Maximum chunk size for data integrity.
  • Constructor Summary

    Constructors
    Constructor
    Description
    SalmonIntegrity(boolean integrity, byte[] key, Integer 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

    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[] includeHeaderData)
    Generate a hash signatures for each data chunk.
    int
    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[]
    Get the hash key.
    static long
    getTotalHashDataLength(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
    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 Details

    • MAX_CHUNK_SIZE

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

      public static final int DEFAULT_CHUNK_SIZE
      Default chunk size for integrity.
      See Also:
  • Constructor Details

    • SalmonIntegrity

      public SalmonIntegrity(boolean integrity, byte[] key, Integer 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 checks.
      key - The key to use for hashing.
      chunkSize - The chunk size. Use 0 to enable integrity on the whole file (1 chunk). 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.
      SalmonSecurityException - When security has failed
  • Method Details

    • 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(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:
      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[] includeHeaderData)
      Generate a hash signatures for each data chunk.
      Parameters:
      buffer - The buffer containing the data chunks.
      includeHeaderData - Include the header data in the first chunk.
      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.