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:
SHA-1
SHA-256
DigestInputStream
, DigestOutputStream
Modifier and Type | Class and Description |
---|---|
private static class |
Modifier and Type | Field 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 |
Access | Constructor 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 |
Modifier and Type | Method and Description |
---|---|
public Object | Returns: a clone if the implementation is cloneable.Overrides java. Returns a clone if the implementation is cloneable. |
public byte[] | Returns: the array of bytes for the resulting hash value.Completes the hash computation by performing final operations such as padding. |
public int | Returns: the number of bytes placed intobuf 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.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 algorithmReturns 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.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: aMessageDigest object that implements the
specified algorithmthe 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 |
public static MessageDigest | Returns: aMessageDigest object that implements the
specified algorithmthe 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 |
public static MessageDigest | Returns: aMessageDigest object that implements the
specified algorithmthe 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 |
public final Provider | Returns: the provider of this message digest objectReturns the provider of this message digest object. |
private String | |
public static boolean | Returns: true if the digests are equal, false otherwise.one of the digests to compare. digesta, byte[] the other digest to compare. digestb)Compares two digests for equality. |
public void | |
public String | toString()
Overrides java. 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
len)offset .Updates the digest using the specified array of bytes, starting at the specified offset. |
public void | |
public final void |
algorithm | back to summary |
---|---|
private final String algorithm |
IN_PROGRESS | back to summary |
---|---|
private static final int IN_PROGRESS |
INITIAL | back to summary |
---|---|
private static final int INITIAL |
pdebug | back to summary |
---|---|
private static final Debug pdebug |
provider | back to summary |
---|---|
private Provider provider |
skipDebug | back to summary |
---|---|
private static final boolean skipDebug |
state | back to summary |
---|---|
private int state |
MessageDigest | back to summary |
---|---|
protected MessageDigest(String algorithm) Creates a message digest with the specified algorithm name.
|
MessageDigest | back to summary |
---|---|
private MessageDigest(String algorithm, Provider p) |
clone | back to summary |
---|---|
public Object clone() throws CloneNotSupportedException Overrides java. Returns a clone if the implementation is cloneable.
|
digest | back 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.
|
digest | back 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.
|
digest | back 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
|
getAlgorithm | back 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.
|
getDigestLength | back 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.
|
getInstance | back to summary |
---|---|
public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException Returns a This method traverses the list of registered security Providers,
starting with the most preferred Provider.
A new Note that the list of registered providers may be retrieved via
the Implementation Note The JDK Reference Implementation additionally uses the
|
getInstance | back to summary |
---|---|
public static MessageDigest getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException Returns a A new Note that the list of registered providers may be retrieved via
the
|
getInstance | back to summary |
---|---|
public static MessageDigest getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException Returns a A new
|
getProvider | back to summary |
---|---|
public final Provider getProvider() Returns the provider of this message digest object.
|
getProviderName | back to summary |
---|---|
private String getProviderName() |
isEqual | back 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
|
reset | back to summary |
---|---|
public void reset() Resets the digest for further use. |
toString | back to summary |
---|---|
public String toString() Overrides java. Returns a string representation of this message digest object.
|
update | back to summary |
---|---|
public void update(byte input) Updates the digest using the specified byte.
|
update | back 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.
|
update | back to summary |
---|---|
public void update(byte[] input) Updates the digest using the specified array of bytes.
|
update | back to summary |
---|---|
public final void update(ByteBuffer input) Update the digest using the specified ByteBuffer. The digest is
updated using the
|
Modifier and Type | Class and Description |
---|---|
private static class |
Modifier and Type | Field and Description |
---|---|
private final MessageDigestSpi |
Access | Constructor and Description |
---|---|
private |
Modifier and Type | Method and Description |
---|---|
public Object | Returns: a clone if the delegate is cloneable.Overrides java. Returns a clone if the delegate is cloneable. |
protected byte[] | engineDigest()
Implements abstract java. 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 len)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.Overrides java. Completes the hash computation by performing final operations such as padding. |
protected int | engineGetDigestLength()
Overrides java. Returns the digest length in bytes. |
protected void | engineReset()
Implements abstract java. Resets the digest for further use. |
protected void | engineUpdate(byte
the byte to use for the update. input)Implements abstract java. 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
len)offset .Implements abstract java. Updates the digest using the specified array of bytes, starting at the specified offset. |
protected void | engineUpdate(ByteBuffer
the ByteBuffer input)Overrides java. Update the digest using the specified ByteBuffer. |
public void | engineUpdate(SecretKey
the key whose value is to be digested. key)Implements sun. Updates the digest using the specified key. |
pack-priv static MessageDigest. |
digestSpi | back to summary |
---|---|
private final MessageDigestSpi digestSpi |
Delegate | back to summary |
---|---|
private Delegate(MessageDigestSpi digestSpi, String algorithm, Provider p) |
clone | back to summary |
---|---|
public Object clone() throws CloneNotSupportedException Overrides java. Returns a clone if the delegate is cloneable.
|
engineDigest | back to summary |
---|---|
protected byte[] engineDigest() Implements abstract java. Doc from java. Completes the hash computation by performing final
operations such as padding. Once
|
engineDigest | back to summary |
---|---|
protected int engineDigest(byte[] buf, int offset, int len) throws DigestException Overrides java. Doc from java. Completes the hash computation by performing final
operations such as padding. Once
|
engineGetDigestLength | back to summary |
---|---|
protected int engineGetDigestLength() Overrides java. Doc from java. 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.
|
engineReset | back to summary |
---|---|
protected void engineReset() Implements abstract java. Doc from java. Resets the digest for further use.
|
engineUpdate | back to summary |
---|---|
protected void engineUpdate(byte input) Implements abstract java. Doc from java. Updates the digest using the specified byte.
|
engineUpdate | back to summary |
---|---|
protected void engineUpdate(byte[] input, int offset, int len) Implements abstract java. Doc from java. Updates the digest using the specified array of bytes, starting at the specified offset.
|
engineUpdate | back to summary |
---|---|
protected void engineUpdate(ByteBuffer input) Overrides java. Doc from java. Update the digest using the specified ByteBuffer. The digest is
updated using the
|
engineUpdate | back to summary |
---|---|
public void engineUpdate(SecretKey key) throws InvalidKeyException Implements sun. Doc from sun. 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 |
of | back to summary |
---|---|
pack-priv static MessageDigest. |
Access | Constructor and Description |
---|---|
private |
CloneableDelegate | back to summary |
---|---|
private CloneableDelegate(MessageDigestSpi digestSpi, String algorithm, Provider p) |