1 43 package net.jforum.util; 44 45 import java.security.MessageDigest ; 46 import java.security.NoSuchAlgorithmException ; 47 48 import net.jforum.exceptions.ForumException; 49 50 56 public class MD5 57 { 58 65 public static String crypt(String str) 66 { 67 if (str == null || str.length() == 0) { 68 throw new IllegalArgumentException ("String to encript cannot be null or zero length"); 69 } 70 71 StringBuffer hexString = new StringBuffer (); 72 73 try { 74 MessageDigest md = MessageDigest.getInstance("MD5"); 75 md.update(str.getBytes()); 76 byte[] hash = md.digest(); 77 78 for (int i = 0; i < hash.length; i++) { 79 if ((0xff & hash[i]) < 0x10) { 80 hexString.append("0" + Integer.toHexString((0xFF & hash[i]))); 81 } 82 else { 83 hexString.append(Integer.toHexString(0xFF & hash[i])); 84 } 85 } 86 } 87 catch (NoSuchAlgorithmException e) { 88 throw new ForumException("" + e); 89 } 90 91 return hexString.toString(); 92 } 93 } 94 | Popular Tags |