MD5

From Rosetta Code
Revision as of 07:14, 19 November 2007 by rosettacode>Mwn3d (Created page and added Java example from www.mindprod.com.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
MD5
You are encouraged to solve this task according to the task description, using any language you may know.

Encode a string using an MD5 algorithm. The algorithm can be found on the wiki.


Java

Modified from mindprod's Java Glossary:

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Test MD5 digest computation
 *
 * @author Roedy Green
 * @version 1.0
 * @since 2004-06-07
 */
public final class MD5{
	public static void main(String[] args) throws UnsupportedEncodingException,
			NoSuchAlgorithmException{
		byte[] theTextToDigestAsBytes=
				"The quick brown fox jumped over the lazy dog's back"
						.getBytes("8859_1");
		MessageDigest md= MessageDigest.getInstance("MD5");
		md.update(theTextToDigestAsBytes);
		byte[] digest= md.digest();

		// dump out the hash
		for(byte b: digest){
			System.out.print(Integer.toHexString(b & 0xff).toUpperCase());
		}
	}
}