JcaMessageDigest.java - Message Digest Sample Program

This section provides a tutorial example on how to write a sample program to use the java.security.MessageDigest class to digest any input file with the MD5 or SHA message digest algorithm.

The following sample program shows you how to invoke the message digest algorithms implemented by the default provider, Sun, and digest the data from the input file.

/* JcaMessageDigest.java
 * Copyright (c) 2014 HerongYang.com, All Rights Reserved.
import java.io.*;
import java.security.*;
class JcaMessageDigest {
   public static void main(String[] a) {
      if (a.length<3) {
         System.out.println("java JcaMessageDigest input output"
            +" algorithm");
      String input = a[0];
      String output = a[1];
      String algorithm = a[2]; // SHA, MD5
      try {
      } catch (Exception e) {
         System.out.println("Exception: "+e);
   private static void digest(String input, String output, 
      String algorithm) throws Exception {
      MessageDigest md = MessageDigest.getInstance(algorithm);
      System.out.println("MessageDigest Object Info: ");
      System.out.println("Algorithm = "+md.getAlgorithm());
      System.out.println("Provider = "+md.getProvider());
      System.out.println("Digest Length = "+md.getDigestLength());
      System.out.println("toString = "+md.toString());
      FileInputStream in = new FileInputStream(input);
      int bufSize = 1024;
      byte[] buffer = new byte[bufSize];
      int n = in.read(buffer,0,bufSize);
      int count = 0;
      while (n!=-1) {
         count += n;
         n = in.read(buffer,0,bufSize);
      FileOutputStream out = new FileOutputStream(output);
      byte[] digest = md.digest();
      System.out.println("Message Digest Processing Info: ");
      System.out.println("Number of input bytes = "+count);
      System.out.println("Number of output bytes = "+digest.length);

Here is the result of my first test. It was done with JDK 1.8:

C:\herong>javac JcaMessageDigest.java

C:\herong>java JcaMessageDigest JcaMessageDigest.class digest.sha SHA
MessageDigest Object Info:
Algorithm = SHA
Provider = SUN version 10
Digest Length = 20
toString = SHA Message Digest from SUN, <initialized>

>dir *.class *.sha
   2,555 JcaMessageDigest.class
      20 digest.sha

The program seems to be working:

Last update: 2014.

Table of Contents

 About This JDK Tutorial Book

 JDK (Java Development Kit)

 Java Date-Time API

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

 Locales, Localization Methods and Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Java Logging

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

Message Digest Algorithm Implementations in JDK

 java.security.MessageDigest - Message Digest MD5 and SHA

JcaMessageDigest.java - Message Digest Sample Program

 Comparing Message Digest Output of MD5 and SHA

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials


 Full Version in PDF/EPUB