dk.netarkivet.common.utils
Class MD5

java.lang.Object
  extended by dk.netarkivet.common.utils.MD5

public class MD5
extends java.lang.Object

A class that does MD5 checksumming We don't want everybody to have to do MessageDigest.getInstance() and hex conversion.


Field Summary
(package private) static int MAGIC_INTEGER_4
           
(package private) static int MAGIC_INTEGER_OxOF
           
 
Constructor Summary
MD5()
           
 
Method Summary
static java.lang.String generateMD5(byte[] msg)
          Generate an MD5 for a byte array.
static java.lang.String generateMD5(java.io.InputStream instream)
          Generates an MD5 on an InputStream, throwing away the data itself.
static java.lang.String generateMD5onFile(java.io.File file)
          Generates an MD5 on a given file.
static java.security.MessageDigest getMessageDigestInstance()
          Return na MD5 MessageDigest object.
static java.lang.String toHex(byte[] ba)
          Converts a byte array to a hexstring.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAGIC_INTEGER_4

static final int MAGIC_INTEGER_4
See Also:
Constant Field Values

MAGIC_INTEGER_OxOF

static final int MAGIC_INTEGER_OxOF
See Also:
Constant Field Values
Constructor Detail

MD5

public MD5()
Method Detail

getMessageDigestInstance

public static java.security.MessageDigest getMessageDigestInstance()
Return na MD5 MessageDigest object.

Returns:
a MessageDigest object

generateMD5

public static java.lang.String generateMD5(byte[] msg)
Generate an MD5 for a byte array.

Parameters:
msg - The given bytearray
Returns:
the MD5 for a byte array

generateMD5onFile

public static java.lang.String generateMD5onFile(java.io.File file)
                                          throws java.io.IOException,
                                                 java.io.FileNotFoundException
Generates an MD5 on a given file. Reads the entire file into a byte array - inefficient. Very inefficient if file is retrieved over network.

Parameters:
file - Unique reference to file for which to generate checksum
Returns:
The generated MD5 checksum as string.
Throws:
java.io.IOException
java.io.FileNotFoundException

generateMD5

public static java.lang.String generateMD5(java.io.InputStream instream)
Generates an MD5 on an InputStream, throwing away the data itself.

Parameters:
instream - An inputstream to generate MD5 on. The contents of the stream will be consumed by this call, but the stream will not be closed.
Returns:
The generated MD5 checksum as a string.
Throws:
IOFailure - if there is an error reading from the stream

toHex

public static java.lang.String toHex(byte[] ba)
Converts a byte array to a hexstring.

Parameters:
ba - the bytearray to be converted
Returns:
ba converted to a hexstring