Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. package de.viathinksoft.utils.security;
  2.  
  3. import java.security.MessageDigest;
  4. import java.security.NoSuchAlgorithmException;
  5.  
  6. public class MD5 {
  7.  
  8.         public static String digest(String input) throws NoSuchAlgorithmException {
  9.                 if (input == null) input = "";
  10.                
  11.                 MessageDigest md5 = MessageDigest.getInstance("MD5");
  12.                 md5.reset();
  13.                 md5.update(input.getBytes());
  14.                 byte[] result = md5.digest();
  15.  
  16.                 StringBuffer hexString = new StringBuffer();
  17.                 for (int i = 0; i < result.length; i++) {
  18.                         if (result[i] <= 15 && result[i] >= 0) {
  19.                                 hexString.append("0");
  20.                         }
  21.                         hexString.append(Integer.toHexString(0xFF & result[i]));
  22.                 }
  23.  
  24.                 return hexString.toString();
  25.         }
  26.  
  27.         public static String digest(String input, String salt)
  28.                         throws NoSuchAlgorithmException {
  29.                 if (salt == null) salt = "";
  30.                 return digest(input.concat(salt));
  31.         }
  32. }
  33.