View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.util;
24  
25  import java.security.MessageDigest;
26  import java.util.regex.Pattern;
27  
28  import org.apache.commons.codec.binary.Base64;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /**
33   * String Utility Class This is used to encode passwords programmatically
34   * 
35   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
36   */
37  public final class StringUtil {
38  	
39  	private static Pattern  DIGIT_PATTERN = Pattern.compile("[0-9]+");
40  	
41      private static final Log log = LogFactory.getLog(StringUtil.class);
42  
43      /**
44       * Checkstyle rule: utility classes should not have public constructor
45       */
46      private StringUtil() {
47      }
48  
49      // ~ Methods
50      // ================================================================
51  
52      /**
53       * Encode a string using algorithm specified in web.xml and return the
54       * resulting encrypted password. If exception, the plain credentials string
55       * is returned
56       * 
57       * @param password
58       *                Password or other credentials to use in authenticating
59       *                this username
60       * @param algorithm
61       *                Algorithm used to do the digest
62       * @return encypted password based on the algorithm.
63       */
64      public static String encodePassword(String password, String algorithm) {
65  	byte[] unencodedPassword = password.getBytes();
66  
67  	MessageDigest md = null;
68  
69  	try {
70  	    // first create an instance, given the provider
71  	    md = MessageDigest.getInstance(algorithm);
72  	} catch (Exception e) {
73  	    log.error("Exception: " + e);
74  
75  	    return password;
76  	}
77  
78  	md.reset();
79  
80  	// call the update method one or more times
81  	// (useful when you don't know the size of your data, eg. stream)
82  	md.update(unencodedPassword);
83  
84  	// now calculate the hash
85  	byte[] encodedPassword = md.digest();
86  
87  	StringBuffer buf = new StringBuffer();
88  
89  	for (byte anEncodedPassword : encodedPassword) {
90  	    if ((anEncodedPassword & 0xff) < 0x10) {
91  		buf.append("0");
92  	    }
93  
94  	    buf.append(Long.toString(anEncodedPassword & 0xff, 16));
95  	}
96  
97  	return buf.toString();
98      }
99  
100     /**
101      * Encode a string using Base64 encoding. Used when storing passwords as
102      * cookies. This is weak encoding in that anyone can use the decodeString
103      * routine to reverse the encoding.
104      * 
105      * @param str
106      *                the string to encode
107      * @return the encoded string
108      */
109     public static String encodeString(String str) {
110     	Base64 encoder = new Base64();
111     	return String.valueOf(encoder.encode(str.getBytes())).trim();
112     }
113 
114     
115     public static boolean containsDigit(String text){
116     	if (text!=null && DIGIT_PATTERN.matcher(text).find()){
117     		return true;
118     	} 
119     	return false;
120     	
121     }
122 }