Java Program to MD5 Algorithm Implementation


-

-

Example:

Copied
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Algorithm {
	public static void main(String[] args) {
		System.out.println("MD5 Text: "+ md5("webisworld"));
		System.out.println("MD5 Numbers: " + md5("54321"));
	}
	public static String md5(String input) {
		String md5 = null;
		if(null == input) return null;
		try {
			// create messageDigest
			MessageDigest digest = MessageDigest.getInstance("MD5");
			// input string in message digest
			digest.update(input.getBytes(), 0, input.length());
			// base 16 hex
			md5 = new BigInteger(1, digest.digest()).toString(16);
		}
		catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return md5;
	}
}

Output:

MD5 Text: c126d8cb7b8972cb0ec38cd3c2c117c3
MD5 Numbers: 1cfcd4f6b8770febfb40cb906715822