package com.gh.common.util;

import java.io.ByteArrayInputStream;
import java.math.BigInteger;
import java.security.MessageDigest;

public class MD5Utils {

	public static String getUpdateMD5(String url, String content) {
		if (url == null && content == null) {
			return null;
		}
		MessageDigest digest = null;
		ByteArrayInputStream bais = null;
		byte buffer[] = new byte[1024];
		int len;
		try {
			digest = MessageDigest.getInstance("MD5");
			bais = new ByteArrayInputStream((url + content).getBytes());
			while ((len = bais.read(buffer, 0, 1024)) != -1) {
				digest.update(buffer, 0, len);
			}
			bais.close();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		BigInteger bigInt = new BigInteger(1, digest.digest());
		return bigInt.toString(16);
	}

	public static String getUrlMD5(String url) {
		if (url == null) {
			return null;
		}
		MessageDigest digest = null;
		ByteArrayInputStream bais = null;
		byte buffer[] = new byte[1024];
		int len;
		try {
			digest = MessageDigest.getInstance("MD5");
			bais = new ByteArrayInputStream(url.getBytes());
			while ((len = bais.read(buffer, 0, 1024)) != -1) {
				digest.update(buffer, 0, len);
			}
			bais.close();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		BigInteger bigInt = new BigInteger(1, digest.digest());
		return bigInt.toString(16);
	}

	public static String getContentMD5(String content) {
		if (content == null) {
			return null;
		}
		MessageDigest digest = null;
		ByteArrayInputStream bais = null;
		byte buffer[] = new byte[1024];
		int len;
		try {
			digest = MessageDigest.getInstance("MD5");
			bais = new ByteArrayInputStream(content.getBytes());
			while ((len = bais.read(buffer, 0, 1024)) != -1) {
				digest.update(buffer, 0, len);
			}
			bais.close();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		BigInteger bigInt = new BigInteger(1, digest.digest());
		return bigInt.toString(16);
	}

}
