java.security

Class 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
Objectclone()
Returns a clone of this instance if cloning is supported.
byte[]digest()
Computes the final digest of the stored data.
intdigest(byte[] buf, int offset, int len)
Computes the final digest of the stored bytes and returns the result.
byte[]digest(byte[] input)
Computes a final update using the input array of bytes, then computes a final digest and returns it.
StringgetAlgorithm()
Returns the name of message digest algorithm.
intgetDigestLength()
Returns the length of the message digest.
static MessageDigestgetInstance(String algorithm)
Returns a new instance of MessageDigest representing the specified algorithm.
static MessageDigestgetInstance(String algorithm, String provider)
Returns a new instance of MessageDigest representing the specified algorithm from a named provider.
static MessageDigestgetInstance(String algorithm, Provider provider)
Returns a new instance of MessageDigest representing the specified algorithm from a designated {@link Provider}.
ProvidergetProvider()
Returns the {@link Provider} of this instance.
static booleanisEqual(byte[] digesta, byte[] digestb)
Does a simple byte comparison of the two digests.
voidreset()
Resets this instance.
StringtoString()
Returns a string representation of this instance.
voidupdate(byte input)
Updates the digest with the byte.
voidupdate(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.
voidupdate(byte[] input)
Updates the digest with the bytes of an array.
voidupdate(ByteBuffer input)
Updates the digest with the remaining bytes of a buffer.

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

clone

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

Returns: a clone of this instance.

Throws: CloneNotSupportedException the implementation does not support cloning.

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)
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.

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 {@link #update(byte[])} and then {@link #digest(byte[])}.

Parameters: input an array of bytes to perform final update with.

Returns: a byte array representing the message digest.

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

getInstance

public static MessageDigest getInstance(String algorithm)
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)
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)
Returns a new instance of MessageDigest representing the specified algorithm from a designated {@link Provider}.

Parameters: algorithm the name of the digest algorithm to use. provider the {@link Provider} to use.

Returns: a new instance representing the desired algorithm.

Throws: NoSuchAlgorithmException if the algorithm is not implemented by {@link 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 {@link Provider} of this instance.

Returns: the {@link Provider} of this instance.

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.

toString

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

Returns: a string representation 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 void update(ByteBuffer input)
Updates the digest with the remaining bytes of a buffer.

Parameters: input The input byte buffer.

Since: 1.5