Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Protected Ctors](https://developer.android.com/reference/java/security/MessageDigest#proctors) \| [Methods](https://developer.android.com/reference/java/security/MessageDigest#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/java/security/MessageDigest#inhmethods)

# MessageDigest


`
public


abstract
class
MessageDigest
`


`


`

`

extends https://developer.android.com/reference/java/security/MessageDigestSpi


`

`


`

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) |||
| ↳ | [java.security.MessageDigestSpi](https://developer.android.com/reference/java/security/MessageDigestSpi) ||
|   | ↳ | java.security.MessageDigest |

<br />

*** ** * ** ***

This 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 `https://developer.android.com/reference/java/security/MessageDigest#update(byte)`
methods. At any point `https://developer.android.com/reference/java/security/MessageDigest#reset()` can be called
to reset the digest. Once all the data to be updated has been
updated, one of the `https://developer.android.com/reference/java/security/MessageDigest#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.

Android provides the following `MessageDigest` algorithms:

| Algorithm | Supported API Levels |
|---|---|
| MD5 | 1+ |
| SHA-1 | 1+ |
| SHA-224 | 1-8,22+ |
| SHA-256 | 1+ |
| SHA-384 | 1+ |
| SHA-512 | 1+ |

These algorithms are described in the [MessageDigest section](https://docs.oracle.com/en/java/javase/17/docs/specs/security/standard-names.html#messagedigest-algorithms) of the Java Cryptography Architecture Standard Algorithm Name Documentation.

<br />

**See also:**

- `https://developer.android.com/reference/java/security/DigestInputStream`
- `https://developer.android.com/reference/java/security/DigestOutputStream`

## Summary

| ### Protected constructors ||
|---|---|
| ` https://developer.android.com/reference/java/security/MessageDigest#MessageDigest(java.lang.String)(https://developer.android.com/reference/java/lang/String algorithm) ` Creates a message digest with the specified algorithm name. |

| ### Public methods ||
|---|---|
| ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/security/MessageDigest#clone()() ` Returns a clone if the implementation is cloneable. |
| ` int` | ` https://developer.android.com/reference/java/security/MessageDigest#digest(byte[],%20int,%20int)(byte[] buf, int offset, int len) ` Completes the hash computation by performing final operations such as padding. |
| ` byte[]` | ` https://developer.android.com/reference/java/security/MessageDigest#digest()() ` Completes the hash computation by performing final operations such as padding. |
| ` byte[]` | ` https://developer.android.com/reference/java/security/MessageDigest#digest(byte[])(byte[] input) ` Performs a final update on the digest using the specified array of bytes, then completes the digest computation. |
| ` final https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/security/MessageDigest#getAlgorithm()() ` Returns a string that identifies the algorithm, independent of implementation details. |
| ` final int` | ` https://developer.android.com/reference/java/security/MessageDigest#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. |
| ` static https://developer.android.com/reference/java/security/MessageDigest` | ` https://developer.android.com/reference/java/security/MessageDigest#getInstance(java.lang.String)(https://developer.android.com/reference/java/lang/String algorithm) ` Returns a MessageDigest object that implements the specified digest algorithm. |
| ` static https://developer.android.com/reference/java/security/MessageDigest` | ` https://developer.android.com/reference/java/security/MessageDigest#getInstance(java.lang.String,%20java.lang.String)(https://developer.android.com/reference/java/lang/String algorithm, https://developer.android.com/reference/java/lang/String provider) ` Returns a MessageDigest object that implements the specified digest algorithm. |
| ` static https://developer.android.com/reference/java/security/MessageDigest` | ` https://developer.android.com/reference/java/security/MessageDigest#getInstance(java.lang.String,%20java.security.Provider)(https://developer.android.com/reference/java/lang/String algorithm, https://developer.android.com/reference/java/security/Provider provider) ` Returns a MessageDigest object that implements the specified digest algorithm. |
| ` final https://developer.android.com/reference/java/security/Provider` | ` https://developer.android.com/reference/java/security/MessageDigest#getProvider()() ` Returns the provider of this message digest object. |
| ` static boolean` | ` https://developer.android.com/reference/java/security/MessageDigest#isEqual(byte[],%20byte[])(byte[] digesta, byte[] digestb) ` Compares two digests for equality. |
| ` void` | ` https://developer.android.com/reference/java/security/MessageDigest#reset()() ` Resets the digest for further use. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/security/MessageDigest#toString()() ` Returns a string representation of this message digest object. |
| ` void` | ` https://developer.android.com/reference/java/security/MessageDigest#update(byte[])(byte[] input) ` Updates the digest using the specified array of bytes. |
| ` void` | ` https://developer.android.com/reference/java/security/MessageDigest#update(byte[],%20int,%20int)(byte[] input, int offset, int len) ` Updates the digest using the specified array of bytes, starting at the specified offset. |
| ` final void` | ` https://developer.android.com/reference/java/security/MessageDigest#update(java.nio.ByteBuffer)(https://developer.android.com/reference/java/nio/ByteBuffer input) ` Update the digest using the specified ByteBuffer. |
| ` void` | ` https://developer.android.com/reference/java/security/MessageDigest#update(byte)(byte input) ` Updates the digest using the specified byte. |

| ### Inherited methods |
|---|---|
| From class ` https://developer.android.com/reference/java/security/MessageDigestSpi ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/security/MessageDigestSpi#clone()() ` Returns a clone if the implementation is cloneable. | | ` int` | ` https://developer.android.com/reference/java/security/MessageDigestSpi#engineDigest(byte[],%20int,%20int)(byte[] buf, int offset, int len) ` Completes the hash computation by performing final operations such as padding. | | ` abstract byte[]` | ` https://developer.android.com/reference/java/security/MessageDigestSpi#engineDigest()() ` Completes the hash computation by performing final operations such as padding. | | ` int` | ` https://developer.android.com/reference/java/security/MessageDigestSpi#engineGetDigestLength()() ` Returns the digest length in bytes. | | ` abstract void` | ` https://developer.android.com/reference/java/security/MessageDigestSpi#engineReset()() ` Resets the digest for further use. | | ` void` | ` https://developer.android.com/reference/java/security/MessageDigestSpi#engineUpdate(java.nio.ByteBuffer)(https://developer.android.com/reference/java/nio/ByteBuffer input) ` Update the digest using the specified ByteBuffer. | | ` abstract void` | ` https://developer.android.com/reference/java/security/MessageDigestSpi#engineUpdate(byte)(byte input) ` Updates the digest using the specified byte. | | ` abstract void` | ` https://developer.android.com/reference/java/security/MessageDigestSpi#engineUpdate(byte[],%20int,%20int)(byte[] input, int offset, int len) ` Updates the digest using the specified array of bytes, starting at the specified offset. | ||
| From class ` https://developer.android.com/reference/java/lang/Object ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Object#clone()() ` Creates and returns a copy of this object. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this one. | | ` void` | ` https://developer.android.com/reference/java/lang/Object#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | | ` final https://developer.android.com/reference/java/lang/Class<?>` | ` https://developer.android.com/reference/java/lang/Object#getClass()() ` Returns the runtime class of this `Object`. | | ` int` | ` https://developer.android.com/reference/java/lang/Object#hashCode()() ` Returns a hash code value for the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notify()() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notifyAll()() ` Wakes up all threads that are waiting on this object's monitor. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Object#toString()() ` Returns a string representation of the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait()() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*. | ||

## Protected constructors

### MessageDigest

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected MessageDigest (String algorithm)
```

Creates a message digest with the specified algorithm name.

<br />

| Parameters ||
|---|---|
| `algorithm` | `String`: the standard name of the digest algorithm. See the MessageDigest section in the [Java Security Standard Algorithm Names Specification](https://docs.oracle.com/en/java/javase/17/docs/specs/security/standard-names.html#messagedigest-algorithms) for information about standard algorithm names. <br /> |

## Public methods

### clone

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Object clone ()
```

Returns a clone if the implementation is cloneable.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/Object` | a clone if the implementation is cloneable. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/CloneNotSupportedException` | if this is called on an implementation that does not support `Cloneable`. |

### digest

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

Completes the hash computation by performing final operations
such as padding. The digest is reset after this call is made.

<br />

| Parameters ||
|---|---|
| `buf` | `byte`: output buffer for the computed digest <br /> |
| `offset` | `int`: offset into the output buffer to begin storing the digest <br /> |
| `len` | `int`: number of bytes within buf allotted for the digest <br /> |

| Returns ||
|---|---|
| `int` | the number of bytes placed into `buf` <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/security/DigestException` | if an error occurs. |

### digest

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public byte[] digest ()
```

Completes the hash computation by performing final operations
such as padding. The digest is reset after this call is made.

<br />

| Returns ||
|---|---|
| `byte[]` | the array of bytes for the resulting hash value. <br /> |

### digest

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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 `https://developer.android.com/reference/java/security/MessageDigest#update(byte[])`,
passing the *input* array to the `update` method,
then calls `https://developer.android.com/reference/java/security/MessageDigest#digest()`.

<br />

| Parameters ||
|---|---|
| `input` | `byte`: the input to be updated before the digest is completed. <br /> |

| Returns ||
|---|---|
| `byte[]` | the array of bytes for the resulting hash value. <br /> |

### getAlgorithm

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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](https://docs.oracle.com/en/java/javase/17/docs/specs/security/standard-names.html#messagedigest-algorithms)
for information about standard algorithm names.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | the name of the algorithm <br /> |

### getDigestLength

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

<br />

| Returns ||
|---|---|
| `int` | the digest length in bytes, or 0 if this operation is not supported by the provider and the implementation is not cloneable. <br /> |

### getInstance

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static MessageDigest getInstance (String algorithm)
```

Returns a MessageDigest object that implements the specified digest
algorithm.

This method traverses the list of registered security Providers,
starting with the most preferred Provider.
A new MessageDigest object encapsulating the
MessageDigestSpi implementation from the first
Provider that supports the specified algorithm is returned.

Note that the list of registered providers may be retrieved via
the `https://developer.android.com/reference/java/security/Security#getProviders()` method.

<br />

##### Implementation Note:

- The JDK Reference Implementation additionally uses the `jdk.security.provider.preferred` `https://developer.android.com/reference/java/security/Security#getProperty(java.lang.String)` property to determine the preferred provider order for the specified algorithm. This may be different than the order of providers returned by `https://developer.android.com/reference/java/security/Security#getProviders()`.

| Parameters ||
|---|---|
| `algorithm` | `String`: the name of the algorithm requested. See the MessageDigest section in the [Java Security Standard Algorithm Names Specification](https://docs.oracle.com/en/java/javase/17/docs/specs/security/standard-names.html#messagedigest-algorithms) for information about standard algorithm names. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/security/MessageDigest` | a `MessageDigest` object that implements the specified algorithm <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if `algorithm` is `null` |
| `https://developer.android.com/reference/java/security/NoSuchAlgorithmException` | if no `Provider` supports a `MessageDigestSpi` implementation for the specified algorithm |

**See also:**

- `https://developer.android.com/reference/java/security/Provider`

### getInstance

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static MessageDigest getInstance (String algorithm, 
                String provider)
```

Returns a MessageDigest object that implements the specified digest
algorithm.

A new MessageDigest object encapsulating the
MessageDigestSpi implementation from the specified provider
is returned. The specified provider must be registered
in the security provider list.

Note that the list of registered providers may be retrieved via
the `https://developer.android.com/reference/java/security/Security#getProviders()` method.

<br />

| Parameters ||
|---|---|
| `algorithm` | `String`: the name of the algorithm requested. See the MessageDigest section in the [Java Security Standard Algorithm Names Specification](https://docs.oracle.com/en/java/javase/17/docs/specs/security/standard-names.html#messagedigest-algorithms) for information about standard algorithm names. <br /> |
| `provider` | `String`: the name of the provider. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/security/MessageDigest` | a `MessageDigest` object that implements the specified algorithm <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the provider name is `null` or empty |
| `https://developer.android.com/reference/java/lang/NullPointerException` | if `algorithm` is `null` |
| `https://developer.android.com/reference/java/security/NoSuchAlgorithmException` | if a `MessageDigestSpi` implementation for the specified algorithm is not available from the specified provider |
| `https://developer.android.com/reference/java/security/NoSuchProviderException` | if the specified provider is not registered in the security provider list |

**See also:**

- `https://developer.android.com/reference/java/security/Provider`

### getInstance

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static MessageDigest getInstance (String algorithm, 
                Provider provider)
```

Returns a MessageDigest object that implements the specified digest
algorithm.

A new MessageDigest object encapsulating the
MessageDigestSpi implementation from the specified Provider
object is returned. Note that the specified Provider object
does not have to be registered in the provider list.

<br />

| Parameters ||
|---|---|
| `algorithm` | `String`: the name of the algorithm requested. See the MessageDigest section in the [Java Security Standard Algorithm Names Specification](https://docs.oracle.com/en/java/javase/17/docs/specs/security/standard-names.html#messagedigest-algorithms) for information about standard algorithm names. <br /> |
| `provider` | `Provider`: the provider. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/security/MessageDigest` | a `MessageDigest` object that implements the specified algorithm <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the specified provider is `null` |
| `https://developer.android.com/reference/java/lang/NullPointerException` | if `algorithm` is `null` |
| `https://developer.android.com/reference/java/security/NoSuchAlgorithmException` | if a `MessageDigestSpi` implementation for the specified algorithm is not available from the specified `Provider` object |

**See also:**

- `https://developer.android.com/reference/java/security/Provider`

### getProvider

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final Provider getProvider ()
```

Returns the provider of this message digest object.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/security/Provider` | the provider of this message digest object <br /> |

### isEqual

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

<br />

##### Implementation Note:

- All bytes in `digesta` are examined to determine equality. The calculation time depends only on the length of `digesta`. It does not depend on the length of `digestb` or the contents of `digesta` and `digestb`.

| Parameters ||
|---|---|
| `digesta` | `byte`: one of the digests to compare. <br /> |
| `digestb` | `byte`: the other digest to compare. <br /> |

| Returns ||
|---|---|
| `boolean` | true if the digests are equal, false otherwise. <br /> |

### reset

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void reset ()
```

Resets the digest for further use.

<br />

### toString

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public String toString ()
```

Returns a string representation of this message digest object.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | a string representation of the object. <br /> |

### update

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void update (byte[] input)
```

Updates the digest using the specified array of bytes.

<br />

| Parameters ||
|---|---|
| `input` | `byte`: the array of bytes. <br /> |

### update

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

Updates the digest using the specified array of bytes, starting
at the specified offset.

<br />

| Parameters ||
|---|---|
| `input` | `byte`: the array of bytes. <br /> |
| `offset` | `int`: the offset to start from in the array of bytes. <br /> |
| `len` | `int`: the number of bytes to use, starting at `offset`. <br /> |

### update

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final void update (ByteBuffer input)
```

Update the digest using the specified ByteBuffer. The digest is
updated using the `input.remaining()` bytes starting
at `input.position()`.
Upon return, the buffer's position will be equal to its limit;
its limit will not have changed.

<br />

| Parameters ||
|---|---|
| `input` | `ByteBuffer`: the ByteBuffer <br /> |

### update

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void update (byte input)
```

Updates the digest using the specified byte.

<br />

| Parameters ||
|---|---|
| `input` | `byte`: the byte with which to update the digest. <br /> |