public abstract class MessageDigest extends MessageDigestSpi
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.
MessageDigestSpi
Modifier | Constructor and Description |
---|---|
protected |
MessageDigest(String algorithm)
Constructs a new instance of
MessageDigest with the name of
the algorithm to use. |
Modifier and Type | Method and Description |
---|---|
Object |
clone()
Creates and returns a copy of this
Object . |
byte[] |
digest()
Computes and returns the final hash value for this
MessageDigest . |
byte[] |
digest(byte[] input)
Performs the final update and then computes and returns the final hash
value for this
MessageDigest . |
int |
digest(byte[] buf,
int offset,
int len)
Computes and stores the final hash value for this
MessageDigest . |
String |
getAlgorithm()
Returns the name of the algorithm of this
MessageDigest . |
int |
getDigestLength()
Returns the engine digest length in bytes.
|
static MessageDigest |
getInstance(String algorithm)
Returns a new instance of
MessageDigest that utilizes the
specified algorithm. |
static MessageDigest |
getInstance(String algorithm,
Provider provider)
Returns a new instance of
MessageDigest that utilizes the
specified algorithm from the specified provider. |
static MessageDigest |
getInstance(String algorithm,
String provider)
Returns a new instance of
MessageDigest that utilizes the
specified algorithm from the specified provider. |
Provider |
getProvider()
Returns the provider associated with this
MessageDigest . |
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.
|
void |
reset()
Puts this
MessageDigest back in an initial state, such that it is
ready to compute a one way hash value. |
String |
toString()
Returns a string containing a concise, human-readable description of this
MessageDigest including the name of its algorithm. |
void |
update(byte arg0)
Updates this
MessageDigest using the given byte . |
void |
update(byte[] input)
Updates this
MessageDigest using the given byte[] . |
void |
update(byte[] input,
int offset,
int len)
Updates this
MessageDigest using the given byte[] . |
void |
update(ByteBuffer input)
Updates this
MessageDigest using the given input . |
engineDigest, engineDigest, engineGetDigestLength, engineReset, engineUpdate, engineUpdate, engineUpdate
protected MessageDigest(String algorithm)
MessageDigest
with the name of
the algorithm to use.algorithm
- the name of algorithm to usepublic static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException
MessageDigest
that utilizes the
specified algorithm.algorithm
- the name of the algorithm to useMessageDigest
that utilizes the
specified algorithmNoSuchAlgorithmException
- if the specified algorithm is not availableNullPointerException
- if algorithm
is null
public static MessageDigest getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException
MessageDigest
that utilizes the
specified algorithm from the specified provider.algorithm
- the name of the algorithm to useprovider
- the name of the providerMessageDigest
that utilizes the
specified algorithm from the specified providerNoSuchAlgorithmException
- if the specified algorithm is not availableNoSuchProviderException
- if the specified provider is not availableNullPointerException
- if algorithm
is null
IllegalArgumentException
- if provider == null || provider.isEmpty()
public static MessageDigest getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException
MessageDigest
that utilizes the
specified algorithm from the specified provider.algorithm
- the name of the algorithm to useprovider
- the providerMessageDigest
that utilizes the
specified algorithm from the specified providerNoSuchAlgorithmException
- if the specified algorithm is not availableNullPointerException
- if algorithm
is null
IllegalArgumentException
- if provider == null
public void reset()
MessageDigest
back in an initial state, such that it is
ready to compute a one way hash value.public void update(byte arg0)
MessageDigest
using the given byte
.arg0
- the byte
to update this MessageDigest
withreset()
public void update(byte[] input, int offset, int len)
MessageDigest
using the given byte[]
.input
- the byte
arrayoffset
- the index of the first byte in input
to update fromlen
- the number of bytes in input
to update fromIllegalArgumentException
- if offset
or len
are not valid in respect to
input
public void update(byte[] input)
MessageDigest
using the given byte[]
.input
- the byte
arrayNullPointerException
- if input
is null
public byte[] digest()
MessageDigest
.
After the digest is computed the receiver is reset.reset()
public int digest(byte[] buf, int offset, int len) throws DigestException
MessageDigest
.
After the digest is computed the receiver is reset.buf
- the buffer to store the resultoffset
- the index of the first byte in buf
to storelen
- the number of bytes allocated for the digestbuf
DigestException
- if an error occursIllegalArgumentException
- if offset
or len
are not valid in respect to
buf
reset()
public byte[] digest(byte[] input)
MessageDigest
. After the digest is computed the
receiver is reset.input
- the byte
arrayreset()
public String toString()
MessageDigest
including the name of its algorithm.public static boolean isEqual(byte[] digesta, byte[] digestb)
digesta
- the first digest to be compareddigestb
- the second digest to be comparedtrue
if the two hashes are equal, false
otherwisepublic final String getAlgorithm()
MessageDigest
.MessageDigest
public final Provider getProvider()
MessageDigest
.MessageDigest
public final int getDigestLength()
Cloneable
,
0
is returned.0
public Object clone() throws CloneNotSupportedException
Object
Object
. 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 call super.clone()
to create the new instance and then create deep copies of the nested,
mutable objects.clone
in class MessageDigestSpi
CloneNotSupportedException
- if this object's class does not implement the Cloneable
interface.public final void update(ByteBuffer input)
MessageDigest
using the given input
.input
- the ByteBuffer