com.Ostermiller.util
Class MD5

java.lang.Object
  extended by com.Ostermiller.util.MD5

public class MD5
extends java.lang.Object

MD5 hash generator. More information about this class is available from ostermiller.org.

This class takes as input a message of arbitrary length and produces as output a 128-bit "fingerprint" or "message digest" of the input. It is conjectured that it is computationally infeasible to produce two messages having the same message digest, or to produce any message having a given pre-specified target message digest. The MD5 algorithm is intended for digital signature applications, where a large file must be "compressed" in a secure manner before being encrypted with a private (secret) key under a public-key cryptosystem such as RSA.

For more information see RFC1321.

Since:
ostermillerutils 1.00.00
Author:
Santeri Paavolainen http://www.helsinki.fi/~sjpaavol/programs/md5/, Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
See Also:
MD5OutputStream, MD5InputStream

Constructor Summary
MD5()
          Class constructor
 
Method Summary
 byte[] getHash()
          Gets this hash sum as an array of 16 bytes.
static byte[] getHash(byte[] b)
          Gets the MD5 hash of the given byte array.
static byte[] getHash(java.io.File f)
          Gets the MD5 hash of the given file.
static byte[] getHash(java.io.InputStream in)
          Gets the MD5 hash the data on the given InputStream.
static byte[] getHash(java.lang.String s)
          Gets the MD5 hash of the given String.
static byte[] getHash(java.lang.String s, java.lang.String enc)
          Gets the MD5 hash of the given String.
 java.lang.String getHashString()
          Returns 32-character hex representation of this hash.
static java.lang.String getHashString(byte[] b)
          Gets the MD5 hash of the given byte array.
static java.lang.String getHashString(java.io.File f)
          Gets the MD5 hash of the given file.
static java.lang.String getHashString(java.io.InputStream in)
          Gets the MD5 hash the data on the given InputStream.
static java.lang.String getHashString(java.lang.String s)
          Gets the MD5 hash of the given String.
static java.lang.String getHashString(java.lang.String s, java.lang.String enc)
          Gets the MD5 hash of the given String.
static void main(java.lang.String[] args)
          Command line program that will take files as arguments and output the MD5 sum for each file.
 void reset()
          Reset the MD5 sum to its initial state.
 java.lang.String toString()
          Returns 32-character hex representation of this hash.
 void update(byte b)
          Updates this hash with a single byte.
 void update(byte[] buffer)
          Update this hash with the given data.
 void update(byte[] buffer, int length)
          Update this hash with the given data.
 void update(byte[] buffer, int offset, int length)
          Update this hash with the given data.
 void update(java.lang.String s)
          Update this hash with a String.
 void update(java.lang.String s, java.lang.String enc)
          Update this hash with a String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MD5

public MD5()
Class constructor

Since:
ostermillerutils 1.00.00
Method Detail

main

public static void main(java.lang.String[] args)
Command line program that will take files as arguments and output the MD5 sum for each file.

Parameters:
args - command line arguments
Since:
ostermillerutils 1.00.00

getHash

public byte[] getHash()
Gets this hash sum as an array of 16 bytes.

Returns:
Array of 16 bytes, the hash of all updated bytes.
Since:
ostermillerutils 1.00.00

getHashString

public java.lang.String getHashString()
Returns 32-character hex representation of this hash.

Returns:
String representation of this object's hash.
Since:
ostermillerutils 1.00.00

getHash

public static byte[] getHash(byte[] b)
Gets the MD5 hash of the given byte array.

Parameters:
b - byte array for which an MD5 hash is desired.
Returns:
Array of 16 bytes, the hash of all updated bytes.
Since:
ostermillerutils 1.00.00

getHashString

public static java.lang.String getHashString(byte[] b)
Gets the MD5 hash of the given byte array.

Parameters:
b - byte array for which an MD5 hash is desired.
Returns:
32-character hex representation the data's MD5 hash.
Since:
ostermillerutils 1.00.00

getHash

public static byte[] getHash(java.io.InputStream in)
                      throws java.io.IOException
Gets the MD5 hash the data on the given InputStream.

Parameters:
in - byte array for which an MD5 hash is desired.
Returns:
Array of 16 bytes, the hash of all updated bytes.
Throws:
java.io.IOException - if an I/O error occurs.
Since:
ostermillerutils 1.00.00

getHashString

public static java.lang.String getHashString(java.io.InputStream in)
                                      throws java.io.IOException
Gets the MD5 hash the data on the given InputStream.

Parameters:
in - byte array for which an MD5 hash is desired.
Returns:
32-character hex representation the data's MD5 hash.
Throws:
java.io.IOException - if an I/O error occurs.
Since:
ostermillerutils 1.00.00

getHash

public static byte[] getHash(java.io.File f)
                      throws java.io.IOException
Gets the MD5 hash of the given file.

Parameters:
f - file for which an MD5 hash is desired.
Returns:
Array of 16 bytes, the hash of all updated bytes.
Throws:
java.io.IOException - if an I/O error occurs.
Since:
ostermillerutils 1.00.00

getHashString

public static java.lang.String getHashString(java.io.File f)
                                      throws java.io.IOException
Gets the MD5 hash of the given file.

Parameters:
f - file array for which an MD5 hash is desired.
Returns:
32-character hex representation the data's MD5 hash.
Throws:
java.io.IOException - if an I/O error occurs.
Since:
ostermillerutils 1.00.00

getHash

public static byte[] getHash(java.lang.String s)
Gets the MD5 hash of the given String. The string is converted to bytes using the current platform's default character encoding.

Parameters:
s - String for which an MD5 hash is desired.
Returns:
Array of 16 bytes, the hash of all updated bytes.
Since:
ostermillerutils 1.00.00

getHashString

public static java.lang.String getHashString(java.lang.String s)
Gets the MD5 hash of the given String. The string is converted to bytes using the current platform's default character encoding.

Parameters:
s - String for which an MD5 hash is desired.
Returns:
32-character hex representation the data's MD5 hash.
Since:
ostermillerutils 1.00.00

getHash

public static byte[] getHash(java.lang.String s,
                             java.lang.String enc)
                      throws java.io.UnsupportedEncodingException
Gets the MD5 hash of the given String.

Parameters:
s - String for which an MD5 hash is desired.
enc - The name of a supported character encoding.
Returns:
Array of 16 bytes, the hash of all updated bytes.
Throws:
java.io.UnsupportedEncodingException - If the named encoding is not supported.
Since:
ostermillerutils 1.00.00

getHashString

public static java.lang.String getHashString(java.lang.String s,
                                             java.lang.String enc)
                                      throws java.io.UnsupportedEncodingException
Gets the MD5 hash of the given String.

Parameters:
s - String for which an MD5 hash is desired.
enc - The name of a supported character encoding.
Returns:
32-character hex representation the data's MD5 hash.
Throws:
java.io.UnsupportedEncodingException - If the named encoding is not supported.
Since:
ostermillerutils 1.00.00

reset

public void reset()
Reset the MD5 sum to its initial state.

Since:
ostermillerutils 1.00.00

toString

public java.lang.String toString()
Returns 32-character hex representation of this hash.

Overrides:
toString in class java.lang.Object
Returns:
String representation of this object's hash.
Since:
ostermillerutils 1.00.00

update

public void update(byte[] buffer,
                   int offset,
                   int length)
Update this hash with the given data.

If length bytes are not available to be hashed, as many bytes as possible will be hashed.

Parameters:
buffer - Array of bytes to be hashed.
offset - Offset to buffer array.
length - number of bytes to hash.
Since:
ostermillerutils 1.00.00

update

public void update(byte[] buffer,
                   int length)
Update this hash with the given data.

If length bytes are not available to be hashed, as many bytes as possible will be hashed.

Parameters:
buffer - Array of bytes to be hashed.
length - number of bytes to hash.
Since:
ostermillerutils 1.00.00

update

public void update(byte[] buffer)
Update this hash with the given data.

Parameters:
buffer - Array of bytes to be hashed.
Since:
ostermillerutils 1.00.00

update

public void update(byte b)
Updates this hash with a single byte.

Parameters:
b - byte to be hashed.
Since:
ostermillerutils 1.00.00

update

public void update(java.lang.String s)
Update this hash with a String. The string is converted to bytes using the current platform's default character encoding.

Parameters:
s - String to be hashed.
Since:
ostermillerutils 1.00.00

update

public void update(java.lang.String s,
                   java.lang.String enc)
            throws java.io.UnsupportedEncodingException
Update this hash with a String.

Parameters:
s - String to be hashed.
enc - The name of a supported character encoding.
Throws:
java.io.UnsupportedEncodingException - If the named encoding is not supported.
Since:
ostermillerutils 1.00.00


Copyright © 2003 MBARI.

The Monterey Bay Aquarium Research Institute (MBARI) provides this documentation and code "as is", with no warranty, express or implied, of its quality or consistency. It is provided without support and without obligation on the part of MBARI to assist in its use, correction, modification, or enhancement.