Comparing Message Digest Output of MD5 and SHA

This section a tutorial example on how to view and compare message digest output from two standard message digest algorithms MD5 and SHA.

The message digest output file contains binary data, which cannot be viewed directly. In order to see the digest data, I need to use my other program, HexWriter.java, to convert binary data to hex numbers. See chapter "Encoding Conversion" for details.

Here is how to look at the digest data in hex numbers, 16 bytes per line:

herong> javac HexWriter.java

herong> java HexWriter digest.sha digest_sha.hex

herong> more digest_sha.hex
0520E172B3E334C9BDD92D7ADBC98F1C
BD4162D0

My second test is to see the digest data of the same input data, JcaMessageDigest.class, using MD5 algorithm:

herong> java JcaMessageDigest JcaMessageDigest.class digest.md5 MD5

MessageDigest Object Info:
Algorithm = MD5
Provider = SUN version 10
Digest Length = 16
toString = MD5 Message Digest from SUN, <initialized>

Message Digest Processing Info:
Number of input bytes = 2555
Number of output bytes = 16

herong> java HexWriter digest.md5 digest_md5.hex

herong> more digest_md5.hex
7DA3B53BBEC296D835DC58A1AD5FA664

My third test is to see how the digest data changes if I add one byte to the input data:

herong> copy con dot.tmp
.^Z

herong> copy JcaMessageDigest.class JcaMessageDigest_1.class

herong> more dot.tmp >> JcaMessageDigest_1.class

herong> dir *.class
   2,217 JcaMessageDigest.class
   2,218 JcaMessageDigest_1.class

herong> java JcaMessageDigest JcaMessageDigest_1.class digest_1.md5 MD5

MessageDigest Object Info:
Algorithm = MD5
Provider = SUN version 1.8
Digest Length = 16
toString = MD5 Message Digest from SUN, <initialized>

Message Digest Processing Info:
Number of input bytes = 2218
Number of output bytes = 16

herong> java HexWriter digest_1.md5 digest_1_md5.hex

herong> more digest_1_md5.hex
31B82738B990A2ECDA63547B660CA0F0

A couple of interesting notes here:

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

 References

 Full Version in PDF/EPUB