1 24 package org.riotfamily.common.util; 25 26 import java.security.MessageDigest ; 27 import java.security.NoSuchAlgorithmException ; 28 29 32 public final class HashUtils { 33 34 private static final String MD5 = "MD5"; 35 36 private static MessageDigest md5Digest; 37 38 private HashUtils() { 39 } 40 41 51 public static synchronized String md5(String data) { 52 if (md5Digest == null) { 53 try { 54 md5Digest = MessageDigest.getInstance(MD5); 55 } 56 catch (NoSuchAlgorithmException e) { 57 throw new RuntimeException (e); 58 } 59 } 60 md5Digest.update(data.getBytes()); 61 return toHex(md5Digest.digest()); 62 } 63 64 71 public static String toHex(byte[] buffer) { 72 StringBuffer sb = new StringBuffer (); 73 String s = null; 74 for (int i = 0; i < buffer.length; i++) { 75 s = Integer.toHexString(buffer[i] & 0xff); 76 if (s.length() < 2) { 77 sb.append('0'); 78 } 79 sb.append(s); 80 } 81 return sb.toString(); 82 } 83 84 } 85 | Popular Tags |