View Javadoc

1   package com.panogenesis.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  /***
11   * String Utility Class This is used to encode passwords programmatically
12   *
13   * <p>
14   * <a h
15   * ref="StringUtil.java.html"><i>View Source</i></a>
16   * </p>
17   * 
18   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
19   */
20  public class StringUtil {
21      //~ Static fields/initializers =============================================
22  
23      private final static Log log = LogFactory.getLog(StringUtil.class);
24  
25      //~ Methods ================================================================
26  
27      /***
28       * Encode a string using algorithm specified in web.xml and return the
29       * resulting encrypted password. If exception, the plain credentials
30       * string is returned
31       *
32       * @param password Password or other credentials to use in authenticating
33       *        this username
34       * @param algorithm Algorithm used to do the digest
35       *
36       * @return encypted password based on the algorithm.
37       */
38      public static String encodePassword(String password, String algorithm) {
39          byte[] unencodedPassword = password.getBytes();
40  
41          MessageDigest md = null;
42  
43          try {
44              // first create an instance, given the provider
45              md = MessageDigest.getInstance(algorithm);
46          } catch (Exception e) {
47              log.error("Exception: " + e);
48  
49              return password;
50          }
51  
52          md.reset();
53  
54          // call the update method one or more times
55          // (useful when you don't know the size of your data, eg. stream)
56          md.update(unencodedPassword);
57  
58          // now calculate the hash
59          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      /***
75       * Encode a string using Base64 encoding. Used when storing passwords
76       * as cookies.
77       *
78       * This is weak encoding in that anyone can use the decodeString
79       * routine to reverse the encoding.
80       *
81       * @param str
82       * @return String
83       */
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      /***
90       * Decode a string using Base64 encoding.
91       *
92       * @param str
93       * @return String
94       */
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 }