MD5加密,不用多少,直接上代码,分为16位和32位加密两种,代码如下,望批评指正
分为加密单个字符串和加密字符串数组,debug下,不难
/** * MD5字符串加密 * * @param resource 源字符串 * @return <tt>String</tt> 加密后的MD5字符串 */ public static String md5Encryption(String resource) { if (resource == null) { resource = "null"; } String str = null; MessageDigest md; try { md = MessageDigest.getInstance("MD5"); md.update(resource.getBytes("UTF-8")); 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)); } // 32位的加密 //str = buf.toString(); // 16位的加密 str = buf.toString().substring(8,24); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return str; } /** * MD5字符串加密 * * @param resource 源字符串 * @return <tt>String</tt> 加密后的MD5字符串 */ public static String md5Encryption(String[] resource){ if(null==resource||resource.length<1){ return null; } String allMD5 = ""; for(String tempStr : resource){ allMD5 = allMD5 + md5Encryption(tempStr); } return allMD5; }
public static String converZipToString(byte[] zippedData) { ByteArrayInputStream byteInput = null; GZIPInputStream gzin = null; ByteArrayOutputStream byteOutput = null; String data = null; byte[] byteData = null; try { byte[] buf = new byte[1024]; byteInput = new ByteArrayInputStream(zippedData); gzin = new GZIPInputStream(byteInput); byteOutput = new ByteArrayOutputStream(); int num = -1; while ((num = gzin.read(buf, 0, buf.length)) != -1) { byteOutput.write(buf, 0, num); } byteData = byteOutput.toByteArray(); if (byteOutput != null) { byteOutput.flush(); byteOutput.close(); } if (byteInput != null) { byteInput.close(); } if (gzin != null) { gzin.close(); } data = new String(byteData, "UTF-8"); } catch (IOException e) { return null; } return data; }
public static List<ItemAll> removeDuplicateAtMap(List<ItemAll> list) { Map<String, ItemAll> map = new HashMap<String, ItemAll>(); for (ItemAll i : list) { // TODO 到底应该怎么去重复,需要确认 map.put(CrabmanStringUtil.md5Encryption(i.getUrl()), i); // map.put(CrabmanStringUtil.md5Encryption //(SomeUtilsAt.decode(i.getUrl())+ i.getFileTime()+ i.getKeyWord() + i.getSearchType()), i); } list.clear(); list.addAll(map.values()); return list; }