md5加密(1)
package com.js.ai.modules.pointwall.util; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * * @ClassName: MD5Utils * @Description: 邀请码生成 * @author: xfzhong * @date: 2017年4月10日 */ public class MD5Utils { public static String generate(String user ){ String cacheKey; try { final MessageDigest mDigest = MessageDigest.getInstance("MD5"); mDigest.update(user.getBytes()); cacheKey = bytesToHexString(mDigest.digest()); } catch (NoSuchAlgorithmException e) { cacheKey=String.valueOf(user.hashCode()); } String newCacheKey=cacheKey.substring(0, 6); return newCacheKey; } private static String bytesToHexString(byte[] bytes){ StringBuffer sbBuffer=new StringBuffer(); for(int i=0;i<bytes.length;i++){ String hex=Integer.toHexString(0xFF & bytes[i]); if(hex.length()==1){ sbBuffer.append('0'); } sbBuffer.append(hex); } return sbBuffer.toString(); } }