1 package org.appfuse.util; 2 3 import java.io.IOException ; 4 import java.security.MessageDigest ; 5 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 9 10 20 public class StringUtil { 21 23 private final static Log log = LogFactory.getLog(StringUtil.class); 24 25 27 38 public static String encodePassword(String password, String algorithm) { 39 byte[] unencodedPassword = password.getBytes(); 40 41 MessageDigest md = null; 42 43 try { 44 md = MessageDigest.getInstance(algorithm); 46 } catch (Exception e) { 47 log.error("Exception: " + e); 48 49 return password; 50 } 51 52 md.reset(); 53 54 md.update(unencodedPassword); 57 58 byte[] encodedPassword = md.digest(); 60 61 StringBuffer buf = new StringBuffer (); 62 63 for (int i = 0; i < encodedPassword.length; i++) { 64 if ((encodedPassword[i] & 0xff) < 0x10) { 65 buf.append("0"); 66 } 67 68 buf.append(Long.toString(encodedPassword[i] & 0xff, 16)); 69 } 70 71 return buf.toString(); 72 } 73 74 84 public static String encodeString(String str) { 85 sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder(); 86 return encoder.encodeBuffer(str.getBytes()).trim(); 87 } 88 89 95 public static String decodeString(String str) { 96 sun.misc.BASE64Decoder dec = new sun.misc.BASE64Decoder(); 97 try { 98 return new String (dec.decodeBuffer(str)); 99 } catch (IOException io) { 100 throw new RuntimeException (io.getMessage(), io.getCause()); 101 } 102 } 103 } 104 | Popular Tags |