JDK Tutorials - Herong's Tutorial Examples - v6.32, by Herong Yang
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) 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:
herong> javac JcaMessageDigest.java 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> Message Digest Processing Info: Number of input bytes = 2521 Number of output bytes = 20 herong> dir *.class *.sha 2,555 JcaMessageDigest.class 20 digest.sha
The program seems to be working:
Table of Contents
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
Encoding Conversion Programs for Encoded Text Files
Datagram Network Communication
DOM (Document Object Model) - API for XML Files
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