String MD5(String str) { try { java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5"); byte[] array = md.digest(str.getBytes("UTF-8")); return new String(encodeHex(array, DIGITS_LOWER)); } catch (java.security.NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; }
/** * Computing the hash for a method is useful so as to create an alternate key from the method name * + signature. * * @param qualifiedMethodName * @param methodSignature * @return */ public static String computeHash(String qualifiedMethodName, String methodSignature) { String fullMethodName = qualifiedMethodName + methodSignature; byte[] bytes = fullMethodName.getBytes(); md5.reset(); md5.update(bytes); byte[] messageBytes = md5.digest(); StringBuffer message = new StringBuffer(); for (byte aByte : messageBytes) { String expanded = Integer.toHexString(0xFF & aByte); if (expanded.length() == 1) { // normalize length of all strings message.append("0"); } message.append(expanded); } return message.toString(); }