JDK (Java Development Kit) Tutorials
Dr. Herong Yang, Version 5.00

java.security.MessageDigest - Message Digest MD5 and SHA

This section describes 2 popular message digest algorithm, MD5 and SHA, implemented in JDK as part of the JCA (Java Cryptography Architecture) package.

What is message digest? Message digest is a one-way hash function that takes arbitrary-sized data and outputs a fixed-length hash value.

Known message digest algorithms are:

  • SHA - The Secure Hash Algorithm, as defined in Secure Hash Standard, NIST FIPS 180-1.
  • MD2 - The MD2 message digest algorithm as defined in RFC 1319.
  • MD5 - The MD5 message digest algorithm as defined in RFC 1321.

Message digest is supported in JDK as part of the JCA (Java Cryptography Architecture) package, which has been included in JDK since 1.1:

In JCA, the java.security.MessageDigest class is an abstract class providing a link to implementation classes of message digest algorithms provided by various security package providers. Major methods in the MessageDigest class are:

getInstance() - Returns a message digest object of the specified algorithm from the implementation of the specified provider. If provider is not specified, the default implementation is used. This is a static method.

update() - Adds more data to the current message digest object for processing.

digest() - Returns the digest data of the input data processed so far in the current message digest object. It also removes the input data received in the object.

getAlgorithm() - Returns the algorithm name of the current message digest object.

getProvider() - Returns the provider as a provider object of the current message digest object.

See the next section for a sample program of using the MessageDigest class.

Last update: 2002.

Sections in This Chapter

java.security.MessageDigest - Message Digest MD5 and SHA

JcaMessageDigest.java - Message Digest Sample Program

Comparing Message Digest Output of MD5 and SHA

Dr. Herong Yang, updated in 2008
java.security.MessageDigest - Message Digest MD5 and SHA