org.rhq.core.util
Class MessageDigestGenerator

java.lang.Object
  extended by org.rhq.core.util.MessageDigestGenerator

public class MessageDigestGenerator
extends Object

An object that generates a message digest or hash for algorithms such as MD5 or SHA. This class is basically a wrapper around MessageDigest and provides convenience methods making it easier to generate hashes. There are static methods in here that use MD5 as the default algorithm. If you want to use another algorithm, instantiate an instance of this object and use its instance methods.


Field Summary
static String MD5
          you can pass this to the constructor to indicate you want to generate MD5 message digests
static String SHA_256
          you can pass this to the constructor to indicate you want to generate SHA 256 message digests
 
Constructor Summary
MessageDigestGenerator()
          Creates a new MessageDigestGenerator object using MD5 as the default algorithm.
MessageDigestGenerator(String algorithm)
          Creates a new MessageDigestGenerator using the specified algorithm.
 
Method Summary
 void add(byte[] bytes)
          Use this to add more data to the set of data used to calculate the hash.
 void add(byte[] bytes, int offset, int length)
          Use this to add more data to the set of data used to calculate the hash.
 void add(InputStream is)
          Use this to add more data to the set of data used to calculate the digest.
 byte[] calcDigest(File file)
           
 byte[] calcDigest(InputStream is)
           
 byte[] calcDigest(String source_str)
           
 byte[] calcDigest(URL url)
           
 String calcDigestString(byte[] bytes)
           
 String calcDigestString(File file)
           
 String calcDigestString(InputStream is)
           
 String calcDigestString(String source_str)
           
 String calcDigestString(URL url)
           
 byte[] getDigest()
          After all the data has been added to the message digest via add methods, this method is used to finalize the digest calcualation and return the digest.
static byte[] getDigest(File file)
          Calculates the digest for a given file.
static byte[] getDigest(InputStream is)
          Returns the digest for the data found in the given stream.
static byte[] getDigest(String source_str)
          Calculates a digest for a given string.
 String getDigestString()
          After all the data has been added to the message digest via add methods, this method is used to finalize the digest calcualation and return the digest as a String.
static String getDigestString(File file)
          Calculates the digest for a given file.
static String getDigestString(InputStream is)
          Similar to getDigest(InputStream), only this returns the digest as a String.
static String getDigestString(String source_str)
          Calculates a digest for a given string and returns the digest's String representation.
static String getDigestString(URL url)
          Calculates the digest for a given file.
 MessageDigest getMessageDigest()
          Returns the MessageDigest object that is used to compute the digest.
static void main(String[] args)
          This can be used to generate the digest hash from the command line.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MD5

public static final String MD5
you can pass this to the constructor to indicate you want to generate MD5 message digests

See Also:
Constant Field Values

SHA_256

public static final String SHA_256
you can pass this to the constructor to indicate you want to generate SHA 256 message digests

See Also:
Constant Field Values
Constructor Detail

MessageDigestGenerator

public MessageDigestGenerator()
Creates a new MessageDigestGenerator object using MD5 as the default algorithm.

MD5 is used as the default algorithm for backward compatibility. It originally only supported MD5 and has since been refactored to support algortithms that are supported by your version of Java.

Throws:
IllegalStateException - if the MD5 algorithm cannot be computed by the VM

MessageDigestGenerator

public MessageDigestGenerator(String algorithm)
Creates a new MessageDigestGenerator using the specified algorithm.

Parameters:
algorithm - The algorithm to use (e.g., MD5, SHA-256)
Throws:
IllegalStateException - if the algorithm is not supported by the VM
Method Detail

getMessageDigest

public MessageDigest getMessageDigest()
Returns the MessageDigest object that is used to compute the digest.

Returns:
object that will perform the calculations

add

public void add(InputStream is)
         throws IOException
Use this to add more data to the set of data used to calculate the digest. Once all data has been added, call getDigest() to get the final value.

Parameters:
is - the stream whose data is to be part of the set of data from which the digest is to be calculated
Throws:
IOException - if there was a problem reading from the stream

add

public void add(byte[] bytes)
Use this to add more data to the set of data used to calculate the hash. Once all data has been added, call getDigest() to get the final digest value.

If bytes is null, this method is a no-op and simply returns.

Parameters:
bytes - data to be part of the set of data from which the digest is to be calculated

add

public void add(byte[] bytes,
                int offset,
                int length)
Use this to add more data to the set of data used to calculate the hash. Once all data has been added, call getDigest() to get the final digest value.

If bytes is null, this method is a no-op and simply returns.

Parameters:
bytes - data to be part of the set of data from which the digest is to be calculated
offset - the offset to start from in the array of bytes
length - the number of bytes to use, starting at offset

getDigest

public byte[] getDigest()
After all the data has been added to the message digest via add methods, this method is used to finalize the digest calcualation and return the digest. You can get the String form of this digest if you call getDigestString() instead.

Returns:
the bytes of the digest

getDigestString

public String getDigestString()
After all the data has been added to the message digest via add methods, this method is used to finalize the digest calcualation and return the digest as a String. You can get the actual bytes of the digest if you call getDigest() instead.

Returns:
the digest as a string

getDigest

public static byte[] getDigest(InputStream is)
                        throws IOException
Returns the digest for the data found in the given stream. The digest is returned as a byte array; if you want the digest as a String, call getDigestString methods instead.

Parameters:
is - the stream whose data is to be used to calculate the digest
Returns:
the stream data's hash
Throws:
IOException - if failed to read the stream for some reason

calcDigest

public byte[] calcDigest(InputStream is)
                  throws IOException
Throws:
IOException

getDigestString

public static String getDigestString(InputStream is)
                              throws IOException
Similar to getDigest(InputStream), only this returns the digest as a String.

Parameters:
is - the stream whose data is to be used to calculate the digest
Returns:
the stream data's digest as a String
Throws:
IOException - if failed to read the stream for some reason

calcDigestString

public String calcDigestString(InputStream is)
                        throws IOException
Throws:
IOException

getDigest

public static byte[] getDigest(String source_str)
Calculates a digest for a given string.

Parameters:
source_str - the string whose contents will be used as the data to calculate the digest
Returns:
the string's digest
Throws:
RuntimeException - if a system error occurred - should never really happen

calcDigest

public byte[] calcDigest(String source_str)

getDigestString

public static String getDigestString(String source_str)
Calculates a digest for a given string and returns the digest's String representation.

Parameters:
source_str - the string whose contents will be used as the data to calculate the digest
Returns:
the string's digest or hash as a String

calcDigestString

public String calcDigestString(String source_str)

getDigest

public static byte[] getDigest(File file)
                        throws IOException
Calculates the digest for a given file. The file's contents will be used as the source data for the digest calculation.

Parameters:
file - the file whose contents are to be used to calculate the digest.
Returns:
the file content's digest
Throws:
IOException - if the file could not be read or accessed

calcDigest

public byte[] calcDigest(File file)
                  throws IOException
Throws:
IOException

calcDigest

public byte[] calcDigest(URL url)
                  throws IOException
Throws:
IOException

getDigestString

public static String getDigestString(File file)
                              throws IOException
Calculates the digest for a given file. The file's contents will be used as the source data for the digest calculation.

Parameters:
file - the file whose contents are to be used to calculate the digest.
Returns:
the file content's digest as a String
Throws:
IOException - if the file could not be read or accessed

calcDigestString

public String calcDigestString(File file)
                        throws IOException
Throws:
IOException

calcDigestString

public String calcDigestString(byte[] bytes)
                        throws IOException
Throws:
IOException

getDigestString

public static String getDigestString(URL url)
                              throws IOException
Calculates the digest for a given file. The file's contents will be used as the source data for the digest calculation.

Parameters:
url - the URL whose contents are to be used to calculate the digest.
Returns:
the URL content's digest as a String
Throws:
IOException - if the URL could not be read or accessed

calcDigestString

public String calcDigestString(URL url)
                        throws IOException
Throws:
IOException

main

public static void main(String[] args)
                 throws Exception
This can be used to generate the digest hash from the command line.

Parameters:
args - one and only one filename - may or may not be a .jar file.
Throws:
Exception - if failed to compute the digest for some reason


Copyright © 2008-2012 Red Hat, Inc.. All Rights Reserved.