数字摘要(MD5)
package com.test;import java.security.MessageDigest;public class MD5 {/** * @param args */public static void main(String[] args) throws Exception {String md5 = md5("测试数据");System.out.println(md5);}public static String md5(String data) throws Exception{String md5 = "";MessageDigest md = MessageDigest.getInstance("MD5");md.update(data.getBytes());byte[] rs = md.digest();md5 = toHex(rs);return md5;}public static String toHex(byte[] rs){StringBuilder sb = new StringBuilder("");for (int i = 0; i < rs.length; i++) {int h = ((rs[i]>>4) & 0x0f);int l = rs[i] & 0x0f;sb.append(h > 9 ? (char)((h - 10) + 'a') : (char)(h + '0'));sb.append(l > 9 ? (char)((l - 10) + 'a') : (char)(l + '0'));}return sb.toString();}}