Package java.security
Class MessageDigest
java.lang.Object
java.security.MessageDigestSpi
java.security.MessageDigest
- Direct Known Subclasses:
BCMessageDigest,OpenSSLMessageDigestJDK
public abstract class MessageDigest extends MessageDigestSpi
Uses a one-way hash function to turn an arbitrary number of bytes into a
fixed-length byte sequence. The original arbitrary-length sequence is the
message, and the fixed-length byte sequence is the digest or
message digest.
Sample Code
The basic pattern to digest an InputStream looks like this:
MessageDigest digester = MessageDigest.getInstance("MD5");
byte[] bytes = new byte[8192];
int byteCount;
while ((byteCount = in.read(bytes)) > 0) {
digester.update(bytes, 0, byteCount);
}
byte[] digest = digester.digest();
That is, after creating or resetting a MessageDigest you should
call update(byte[],int,int) for each block of input data, and then call digest()
to get the final digest. Note that calling digest resets the MessageDigest.
Advanced users who want partial digests should clone their MessageDigest before
calling digest.
This class is not thread-safe.
- See Also:
MessageDigestSpi
-
Constructor Summary
Constructors Modifier Constructor Description protectedMessageDigest(String algorithm)Constructs a new instance ofMessageDigestwith the name of the algorithm to use. -
Method Summary
Modifier and Type Method Description Objectclone()Creates and returns a copy of thisObject.byte[]digest()Computes and returns the final hash value for thisMessageDigest.byte[]digest(byte[] input)Performs the final update and then computes and returns the final hash value for thisMessageDigest.intdigest(byte[] buf, int offset, int len)Computes and stores the final hash value for thisMessageDigest.StringgetAlgorithm()Returns the name of the algorithm of thisMessageDigest.intgetDigestLength()Returns the engine digest length in bytes.static MessageDigestgetInstance(String algorithm)Returns a new instance ofMessageDigestthat utilizes the specified algorithm.static MessageDigestgetInstance(String algorithm, String provider)Returns a new instance ofMessageDigestthat utilizes the specified algorithm from the specified provider.static MessageDigestgetInstance(String algorithm, Provider provider)Returns a new instance ofMessageDigestthat utilizes the specified algorithm from the specified provider.ProvidergetProvider()Returns the provider associated with thisMessageDigest.static booleanisEqual(byte[] digesta, byte[] digestb)Indicates whether to digest are equal by performing a simply byte-per-byte compare of the two digests.voidreset()Puts thisMessageDigestback in an initial state, such that it is ready to compute a one way hash value.StringtoString()Returns a string containing a concise, human-readable description of thisMessageDigestincluding the name of its algorithm.voidupdate(byte arg0)Updates thisMessageDigestusing the givenbyte.voidupdate(byte[] input)Updates thisMessageDigestusing the givenbyte[].voidupdate(byte[] input, int offset, int len)Updates thisMessageDigestusing the givenbyte[].voidupdate(ByteBuffer input)Updates thisMessageDigestusing the giveninput.Methods inherited from class java.security.MessageDigestSpi
engineDigest, engineDigest, engineGetDigestLength, engineReset, engineUpdate, engineUpdate, engineUpdate
-
Constructor Details
-
MessageDigest
Constructs a new instance ofMessageDigestwith the name of the algorithm to use.- Parameters:
algorithm- the name of algorithm to use
-
-
Method Details
-
getInstance
Returns a new instance ofMessageDigestthat utilizes the specified algorithm.- Parameters:
algorithm- the name of the algorithm to use- Returns:
- a new instance of
MessageDigestthat utilizes the specified algorithm - Throws:
NoSuchAlgorithmException- if the specified algorithm is not availableNullPointerException- ifalgorithmisnull
-
getInstance
public static MessageDigest getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderExceptionReturns a new instance ofMessageDigestthat utilizes the specified algorithm from the specified provider.- Parameters:
algorithm- the name of the algorithm to useprovider- the name of the provider- Returns:
- a new instance of
MessageDigestthat utilizes the specified algorithm from the specified provider - Throws:
NoSuchAlgorithmException- if the specified algorithm is not availableNoSuchProviderException- if the specified provider is not availableNullPointerException- ifalgorithmisnullIllegalArgumentException- ifprovider == null || provider.isEmpty()
-
getInstance
public static MessageDigest getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmExceptionReturns a new instance ofMessageDigestthat utilizes the specified algorithm from the specified provider.- Parameters:
algorithm- the name of the algorithm to useprovider- the provider- Returns:
- a new instance of
MessageDigestthat utilizes the specified algorithm from the specified provider - Throws:
NoSuchAlgorithmException- if the specified algorithm is not availableNullPointerException- ifalgorithmisnullIllegalArgumentException- ifprovider == null
-
reset
public void reset()Puts thisMessageDigestback in an initial state, such that it is ready to compute a one way hash value. -
update
public void update(byte arg0)Updates thisMessageDigestusing the givenbyte.- Parameters:
arg0- thebyteto update thisMessageDigestwith- See Also:
reset()
-
update
public void update(byte[] input, int offset, int len)Updates thisMessageDigestusing the givenbyte[].- Parameters:
input- thebytearrayoffset- the index of the first byte ininputto update fromlen- the number of bytes ininputto update from- Throws:
IllegalArgumentException- ifoffsetorlenare not valid in respect toinput
-
update
public void update(byte[] input)Updates thisMessageDigestusing the givenbyte[].- Parameters:
input- thebytearray- Throws:
NullPointerException- ifinputisnull
-
digest
public byte[] digest()Computes and returns the final hash value for thisMessageDigest. After the digest is computed the receiver is reset.- Returns:
- the computed one way hash value
- See Also:
reset()
-
digest
Computes and stores the final hash value for thisMessageDigest. After the digest is computed the receiver is reset.- Parameters:
buf- the buffer to store the resultoffset- the index of the first byte inbufto storelen- the number of bytes allocated for the digest- Returns:
- the number of bytes written to
buf - Throws:
DigestException- if an error occursIllegalArgumentException- ifoffsetorlenare not valid in respect tobuf- See Also:
reset()
-
digest
public byte[] digest(byte[] input)Performs the final update and then computes and returns the final hash value for thisMessageDigest. After the digest is computed the receiver is reset.- Parameters:
input- thebytearray- Returns:
- the computed one way hash value
- See Also:
reset()
-
toString
Returns a string containing a concise, human-readable description of thisMessageDigestincluding the name of its algorithm. -
isEqual
public static boolean isEqual(byte[] digesta, byte[] digestb)Indicates whether to digest are equal by performing a simply byte-per-byte compare of the two digests.- Parameters:
digesta- the first digest to be compareddigestb- the second digest to be compared- Returns:
trueif the two hashes are equal,falseotherwise
-
getAlgorithm
Returns the name of the algorithm of thisMessageDigest.- Returns:
- the name of the algorithm of this
MessageDigest
-
getProvider
Returns the provider associated with thisMessageDigest.- Returns:
- the provider associated with this
MessageDigest
-
getDigestLength
public final int getDigestLength()Returns the engine digest length in bytes. If the implementation does not implement this function or is not an instance ofCloneable,0is returned.- Returns:
- the digest length in bytes, or
0
-
clone
Description copied from class:ObjectCreates and returns a copy of thisObject. The default implementation returns a so-called "shallow" copy: It creates a new instance of the same class and then copies the field values (including object references) from this instance to the new instance. A "deep" copy, in contrast, would also recursively clone nested objects. A subclass that needs to implement this kind of cloning should callsuper.clone()to create the new instance and then create deep copies of the nested, mutable objects.- Overrides:
clonein classMessageDigestSpi- Returns:
- a copy of this object.
- Throws:
CloneNotSupportedException- if this object's class does not implement theCloneableinterface.
-
update
Updates thisMessageDigestusing the giveninput.- Parameters:
input- theByteBuffer
-