Final

java.security
Class MessageDigest

java.lang.Object
  |
  +--java.security.MessageDigestSpi
        |
        +--java.security.MessageDigest

public abstract class MessageDigest
extends MessageDigestSpi

This class provides the functionality of a digest algorithm, like MD5 or SHA. This class is is abstract and extends from MessageDigestSpi to be compatible with JDK, which does it for historical reasons (check the JDK's Javadoc files).


Constructor Summary
protected MessageDigest(String algorithmName)
          Create a new MessageDigest with its algorithm set to the argument.
 
Method Summary
 Object clone()
          Answers a new instance of the same class as the receiver, whose slots have been filled in with the values in the slots of the receiver.
 byte[] digest()
          Computes and answers the final hash value that the receiver represents.
 byte[] digest(byte[] bytesToHash)
          Includes the bytes of the argument in the hash value computed by the receiver, and then computes the final digest value.
 int digest(byte[] buffer, int offset, int length)
          Computes the digest and stores it into the buffer passed as parameter.
 String getAlgorithm()
          Answers the standard Java Security name for the algorithm being used by the receiver.
 int getDigestLength()
          Return the engine digest length in bytes.
static MessageDigest getInstance(String algorithmName)
          Answers a new MessageDigest which is capable of running the algorithm described by the argument.
static MessageDigest getInstance(String algorithmName, Provider provider)
          Answers a new MessageDigest which is capable of running the algorithm described by the argument.
static MessageDigest getInstance(String algorithmName, String providerName)
          Answers a new MessageDigest which is capable of running the algorithm described by the argument.
 Provider getProvider()
          Returns the Provider of the digest represented by the receiver.
static boolean isEqual(byte[] digesta, byte[] digestb)
          Does a simply byte-per-byte compare of the two digests.
 void reset()
          Puts the receiver back in an initial state, such that it is ready to compute a new hash.
 String toString()
          Answers a string containing a concise, human-readable description of the receiver.
 void update(byte byteToHash)
          Includes the argument in the hash value computed by the receiver.
 void update(byte[] bytesToHash)
          Includes the bytes of the argument in the hash value computed by the receiver.
 void update(byte[] bytesToHash, int offset, int count)
          Includes a range of bytes from the first argument in the hash value computed by the receiver.
 
Methods inherited from class java.security.MessageDigestSpi
engineDigest, engineDigest, engineGetDigestLength, engineReset, engineUpdate, engineUpdate
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MessageDigest

protected MessageDigest(String algorithmName)
Create a new MessageDigest with its algorithm set to the argument.

Parameters:
algorithmName - java.lang.String the algorithm that the receiver will represent
Method Detail

clone

public Object clone()
             throws CloneNotSupportedException
Answers a new instance of the same class as the receiver, whose slots have been filled in with the values in the slots of the receiver.

Classes which wish to support cloning must specify that they implement the Cloneable interface, since the native implementation checks for this.

Overrides:
clone in class MessageDigestSpi
Returns:
a shallow copy of this object
Throws:
CloneNotSupportedException - if the component does not implement the interface Cloneable

digest

public byte[] digest()
Computes and answers the final hash value that the receiver represents. After the digest is computed the receiver is reset.

Returns:
the hash the receiver computed
See Also:
reset()

digest

public byte[] digest(byte[] bytesToHash)
Includes the bytes of the argument in the hash value computed by the receiver, and then computes the final digest value.

Parameters:
bytesToHash - byte[] the source array
Returns:
the hash the receiver computed
See Also:
update(byte), update(byte[]), update(byte[], int, int), digest(), digest(byte[]), digest(byte[], int, int)

digest

public int digest(byte[] buffer,
                  int offset,
                  int length)
           throws DigestException
Computes the digest and stores it into the buffer passed as parameter.

Parameters:
buffer - byte[] the array into which to store the digest
offset - the starting offset into the array
length - the number of bytes available to store the digest
Returns:
the number of bytes copied
Throws:
DigestException - If an error occurs
See Also:
digest(), digest(byte[]), digest(byte[], int, int)

getAlgorithm

public final String getAlgorithm()
Answers the standard Java Security name for the algorithm being used by the receiver.

Returns:
String the name of the algorithm

getDigestLength

public final int getDigestLength()
Return the engine digest length in bytes. Default is 0.

Returns:
int the engine digest length in bytes

getInstance

public static MessageDigest getInstance(String algorithmName)
                                 throws NoSuchAlgorithmException
Answers a new MessageDigest which is capable of running the algorithm described by the argument. The result will be an instance of a subclass of MessageDigest which implements that algorithm.

Parameters:
algorithmName - java.lang.String Name of the algorithm desired
Returns:
MessageDigest a concrete implementation for the algorithm desired.
Throws:
NoSuchAlgorithmException - If the algorithm cannot be found

getInstance

public static MessageDigest getInstance(String algorithmName,
                                        String providerName)
                                 throws NoSuchAlgorithmException,
                                        NoSuchProviderException
Answers a new MessageDigest which is capable of running the algorithm described by the argument. The result will be an instance of a subclass of MessageDigest which implements that algorithm.

Parameters:
algorithmName - java.lang.String Name of the algorithm desired
providerName - java.lang.String Name of the provider which has to implement the algorithm
Returns:
MessageDigest a concrete implementation for the algorithm desired.
Throws:
NoSuchAlgorithmException - If the algorithm cannot be found
NoSuchProviderException - If the provider cannot be found

getInstance

public static MessageDigest getInstance(String algorithmName,
                                        Provider provider)
                                 throws NoSuchAlgorithmException
Answers a new MessageDigest which is capable of running the algorithm described by the argument. The result will be an instance of a subclass of MessageDigest which implements that algorithm.

Parameters:
provider - Provider Provider which has to implement the algorithm
Returns:
MessageDigest a concrete implementation for the algorithm desired.
Throws:
NoSuchAlgorithmException - If the algorithm cannot be found

getProvider

public final Provider getProvider()
Returns the Provider of the digest represented by the receiver.

Returns:
Provider an instance of a subclass of java.security.Provider

isEqual

public static boolean isEqual(byte[] digesta,
                              byte[] digestb)
Does a simply byte-per-byte compare of the two digests.

Parameters:
digesta - One of the digests to compare
digestb - The digest to compare to
Returns:
true if the two hashes are equal false if the two hashes are not equal

reset

public void reset()
Puts the receiver back in an initial state, such that it is ready to compute a new hash.

See Also:
MessageDigest.Wrapper.engineReset()

toString

public String toString()
Answers a string containing a concise, human-readable description of the receiver.

Overrides:
toString in class Object
Returns:
a printable representation for the receiver.

update

public void update(byte[] bytesToHash)
Includes the bytes of the argument in the hash value computed by the receiver.

Parameters:
bytesToHash - byte[] the source array

update

public void update(byte[] bytesToHash,
                   int offset,
                   int count)
Includes a range of bytes from the first argument in the hash value computed by the receiver.

Parameters:
bytesToHash - byte[] the source array
offset - the starting offset into the array
count - the number of bytes to include in the hash

update

public void update(byte byteToHash)
Includes the argument in the hash value computed by the receiver.

Parameters:
byteToHash - byte the byte to feed to the hash algorithm
See Also:
reset()

Final

Licensed Materials - Property of IBM
(C) Copyright IBM Corp. 2006 All Rights Reserved.