package com.dw.lib;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.util.UUID;

public class MD5Generator {

	public String generateValue() throws Exception {
		return generateValue(UUID.randomUUID().toString());
	}

	private static final char[] hexCode = "0123456789abcdef".toCharArray();

	public static String toHexString(byte[] data) {
		if (data == null) {
			return null;
		}
		StringBuilder r = new StringBuilder(data.length * 2);
		for (byte b : data) {
			r.append(hexCode[(b >> 4) & 0xF]);
			r.append(hexCode[(b & 0xF)]);
		}
		return r.toString();
	}

	public String generateValue(String param) throws Exception {
		try {
			MessageDigest algorithm = MessageDigest.getInstance("MD5");
			algorithm.reset();
			algorithm.update(param.getBytes());
			byte[] messageDigest = algorithm.digest();
			return toHexString(messageDigest);
		} catch (Exception e) {
			throw new Exception("OAuth Token cannot be generated.", e);
		}
	}
	
	public String generateFileValue(String filePath) throws Exception {
		try {
			MessageDigest algorithm = MessageDigest.getInstance("MD5");
			algorithm.reset();
			algorithm.update(Files.readAllBytes(Paths.get(filePath)));
			byte[] messageDigest = algorithm.digest();
			return toHexString(messageDigest);
		} catch (Exception e) {
			throw new Exception("Generate MD5 of file show exceptions: ", e);
		}
	}
	
	public static void main(String[] args) {
		
	}
}

@@ -1446,14 +1446,270 @@
 gs) %7B%0A%09%09
+MD5Generator m = new MD5Generator();%0A%09%09try %7B%0A%09%09%09System.out.println(m.generateFileValue(%22/Users/di.wu/test/1.json%22));%0A%09%09%09System.out.println(m.generateValue(%221234%22));%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09// TODO Auto-generated catch block%0A%09%09%09e.printStackTrace();%0A%09%09%7D
 %0A%09%7D%0A%7D%0A

add file path and string test