java.security
Class MessageDigest

java.lang.Object
  extended by java.security.MessageDigestSpi
      extended by java.security.MessageDigest

public abstract class MessageDigest
extends MessageDigestSpi

Message digests are secure one-way hash functions that take arbitrary-sized data and output a fixed-length hash value.

Since:
JDK 1.1
See Also:
MessageDigestSpi

Constructor Summary
protected MessageDigest(String algorithm)
          Constructs a new instance of MessageDigest representing the specified algorithm.
 
Method Summary
 Object clone()
          Returns a clone of this instance if cloning is supported.
 byte[] digest()
          Computes the final digest of the stored data.
 byte[] digest(byte[] input)
          Computes a final update using the input array of bytes, then computes a final digest and returns it.
 int digest(byte[] buf, int offset, int len)
          Computes the final digest of the stored bytes and returns the result.
 String getAlgorithm()
          Returns the name of message digest algorithm.
 int getDigestLength()
          Returns the length of the message digest.
static MessageDigest getInstance(String algorithm)
          Returns a new instance of MessageDigest representing the specified algorithm.
static MessageDigest getInstance(String algorithm, Provider provider)
          Returns a new instance of MessageDigest representing the specified algorithm from a designated Provider.
static MessageDigest getInstance(String algorithm, String provider)
          Returns a new instance of MessageDigest representing the specified algorithm from a named provider.
 Provider getProvider()
          Returns the Provider of this instance.
static boolean isEqual(byte[] digesta, byte[] digestb)
          Does a simple byte comparison of the two digests.
 void reset()
          Resets this instance.
 String toString()
          Returns a string representation of this instance.
 void update(byte input)
          Updates the digest with the byte.
 void update(byte[] input)
          Updates the digest with the bytes of an array.
 void update(byte[] input, int offset, int len)
          Updates the digest with the bytes from the array starting from the specified offset and using the specified length of bytes.
 void update(ByteBuffer input)
          Updates the digest with the remaining bytes of a buffer.
 
Methods inherited from class java.security.MessageDigestSpi
engineDigest, engineDigest, engineGetDigestLength, engineReset, engineUpdate, engineUpdate, engineUpdate
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MessageDigest

protected MessageDigest(String algorithm)
Constructs a new instance of MessageDigest representing the specified algorithm.

Parameters:
algorithm - the name of the digest algorithm to use.
Method Detail

getInstance

public static MessageDigest getInstance(String algorithm)
                                 throws NoSuchAlgorithmException
Returns a new instance of MessageDigest representing the specified algorithm.

Parameters:
algorithm - the name of the digest algorithm to use.
Returns:
a new instance representing the desired algorithm.
Throws:
NoSuchAlgorithmException - if the algorithm is not implemented by any provider.
IllegalArgumentException - if algorithm is null or is an empty string.

getInstance

public static MessageDigest getInstance(String algorithm,
                                        String provider)
                                 throws NoSuchAlgorithmException,
                                        NoSuchProviderException
Returns a new instance of MessageDigest representing the specified algorithm from a named provider.

Parameters:
algorithm - the name of the digest algorithm to use.
provider - the name of the provider to use.
Returns:
a new instance representing the desired algorithm.
Throws:
NoSuchAlgorithmException - if the algorithm is not implemented by the named provider.
NoSuchProviderException - if the named provider was not found.
IllegalArgumentException - if either algorithm or provider is null or empty.

getInstance

public static MessageDigest getInstance(String algorithm,
                                        Provider provider)
                                 throws NoSuchAlgorithmException
Returns a new instance of MessageDigest representing the specified algorithm from a designated Provider.

Parameters:
algorithm - the name of the digest algorithm to use.
provider - the Provider to use.
Returns:
a new instance representing the desired algorithm.
Throws:
NoSuchAlgorithmException - if the algorithm is not implemented by Provider.
IllegalArgumentException - if either algorithm or provider is null, or if algorithm is an empty string.
Since:
1.4
See Also:
Provider

getProvider

public final Provider getProvider()
Returns the Provider of this instance.

Returns:
the Provider of this instance.

update

public void update(byte input)
Updates the digest with the byte.

Parameters:
input - byte to update the digest with.

update

public void update(byte[] input,
                   int offset,
                   int len)
Updates the digest with the bytes from the array starting from the specified offset and using the specified length of bytes.

Parameters:
input - bytes to update the digest with.
offset - the offset to start at.
len - length of the data to update with.

update

public void update(byte[] input)
Updates the digest with the bytes of an array.

Parameters:
input - bytes to update the digest with.

update

public final void update(ByteBuffer input)
Updates the digest with the remaining bytes of a buffer.

Parameters:
input - The input byte buffer.
Since:
1.5

digest

public byte[] digest()
Computes the final digest of the stored data.

Returns:
a byte array representing the message digest.

digest

public int digest(byte[] buf,
                  int offset,
                  int len)
           throws DigestException
Computes the final digest of the stored bytes and returns the result.

Parameters:
buf - an array of bytes to store the result in.
offset - an offset to start storing the result at.
len - the length of the buffer.
Returns:
Returns the length of the buffer.
Throws:
DigestException

digest

public byte[] digest(byte[] input)
Computes a final update using the input array of bytes, then computes a final digest and returns it. It calls update(byte[]) and then digest(byte[]).

Parameters:
input - an array of bytes to perform final update with.
Returns:
a byte array representing the message digest.

toString

public String toString()
Returns a string representation of this instance.

Overrides:
toString in class Object
Returns:
a string representation of this instance.
See Also:
Object.getClass(), Object.hashCode(), Class.getName(), Integer.toHexString(int)

isEqual

public static boolean isEqual(byte[] digesta,
                              byte[] digestb)
Does a simple byte comparison of the two digests.

Parameters:
digesta - first digest to compare.
digestb - second digest to compare.
Returns:
true if both are equal, false otherwise.

reset

public void reset()
Resets this instance.


getAlgorithm

public final String getAlgorithm()
Returns the name of message digest algorithm.

Returns:
the name of message digest algorithm.

getDigestLength

public final int getDigestLength()
Returns the length of the message digest. The default is zero which means that the concrete implementation does not implement this method.

Returns:
length of the message digest.
Since:
1.2

clone

public Object clone()
             throws CloneNotSupportedException
Returns a clone of this instance if cloning is supported. If it does not then a CloneNotSupportedException is thrown. Cloning depends on whether the subclass MessageDigestSpi implements Cloneable which contains the actual implementation of the appropriate algorithm.

Overrides:
clone in class MessageDigestSpi
Returns:
a clone of this instance.
Throws:
CloneNotSupportedException - the implementation does not support cloning.
See Also:
Cloneable