Home » Core Java » security » java.security.MessageDigest Example

About Bhagvan Kommadi

Bhagvan Kommadi
Bhagvan Kommadi is the Founder of Architect Corner & has around 18 years’ experience in the industry, ranging from large scale enterprise development to helping incubate software product start-ups. He has done Masters in Industrial Systems Engineering at Georgia Institute of Technology (1997) and Bachelors in Aerospace Engineering from Indian Institute of Technology, Madras (1993). He is member of IFX forum,Oracle JCP and participant in Java Community Process. He founded Quantica Computacao, the first quantum computing startup in India. Markets and Markets have positioned Quantica Computacao in ‘Emerging Companies’ section of Quantum Computing quadrants. Bhagvan has engineered and developed simulators and tools in the area of quantum technology using IBM Q, Microsoft Q# and Google QScript.

java.security.MessageDigest Example

The Java Cryptographic services include signature, message digest, cipher, mac and key stores. The MessageDigest class supports message digest algorithms – MD2, MD5,SHA-1, SHA-256,SHA-384 and SHA-512. SHA-256 is a 256-bit hash function to provide 128 bits of security against collision attacks. SHA-512 is a 512 bit hash function to provide 256 bits of security. A 384-bit hash is obtained by truncating the SHA-512 output.

Message Digests use arbitrary sized data as input and fixed length value as output. The data is provided using update methods to an initialized MessageDigest instance from a static method. The Message Digest class implements the cloneable interface.

MessageDigestClass is abstract and extends from MessageDigestSpi.The class has methods getInstance (static method) to get the instance. Message Digest is initialized and data is processed through the update methods. Reset method is called to reset the digest. The Hash computation is completed when the digest method is called after the update call.

Source Code Example

The example below shows a sample for the MessageDigest implementation and usage.

MessageDigestExample.java

package com.javacodegeeks.util.security;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author Bhagvan Kommadi
 * Message Digest Example demonstrates the usage of Message Digest
 *
 */
public class MessageDigestExample {

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		String data = "This is a message to be digested using MD5";
		MessageDigest messageDigest;
		try {
			messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.update(data.getBytes());
			byte[] messageDigestMD5 = messageDigest.digest();
			StringBuffer stringBuffer = new StringBuffer();
			for (byte bytes : messageDigestMD5) {
				stringBuffer.append(String.format("%02x", bytes & 0xff));
			}

			System.out.println("data:" + data);
			System.out.println("digestedMD5(hex):" + stringBuffer.toString());
		} catch (NoSuchAlgorithmException exception) {
			// TODO Auto-generated catch block
			exception.printStackTrace();
		}

	}

}

Output

data:This is a message to be digested using MD5
digestedMD5(hex):96d013c1a391809462fb7a2cbd0b2583
Tip
The Message Digest class is used for the generation of a digest using secure one-way hash functions.

Conclusion

The Message Digest class is used for the creation of digest using algorithms – MD2, MD5,SHA-1, SHA-256,SHA-384 and SHA-512.

Download
You can download the source code of the example here: MessageDigestExample.zip
(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of