Generating a MD5 message digest

What MD5 is and why it it important is explained well on the following page:

http://www.enteract.com/~lspitz/md5.html

A digest has two properties:
- it is computationally infeasible to find another input string that generates the same digest
- the digest does not reveal anything about the input that was used to generate it.

The engine class MessageDigest provides the functionality of several
types of cryptographic algorithms. What algorithms are available depends
on the provider you are using. For example, Sun (the default provider) supplies
implementations of the MD5 and SHA-1 message digest algorithms. Other providers
may have their own implementation of these algorithms or other algorithms.

As an implementation is supplied by Sun in the JDK, you don’t even have to
install another provider. Here’s is an example using the implementation by Cryptix:

 
import cryptix.util.core.Hex;
import cryptix.util.test.BaseTest;
import java.security.MessageDigest;
 
class TestMD5 
{
   public static void main(String[] args) {
      String plaintext = "The sun is green and the grass shines";

      MessageDigest md = null;
      try {
         // first create an instance, given the provider
         md = MessageDigest.getInstance("MD5", "Cryptix");
      }
      catch(java.security.NoSuchProviderException e) {
         System.out.println(e);
         System.exit(1);
      }
      catch(java.security.NoSuchAlgorithmException e) {
         System.out.println(e);
         System.exit(1);
      }

      md.reset();

      // call the update method one or more times 
      // (useful when you don't know the size of your data, eg. stream)
      md.update(plaintext.getBytes());

      // now calculate the hash
      byte[] hash = md.digest();

      System.out.println("Plaintext:nt" + plaintext + "n");
      // Hex is a function provided by Cryptix to produce a nice output
      System.out.println("Message Digest:nt" + Hex.toString(hash));
   }
}

generates the output:

 
Plaintext:
	The sun is green and the grass shines

Message Digest:
	470DB3B23F9D445446DFCE291C9F17AB