Java tutorial
//package com.java2s; //License from project: Open Source License import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Main { /** * @param s String to MD5 hash * @return MD5 hashed string */ public static String md5(String s) { if (s == null) { return ""; } try { // Create MD5 Hash MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); digest.update(s.getBytes()); byte[] messageDigest = digest.digest(); return bytesToHex(messageDigest); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return ""; } /** * @param data Byte array to convert to hex * @return Hex string */ public static String bytesToHex(byte[] data) { if (data == null) { return null; } int len = data.length; String str = ""; for (int i = 0; i < len; i++) { if ((data[i] & 0xFF) < 16) { str = str + "0" + java.lang.Integer.toHexString(data[i] & 0xFF); } else { str = str + java.lang.Integer.toHexString(data[i] & 0xFF); } } return str; } }