org.rhq.core.domain.util
Class MD5Generator

java.lang.Object
  extended by org.rhq.core.domain.util.MD5Generator

public class MD5Generator
extends Object

An object that can be used to generate an MD5 hashcode (called a "digest") for files, streams or strings. 128 bit fingerprints are computed as described in R. Rivest, The MD5 Message-Digest Algorithm, RFC1321.


Constructor Summary
MD5Generator()
          Creates a new MD5Generator object.
 
Method Summary
 void add(byte[] bytes)
          Use this to add more data to the set of data used to calculate the MD5.
 void add(InputStream is)
          Use this to add more data to the set of data used to calculate the MD5.
 byte[] getDigest()
          After all the data has been added to the message digest via add(InputStream), this method is used to finalize the MD5 calcualation and return the MD5 hashcode.
static byte[] getDigest(File file)
          Calculates the MD5 for a given file.
static byte[] getDigest(InputStream is)
          Returns the MD5 hashcode for the data found in the given stream.
static byte[] getDigest(String source_str)
          Calculates an MD5 for a given string.
 String getDigestString()
          After all the data has been added to the message digest via add(InputStream) or add(byte[]) this method is used to finalize the MD5 calcualation and return the MD5 hashcode as a String.
static String getDigestString(File file)
          Calculates the MD5 for a given file.
static String getDigestString(InputStream is)
          Similar to getDigest(InputStream), only this returns the MD5 as a String.
static String getDigestString(String source_str)
          Calculates an MD5 for a given string and returns the MD5's String representation.
 MessageDigest getMessageDigest()
          Returns the MessageDigest object that is used to compute the MD5.
static void main(String[] args)
          This can be used to generate the MD5 hashcode from the command line.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MD5Generator

public MD5Generator()
Creates a new MD5Generator object.

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

getMessageDigest

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

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 MD5. Once all data has been added, call getDigest() to get the final MD5 value.

Parameters:
is - the stream whose data is to be part of the set of data from which the MD5 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 MD5. Once all data has been added, call getDigest() to get the final MD5 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 MD5 is to be calculated

getDigest

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

Returns:
the bytes of the MD5 hashcode

getDigestString

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

Returns:
the MD5 hashcode as a string

getDigest

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

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

getDigestString

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

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

getDigest

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

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

getDigestString

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

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

getDigest

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

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

getDigestString

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

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

main

public static void main(String[] args)
                 throws Exception
This can be used to generate the MD5 hashcode 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 MD5 for some reason


Copyright © 2008 RHQ Project Advisory Board (Red Hat, Inc. and Hyperic, Inc.). All Rights Reserved.