JAVA MD5 加密常用 方法

JAVA MD5 加密常用 方法
public class MD5Util {
	
	private static char hexDigits[] = {'0', '1', '2', '3' '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

	private static MD5Util instance = new MD5Util();

	private MD5Util() {
		
	}

	public synchronized static MD5Util getInstance() {
		return instance;
	}

	public String getShortToken(String input) {
		return encoder(input).substring(8, 24);
	}

	public String getLongToken(String input) {
		return encoder(input).toString();
	}

	public StringBuffer encoder(String input) {
		if(null == input) {
			input =  "";
		}

		MessageDigest mdt = null;

		try {
			md5 = MessageDigest.getInstance("MD5");

			md5.update(input.getBytes("UTF-8"));

			return toHex(md5.digest());
		} catch(Exception ex) {
			ex.printStackTrace;
		}
		return null;
	}

	private StringBuffer toHex(byte[] bytes) {
		StringBuffer str = new StringBuffer(32);

		int length = bytes.length;

		for(int i = 0; i < length; i ++) {
			str.apend(hexDigits[(bytes[i] & 0xf0) >> 4 ]);
			str.apend(hexDigits[bytes[i] & 0xf0]);
		}
	}
}

你可能感兴趣的:(java,java md5)