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

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, String provider)
Returns a new instance of MessageDigest representing the specified algorithm from a named provider.
static MessageDigest
getInstance(String algorithm, Provider provider)
Returns a new instance of MessageDigest representing the specified algorithm from a designated 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

clone, engineDigest, engineDigest, engineGetDigestLength, engineReset, engineUpdate, engineUpdate, engineUpdate

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

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 Details

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 interface MessageDigestSpi
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 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.

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.

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

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.
Overrides:
toString in interface Object
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)
Updates the digest with the bytes of an array.
Parameters:
input - bytes 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 final void update(ByteBuffer input)
Updates the digest with the remaining bytes of a buffer.
Parameters:
input - The input byte buffer.
Since:
1.5

MessageDigest.java --- The message digest interface. Copyright (C) 1999, 2002, 2003, 2006 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.