Top Description Inners Fields Constructors Methods
java.security

public abstract Class MessageDigest

extends MessageDigestSpi
Class Inheritance
Known Direct Subclasses
java.security.MessageDigest.Delegate, sun.security.provider.DSA.Raw.NullDigest20
Imports
java.util.*, java.io.ByteArrayOutputStream, .PrintStream, java.nio.ByteBuffer, sun.security.jca.GetInstance, sun.security.util.Debug, .MessageDigestSpi2, javax.crypto.SecretKey

This MessageDigest class provides applications the functionality of a message digest algorithm, such as SHA-1 or SHA-256. Message digests are secure one-way hash functions that take arbitrary-sized data and output a fixed-length hash value.

A MessageDigest object starts out initialized. The data is processed through it using the update methods. At any point reset can be called to reset the digest. Once all the data to be updated has been updated, one of the digest methods should be called to complete the hash computation.

The digest method can be called once for a given number of updates. After digest has been called, the MessageDigest object is reset to its initialized state.

Implementations are free to implement the Cloneable interface. Client applications can test cloneability by attempting cloning and catching the CloneNotSupportedException:

MessageDigest md = MessageDigest.getInstance("SHA-256");

try {
    md.update(toChapter1);
    MessageDigest tc1 = md.clone();
    byte[] toChapter1Digest = tc1.digest();
    md.update(toChapter2);
    ...etc.
} catch (CloneNotSupportedException cnse) {
    throw new DigestException("couldn't make digest of partial content");
}

Note that if a given implementation is not cloneable, it is still possible to compute intermediate digests by instantiating several instances, if the number of digests is known in advance.

Note that this class is abstract and extends from MessageDigestSpi for historical reasons. Application developers should only take notice of the methods defined in this MessageDigest class; all the methods in the superclass are intended for cryptographic service providers who wish to supply their own implementations of message digest algorithms.

Every implementation of the Java platform is required to support the following standard MessageDigest algorithms:

These algorithms are described in the MessageDigest section of the Java Security Standard Algorithm Names Specification. Consult the release documentation for your implementation to see if any other algorithms are supported.
Author
Benjamin Renaud
Since
1.1
See Also
DigestInputStream, DigestOutputStream

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private final String
private static final int
private static final int
private static final Debug
private Provider
private static final boolean
private int

Constructor Summary

AccessConstructor and Description
protected
MessageDigest(String
the standard name of the digest algorithm. See the MessageDigest section in the Java Security Standard Algorithm Names Specification for information about standard algorithm names.
algorithm
)

Creates a message digest with the specified algorithm name.

private

Method Summary

Modifier and TypeMethod and Description
public Object

Returns:

a clone if the implementation is cloneable.
clone
()

Overrides java.security.MessageDigestSpi.clone.

Returns a clone if the implementation is cloneable.

public byte[]

Returns:

the array of bytes for the resulting hash value.
digest
()

Completes the hash computation by performing final operations such as padding.

public int

Returns:

the number of bytes placed into buf
digest
(byte[]
output buffer for the computed digest
buf
,
int
offset into the output buffer to begin storing the digest
offset
,
int
number of bytes within buf allotted for the digest
len
)

Completes the hash computation by performing final operations such as padding.

public byte[]

Returns:

the array of bytes for the resulting hash value.
digest
(byte[]
the input to be updated before the digest is completed.
input
)

Performs a final update on the digest using the specified array of bytes, then completes the digest computation.

public final String

Returns:

the name of the algorithm
getAlgorithm
()

Returns a string that identifies the algorithm, independent of implementation details.

public final int

Returns:

the digest length in bytes, or 0 if this operation is not supported by the provider and the implementation is not cloneable.
getDigestLength
()

Returns the length of the digest in bytes, or 0 if this operation is not supported by the provider and the implementation is not cloneable.

public static MessageDigest

Returns:

a MessageDigest object that implements the specified algorithm
getInstance
(String
the name of the algorithm requested. See the MessageDigest section in the Java Security Standard Algorithm Names Specification for information about standard algorithm names.
algorithm
)

Returns a MessageDigest object that implements the specified digest algorithm.

public static MessageDigest

Returns:

a MessageDigest object that implements the specified algorithm
getInstance
(String
the name of the algorithm requested. See the MessageDigest section in the Java Security Standard Algorithm Names Specification for information about standard algorithm names.
algorithm
,
String
the name of the provider.
provider
)

Returns a MessageDigest object that implements the specified digest algorithm.

public static MessageDigest

Returns:

a MessageDigest object that implements the specified algorithm
getInstance
(String
the name of the algorithm requested. See the MessageDigest section in the Java Security Standard Algorithm Names Specification for information about standard algorithm names.
algorithm
,
Provider
the provider.
provider
)

Returns a MessageDigest object that implements the specified digest algorithm.

public final Provider

Returns:

the provider of this message digest object
getProvider
()

Returns the provider of this message digest object.

private String
public static boolean

Returns:

true if the digests are equal, false otherwise.
isEqual
(byte[]
one of the digests to compare.
digesta
,
byte[]
the other digest to compare.
digestb
)

Compares two digests for equality.

public void
reset()

Resets the digest for further use.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of this message digest object.

public void
update(byte
the byte with which to update the digest.
input
)

Updates the digest using the specified byte.

public void
update(byte[]
the array of bytes.
input
,
int
the offset to start from in the array of bytes.
offset
,
int
the number of bytes to use, starting at offset.
len
)

Updates the digest using the specified array of bytes, starting at the specified offset.

public void
update(byte[]
the array of bytes.
input
)

Updates the digest using the specified array of bytes.

public final void
update(ByteBuffer
the ByteBuffer
input
)

Update the digest using the specified ByteBuffer.

Inherited from java.security.MessageDigestSpi:
engineDigestengineDigestengineGetDigestLengthengineResetengineUpdateengineUpdateengineUpdate

Field Detail

algorithmback to summary
private final String algorithm
IN_PROGRESSback to summary
private static final int IN_PROGRESS
INITIALback to summary
private static final int INITIAL
pdebugback to summary
private static final Debug pdebug
providerback to summary
private Provider provider
skipDebugback to summary
private static final boolean skipDebug
stateback to summary
private int state

Constructor Detail

MessageDigestback to summary
protected MessageDigest(String algorithm)

Creates a message digest with the specified algorithm name.

Parameters
algorithm:String

the standard name of the digest algorithm. See the MessageDigest section in the Java Security Standard Algorithm Names Specification for information about standard algorithm names.

MessageDigestback to summary
private MessageDigest(String algorithm, Provider p)

Method Detail

cloneback to summary
public Object clone() throws CloneNotSupportedException

Overrides java.security.MessageDigestSpi.clone.

Returns a clone if the implementation is cloneable.

Returns:Object

a clone if the implementation is cloneable.

Exceptions
CloneNotSupportedException:
if this is called on an implementation that does not support Cloneable.
digestback to summary
public byte[] digest()

Completes the hash computation by performing final operations such as padding. The digest is reset after this call is made.

Returns:byte[]

the array of bytes for the resulting hash value.

digestback to summary
public int digest(byte[] buf, int offset, int len) throws DigestException

Completes the hash computation by performing final operations such as padding. The digest is reset after this call is made.

Parameters
buf:byte[]

output buffer for the computed digest

offset:int

offset into the output buffer to begin storing the digest

len:int

number of bytes within buf allotted for the digest

Returns:int

the number of bytes placed into buf

Exceptions
DigestException:
if an error occurs.
digestback to summary
public byte[] digest(byte[] input)

Performs a final update on the digest using the specified array of bytes, then completes the digest computation. That is, this method first calls update(input), passing the input array to the update method, then calls digest().

Parameters
input:byte[]

the input to be updated before the digest is completed.

Returns:byte[]

the array of bytes for the resulting hash value.

getAlgorithmback to summary
public final String getAlgorithm()

Returns a string that identifies the algorithm, independent of implementation details. The name should be a standard Java Security name (such as "SHA-256"). See the MessageDigest section in the Java Security Standard Algorithm Names Specification for information about standard algorithm names.

Returns:String

the name of the algorithm

getDigestLengthback to summary
public final int getDigestLength()

Returns the length of the digest in bytes, or 0 if this operation is not supported by the provider and the implementation is not cloneable.

Returns:int

the digest length in bytes, or 0 if this operation is not supported by the provider and the implementation is not cloneable.

Since
1.2
getInstanceback to summary
public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException

Returns a MessageDigest object that implements the specified digest algorithm.

This method traverses the list of registered security Providers, starting with the most preferred Provider. A new MessageDigest object encapsulating the MessageDigestSpi implementation from the first provider that supports the specified algorithm is returned.

Note that the list of registered providers may be retrieved via the Security.getProviders() method.

Implementation Note

The JDK Reference Implementation additionally uses the jdk.security.provider.preferred Security property to determine the preferred provider order for the specified algorithm. This may be different from the order of providers returned by Security.getProviders().

Parameters
algorithm:String

the name of the algorithm requested. See the MessageDigest section in the Java Security Standard Algorithm Names Specification for information about standard algorithm names.

Returns:MessageDigest

a MessageDigest object that implements the specified algorithm

Exceptions
NoSuchAlgorithmException:
if no Provider supports a MessageDigestSpi implementation for the specified algorithm
NullPointerException:
if algorithm is null
See Also
Provider
getInstanceback to summary
public static MessageDigest getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException

Returns a MessageDigest object that implements the specified digest algorithm.

A new MessageDigest object encapsulating the MessageDigestSpi implementation from the specified provider is returned. The specified provider must be registered in the security provider list.

Note that the list of registered providers may be retrieved via the Security.getProviders() method.

Parameters
algorithm:String

the name of the algorithm requested. See the MessageDigest section in the Java Security Standard Algorithm Names Specification for information about standard algorithm names.

provider:String

the name of the provider.

Returns:MessageDigest

a MessageDigest object that implements the specified algorithm

Exceptions
NoSuchAlgorithmException:
if a MessageDigestSpi implementation for the specified algorithm is not available from the specified provider
NoSuchProviderException:
if the specified provider is not registered in the security provider list
IllegalArgumentException:
if the provider name is null or empty
NullPointerException:
if algorithm is null
See Also
Provider
getInstanceback to summary
public static MessageDigest getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException

Returns a MessageDigest object that implements the specified digest algorithm.

A new MessageDigest object encapsulating the MessageDigestSpi implementation from the specified provider is returned. Note that the specified provider does not have to be registered in the provider list.

Parameters
algorithm:String

the name of the algorithm requested. See the MessageDigest section in the Java Security Standard Algorithm Names Specification for information about standard algorithm names.

provider:Provider

the provider.

Returns:MessageDigest

a MessageDigest object that implements the specified algorithm

Exceptions
NoSuchAlgorithmException:
if a MessageDigestSpi implementation for the specified algorithm is not available from the specified Provider object
IllegalArgumentException:
if the specified provider is null
NullPointerException:
if algorithm is null
Since
1.4
See Also
Provider
getProviderback to summary
public final Provider getProvider()

Returns the provider of this message digest object.

Returns:Provider

the provider of this message digest object

getProviderNameback to summary
private String getProviderName()
isEqualback to summary
public static boolean isEqual(byte[] digesta, byte[] digestb)

Compares two digests for equality. Two digests are equal if they have the same length and all bytes at corresponding positions are equal.

Implementation Note

All bytes in digesta are examined to determine equality, unless digestb is null or has a length of zero bytes. If digestb is not null and does not have a length of zero bytes, then the calculation time depends only on the length of digesta. It does not depend on the length of digestb or the contents of digesta and digestb.

Parameters
digesta:byte[]

one of the digests to compare.

digestb:byte[]

the other digest to compare.

Returns:boolean

true if the digests are equal, false otherwise.

resetback to summary
public void reset()

Resets the digest for further use.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns a string representation of this message digest object.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object

updateback to summary
public void update(byte input)

Updates the digest using the specified byte.

Parameters
input:byte

the byte with which to update the digest.

updateback to summary
public void update(byte[] input, int offset, int len)

Updates the digest using the specified array of bytes, starting at the specified offset.

Parameters
input:byte[]

the array of bytes.

offset:int

the offset to start from in the array of bytes.

len:int

the number of bytes to use, starting at offset.

updateback to summary
public void update(byte[] input)

Updates the digest using the specified array of bytes.

Parameters
input:byte[]

the array of bytes.

updateback to summary
public final void update(ByteBuffer input)

Update the digest using the specified ByteBuffer. The digest is updated using the input.remaining() bytes starting at input.position(). Upon return, the buffer's position will be equal to its limit; its limit will not have changed.

Parameters
input:ByteBuffer

the ByteBuffer

Since
1.5
java.security back to summary

private Class MessageDigest.Delegate

extends MessageDigest
implements MessageDigestSpi2
Class Inheritance
All Implemented Interfaces
sun.security.util.MessageDigestSpi2
Known Direct Subclasses
java.security.MessageDigest.Delegate.CloneableDelegate

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private final MessageDigestSpi

Constructor Summary

AccessConstructor and Description
private
Delegate(MessageDigestSpi digestSpi, String algorithm, Provider p)

Method Summary

Modifier and TypeMethod and Description
public Object

Returns:

a clone if the delegate is cloneable.
clone
()

Overrides java.security.MessageDigest.clone.

Returns a clone if the delegate is cloneable.

protected byte[]
engineDigest()

Implements abstract java.security.MessageDigestSpi.engineDigest.

Completes the hash computation by performing final operations such as padding.

protected int
engineDigest(byte[]
the output buffer in which to store the digest
buf
,
int
offset to start from in the output buffer
offset
,
int
number of bytes within buf allotted for the digest. Both this default implementation and the SUN provider do not return partial digests. The presence of this parameter is solely for consistency in our API's. If the value of this parameter is less than the actual digest length, the method will throw a DigestException. This parameter is ignored if its value is greater than or equal to the actual digest length.
len
)

Overrides java.security.MessageDigestSpi.engineDigest.

Completes the hash computation by performing final operations such as padding.

protected int
engineGetDigestLength()

Overrides java.security.MessageDigestSpi.engineGetDigestLength.

Returns the digest length in bytes.

protected void
engineReset()

Implements abstract java.security.MessageDigestSpi.engineReset.

Resets the digest for further use.

protected void
engineUpdate(byte
the byte to use for the update.
input
)

Implements abstract java.security.MessageDigestSpi.engineUpdate.

Updates the digest using the specified byte.

protected void
engineUpdate(byte[]
the array of bytes to use for the update.
input
,
int
the offset to start from in the array of bytes.
offset
,
int
the number of bytes to use, starting at offset.
len
)

Implements abstract java.security.MessageDigestSpi.engineUpdate.

Updates the digest using the specified array of bytes, starting at the specified offset.

protected void
engineUpdate(ByteBuffer
the ByteBuffer
input
)

Overrides java.security.MessageDigestSpi.engineUpdate.

Update the digest using the specified ByteBuffer.

public void
engineUpdate(SecretKey
the key whose value is to be digested.
key
)

Implements sun.security.util.MessageDigestSpi2.engineUpdate.

Updates the digest using the specified key.

pack-priv static MessageDigest.Delegate
of(MessageDigestSpi digestSpi, String algo, Provider p)

Inherited from java.security.MessageDigest:
digestdigestdigestgetAlgorithmgetDigestLengthgetInstancegetInstancegetInstancegetProviderisEqualresettoStringupdateupdateupdateupdate

Field Detail

digestSpiback to summary
private final MessageDigestSpi digestSpi

Constructor Detail

Delegateback to summary
private Delegate(MessageDigestSpi digestSpi, String algorithm, Provider p)

Method Detail

cloneback to summary
public Object clone() throws CloneNotSupportedException

Overrides java.security.MessageDigest.clone.

Returns a clone if the delegate is cloneable.

Returns:Object

a clone if the delegate is cloneable.

Annotations
@Override
Exceptions
CloneNotSupportedException:
if this is called on a delegate that does not support Cloneable.
engineDigestback to summary
protected byte[] engineDigest()

Implements abstract java.security.MessageDigestSpi.engineDigest.

Doc from java.security.MessageDigestSpi.engineDigest.

Completes the hash computation by performing final operations such as padding. Once engineDigest has been called, the engine should be reset (see engineReset). Resetting is the responsibility of the engine implementor.

Returns:byte[]

the array of bytes for the resulting hash value.

Annotations
@Override
engineDigestback to summary
protected int engineDigest(byte[] buf, int offset, int len) throws DigestException

Overrides java.security.MessageDigestSpi.engineDigest.

Doc from java.security.MessageDigestSpi.engineDigest.

Completes the hash computation by performing final operations such as padding. Once engineDigest has been called, the engine should be reset (see engineReset). Resetting is the responsibility of the engine implementor. This method should be abstract, but we leave it concrete for binary compatibility. Knowledgeable providers should override this method.

Parameters
buf:byte[]

the output buffer in which to store the digest

offset:int

offset to start from in the output buffer

len:int

number of bytes within buf allotted for the digest. Both this default implementation and the SUN provider do not return partial digests. The presence of this parameter is solely for consistency in our API's. If the value of this parameter is less than the actual digest length, the method will throw a DigestException. This parameter is ignored if its value is greater than or equal to the actual digest length.

Returns:int

the length of the digest stored in the output buffer.

Annotations
@Override
Exceptions
DigestException:
if an error occurs.
engineGetDigestLengthback to summary
protected int engineGetDigestLength()

Overrides java.security.MessageDigestSpi.engineGetDigestLength.

Doc from java.security.MessageDigestSpi.engineGetDigestLength.

Returns the digest length in bytes.

This concrete method has been added to this previously-defined abstract class. (For backwards compatibility, it cannot be abstract.)

The default behavior is to return 0.

This method may be overridden by a provider to return the digest length.

Returns:int

the digest length in bytes.

Annotations
@Override
engineResetback to summary
protected void engineReset()

Implements abstract java.security.MessageDigestSpi.engineReset.

Doc from java.security.MessageDigestSpi.engineReset.

Resets the digest for further use.

Annotations
@Override
engineUpdateback to summary
protected void engineUpdate(byte input)

Implements abstract java.security.MessageDigestSpi.engineUpdate.

Doc from java.security.MessageDigestSpi.engineUpdate.

Updates the digest using the specified byte.

Parameters
input:byte

the byte to use for the update.

Annotations
@Override
engineUpdateback to summary
protected void engineUpdate(byte[] input, int offset, int len)

Implements abstract java.security.MessageDigestSpi.engineUpdate.

Doc from java.security.MessageDigestSpi.engineUpdate.

Updates the digest using the specified array of bytes, starting at the specified offset.

Parameters
input:byte[]

the array of bytes to use for the update.

offset:int

the offset to start from in the array of bytes.

len:int

the number of bytes to use, starting at offset.

Annotations
@Override
engineUpdateback to summary
protected void engineUpdate(ByteBuffer input)

Overrides java.security.MessageDigestSpi.engineUpdate.

Doc from java.security.MessageDigestSpi.engineUpdate.

Update the digest using the specified ByteBuffer. The digest is updated using the input.remaining() bytes starting at input.position(). Upon return, the buffer's position will be equal to its limit; its limit will not have changed.

Parameters
input:ByteBuffer

the ByteBuffer

Annotations
@Override
engineUpdateback to summary
public void engineUpdate(SecretKey key) throws InvalidKeyException

Implements sun.security.util.MessageDigestSpi2.engineUpdate.

Doc from sun.security.util.MessageDigestSpi2.engineUpdate.

Updates the digest using the specified key. This is used for SSL 3.0 only, we may deprecate and remove the support of this in the future

Parameters
key:SecretKey

the key whose value is to be digested.

Annotations
@Override
ofback to summary
pack-priv static MessageDigest.Delegate of(MessageDigestSpi digestSpi, String algo, Provider p)
java.security back to summary

private final Class MessageDigest.Delegate.CloneableDelegate

extends Delegate
implements Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable

Constructor Summary

AccessConstructor and Description
private

Constructor Detail

CloneableDelegateback to summary
private CloneableDelegate(MessageDigestSpi digestSpi, String algorithm, Provider p)