java MD5加密工具类

package cn.util;

import java.security.MessageDigest;

/**
 * MD5加密工具类
 * @author zyq
 *
 */
public class MD5Util {
	
	public static String encode(String plainText) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte b[] = md.digest();

			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			return buf.toString();
			//System.out.println("result: " + buf.toString());// 32位的加密
			//System.out.println("result: " + buf.toString().substring(8, 24));// 16位的加密

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	public static void main(String args[]) {
		String str = "abc";
		Long b1 = System.currentTimeMillis();
		
		for(int i =0;i<1;i++){
			//MD5.encode(str);
			System.out.println(MD5Util.encode(str)); //900150983cd24fb0d6963f7d28e17f72
			
//			Md5PasswordEncoder md5 = new Md5PasswordEncoder();
//			//md5.setEncodeHashAsBase64(false);
//			//md5.encodePassword(str, "");
//			System.out.println(md5.encodePassword(str, ""));
		}
		Long e1 = System.currentTimeMillis();
		System.out.println("MD5.encode耗时:"+(e1-b1));
	}

}

 

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