Skip to content

Most visited

Recently visited

navigation
Added in API level 1

MessageDigest

public abstract class MessageDigest
extends MessageDigestSpi

java.lang.Object
   ↳ java.security.MessageDigestSpi
     ↳ java.security.MessageDigest


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:

Summary

Protected constructors

MessageDigest(String algorithm)

Constructs a new instance of MessageDigest with the name of the algorithm to use.

Public methods

int digest(byte[] buf, int offset, int len)

Computes and stores the final hash value for this MessageDigest.

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.

final String getAlgorithm()

Returns the name of the algorithm of this MessageDigest.

final int getDigestLength()

Returns the engine digest length in bytes.

static MessageDigest getInstance(String algorithm, String provider)

Returns a new instance of MessageDigest that utilizes the specified algorithm from the specified provider.

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)

Returns a new instance of MessageDigest that utilizes the specified algorithm.

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

final void update(ByteBuffer input)

Updates this MessageDigest using the given input.

void update(byte[] input, int offset, int len)

Updates this MessageDigest using the given byte[].

void update(byte arg0)

Updates this MessageDigest using the given byte.

void update(byte[] input)

Updates this MessageDigest using the given byte[].

Inherited methods

From class java.security.MessageDigestSpi
From class java.lang.Object

Protected constructors

MessageDigest

Added in API level 1
MessageDigest (String algorithm)

Constructs a new instance of MessageDigest with the name of the algorithm to use.

Parameters
algorithm String: the name of algorithm to use

Public methods

digest

Added in API level 1
int digest (byte[] buf, 
                int offset, 
                int len)

Computes and stores the final hash value for this MessageDigest. After the digest is computed the receiver is reset.

Parameters
buf byte: the buffer to store the result
offset int: the index of the first byte in buf to store
len int: the number of bytes allocated for the digest
Returns
int the number of bytes written to buf
Throws
DigestException if an error occurs
IllegalArgumentException if offset or len are not valid in respect to buf

See also:

digest

Added in API level 1
byte[] digest ()

Computes and returns the final hash value for this MessageDigest. After the digest is computed the receiver is reset.

Returns
byte[] the computed one way hash value

See also:

digest

Added in API level 1
byte[] digest (byte[] input)

Performs the final update and then computes and returns the final hash value for this MessageDigest. After the digest is computed the receiver is reset.

Parameters
input byte: the byte array
Returns
byte[] the computed one way hash value

See also:

getAlgorithm

Added in API level 1
String getAlgorithm ()

Returns the name of the algorithm of this MessageDigest.

Returns
String the name of the algorithm of this MessageDigest

getDigestLength

Added in API level 1
int getDigestLength ()

Returns the engine digest length in bytes. If the implementation does not implement this function or is not an instance of Cloneable, 0 is returned.

Returns
int the digest length in bytes, or 0

getInstance

Added in API level 1
MessageDigest getInstance (String algorithm, 
                String provider)

Returns a new instance of MessageDigest that utilizes the specified algorithm from the specified provider.

Parameters
algorithm String: the name of the algorithm to use
provider String: the name of the provider
Returns
MessageDigest a new instance of MessageDigest that utilizes the specified algorithm from the specified provider
Throws
NoSuchAlgorithmException if the specified algorithm is not available
NoSuchProviderException if the specified provider is not available
NullPointerException if algorithm is null
IllegalArgumentException if provider == null || provider.isEmpty()

getInstance

Added in API level 1
MessageDigest getInstance (String algorithm, 
                Provider provider)

Returns a new instance of MessageDigest that utilizes the specified algorithm from the specified provider. The provider supplied does not have to be registered.

Parameters
algorithm String: the name of the algorithm to use
provider Provider: the provider
Returns
MessageDigest a new instance of MessageDigest that utilizes the specified algorithm from the specified provider
Throws
NoSuchAlgorithmException if the specified algorithm is not available
NullPointerException if algorithm is null
IllegalArgumentException if provider == null

getInstance

Added in API level 1
MessageDigest getInstance (String algorithm)

Returns a new instance of MessageDigest that utilizes the specified algorithm.

Parameters
algorithm String: the name of the algorithm to use
Returns
MessageDigest a new instance of MessageDigest that utilizes the specified algorithm
Throws
NoSuchAlgorithmException if the specified algorithm is not available
NullPointerException if algorithm is null

getProvider

Added in API level 1
Provider getProvider ()

Returns the provider associated with this MessageDigest.

Returns
Provider the provider associated with this MessageDigest

isEqual

Added in API level 1
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 byte: the first digest to be compared
digestb byte: the second digest to be compared
Returns
boolean true if the two hashes are equal, false otherwise

reset

Added in API level 1
void reset ()

Puts this MessageDigest back in an initial state, such that it is ready to compute a one way hash value.

toString

Added in API level 1
String toString ()

Returns a string containing a concise, human-readable description of this MessageDigest including the name of its algorithm.

Returns
String a printable representation for this MessageDigest

update

Added in API level 1
void update (ByteBuffer input)

Updates this MessageDigest using the given input.

Parameters
input ByteBuffer: the ByteBuffer

update

Added in API level 1
void update (byte[] input, 
                int offset, 
                int len)

Updates this MessageDigest using the given byte[].

Parameters
input byte: the byte array
offset int: the index of the first byte in input to update from
len int: the number of bytes in input to update from
Throws
IllegalArgumentException if offset or len are not valid in respect to input

update

Added in API level 1
void update (byte arg0)

Updates this MessageDigest using the given byte.

Parameters
arg0 byte: the byte to update this MessageDigest with

See also:

update

Added in API level 1
void update (byte[] input)

Updates this MessageDigest using the given byte[].

Parameters
input byte: the byte array
Throws
NullPointerException if input is null
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.