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("Usage:");
         System.out.println("java JcaMessageDigest input output"
            +" algorithm");
         return;
      }
      String input = a[0];
      String output = a[1];
      String algorithm = a[2]; // SHA, MD5
      try {
         digest(input,output,algorithm);
      } catch (Exception e) {
         System.out.println("Exception: "+e);
         return;
      }
   }
   private static void digest(String input, String output, 
      String algorithm) throws Exception {
      MessageDigest md = MessageDigest.getInstance(algorithm);
      System.out.println();
      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;
         md.update(buffer,0,n);
         n = in.read(buffer,0,bufSize);
      }
      in.close();
      FileOutputStream out = new FileOutputStream(output);
      byte[] digest = md.digest();
      out.write(digest);
      out.close();
      System.out.println();
      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:

>javac -classpath . JcaMessageDigest.java

>java -cp . JcaMessageDigest JcaMessageDigest.class digest.sha SHA
MessageDigest Object Info:
Algorithm = SHA
Provider = SUN version 1.8
Digest Length = 20
toString = SHA Message Digest from SUN, <initialized>


Message Digest Processing Info:
Number of input bytes = 2217
Number of output bytes = 20

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

The program seems to be working:

Last update: 2014.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

 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

 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 - Secret Key Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials

 References

 PDF Printing Version