MD5工具类

可直接使用:

package dev;

import java.security.*;

public class MyMD5Util {
	public MyMD5Util() {}

	public static String func(String str) {
		try {
			byte[] byteStr = str.getBytes();
			MessageDigest digest = MessageDigest.getInstance("MD5");
			digest.update(byteStr);
			byte[] result = digest.digest();
			StringBuffer bf = new StringBuffer();
			for (int i = 0; i < result.length; i++) {
				int val = ((int) result[i]) & 0xff;
				if (val < 16) {
					bf.append("0");
				}
				bf.append(Integer.toHexString(val));
			}
			return bf.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String[] ss) {
//		MyMD5Util md = new MyMD5Util();
		String str1 = "123456mmdd343";
		String result1 = MyMD5Util.func(str1);
		System.out.println(result1);
		
		String str2 = "123456mmdd343";
		String result2 = MyMD5Util.func(str2);
		System.out.println(result2);
	}

}





你可能感兴趣的:(MD5工具类)