Class MessageDigester

java.lang.Object
org.deltava.crypt.MessageDigester

public class MessageDigester extends Object
A class to generate MD5/SHA message digests.
Since:
1.0
Version:
11.1
Author:
Luke
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new Message Digester with a specified algorithm.
    MessageDigester(String algorithm, int bufSize)
    Create a new Message Digester with a specified algorithm and input buffer size.
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    convert(byte[] hash)
    Converts a message digest into a hexadecimal String.
    byte[]
    digest(byte[] data)
    Returns the message digest value for a given amount of data.
    byte[]
    Returns the message digest value for the data within a given stream.
    final String
    Return the Message Digest algorithm in use.
    static byte[]
    parse(String hash)
    Converts a hexadecimal String into a byte array.
    void
    Resets the Message Digester.
    void
    salt(String saltValue)
    "Salts" the message digest by processing a string before the actual data.
    final void
    setBufferSize(int size)
    Updates the buffer size.

    Methods inherited from class java.lang.Object

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

    • MessageDigester

      public MessageDigester(String algorithm) throws CryptoException
      Create a new Message Digester with a specified algorithm.
      Parameters:
      algorithm - the algorithm to use; must be supported by the JVM
      Throws:
      CryptoException
    • MessageDigester

      public MessageDigester(String algorithm, int bufSize)
      Create a new Message Digester with a specified algorithm and input buffer size.
      Parameters:
      algorithm - the algorithm to use; must be supported by the JVM
      bufSize - the buffer Size in bytes.
      See Also:
  • Method Details

    • salt

      public void salt(String saltValue)
      "Salts" the message digest by processing a string before the actual data. This is used to protect (somewhat) against reverse engineering the hash through brute force.
      Parameters:
      saltValue - the salt string
      Throws:
      NullPointerException - if saltValue is null
    • getAlgorithm

      public final String getAlgorithm()
      Return the Message Digest algorithm in use.
      Returns:
      the algorithm name
    • setBufferSize

      public final void setBufferSize(int size)
      Updates the buffer size.
      Parameters:
      size - the new buffer size in bytes. Must be > 64
    • digest

      public byte[] digest(byte[] data)
      Returns the message digest value for a given amount of data.
      Parameters:
      data - the data to digest
      Returns:
      the message digest
    • digest

      public byte[] digest(InputStream is) throws IOException
      Returns the message digest value for the data within a given stream.
      Parameters:
      is - the stream containing the data
      Returns:
      the message digest
      Throws:
      IOException - if an error occurs reading the data
    • reset

      public void reset()
      Resets the Message Digester.
    • convert

      public static String convert(byte[] hash)
      Converts a message digest into a hexadecimal String.
      Parameters:
      hash - the message digest
      Returns:
      the hexadecimal representation of the message digest
    • parse

      public static byte[] parse(String hash)
      Converts a hexadecimal String into a byte array.
      Parameters:
      hash - the hexadecimal message digest
      Returns:
      the message digest