KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > jdbc > hsqldb > DBFunctions


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.jdbc.hsqldb;
21
22 import java.io.UnsupportedEncodingException JavaDoc;
23 import java.security.MessageDigest JavaDoc;
24 import java.security.NoSuchAlgorithmException JavaDoc;
25 import java.util.regex.Matcher JavaDoc;
26 import java.util.regex.Pattern JavaDoc;
27
28 import com.maverick.crypto.encoders.Base64;
29 import com.sslexplorer.boot.ReplacementEngine;
30
31 /**
32  * Utility class containing static methods that are registered with the embedded
33  * HSQLDB server as functions.
34  *
35  * @author Brett Smith <brett@3sp.com>
36  */

37 public class DBFunctions {
38
39     /**
40      * Tests whether a string matches a supplied regular expression and returns
41      * the text if it does.
42      *
43      * @param text text to match
44      * @param regex regular expression
45      * @return text
46      */

47     public static String JavaDoc matches(String JavaDoc text, String JavaDoc regex) {
48         Pattern JavaDoc pattern = ReplacementEngine.getPatternPool().getPattern(regex, false, false);
49         try {
50             Matcher JavaDoc matcher = pattern.matcher(text);
51             return matcher.find() ? text : "";
52         } finally {
53             ReplacementEngine.getPatternPool().releasePattern(pattern);
54         }
55     }
56
57     /*
58      * This is required to make fresh installs work correctly. As of 0.1.4,
59      * passwords became BASE64 encoded and now use the ENCPASSWORD function.
60      */

61     public static String JavaDoc password(String JavaDoc text) throws UnsupportedEncodingException JavaDoc, NoSuchAlgorithmException JavaDoc {
62         return digest(text);
63     }
64
65     /**
66      * Encode a password one-way by first by producing an MD5 hash of the password
67      * then encoding that in BASE64.
68      *
69      * @param password password to encode
70      * @return encoded password
71      * @throws UnsupportedEncodingException
72      * @throws NoSuchAlgorithmException
73      */

74     public static String JavaDoc encPassword(String JavaDoc password) throws UnsupportedEncodingException JavaDoc, NoSuchAlgorithmException JavaDoc {
75         String JavaDoc encoded = encode(digest(password));
76         return encoded;
77     }
78
79     /**
80      * Encode a string as BASE64.
81      *
82      * @param string string to encode
83      * @return encoded string
84      */

85     public static String JavaDoc encode(String JavaDoc string) {
86         return new String JavaDoc(Base64.encode(string.getBytes()));
87     }
88
89     /**
90      * Decode a string from BASE64 to its original form.
91      *
92      * @param string string to decode
93      * @return decoded string
94      */

95     public static String JavaDoc decode(String JavaDoc string) {
96         return new String JavaDoc(Base64.decode(string.getBytes()));
97     }
98
99     /**
100      * Create an MD5 hash of the provided string
101      *
102      * @param string string to hash
103      * @return hashed string
104      */

105     public static String JavaDoc digest(String JavaDoc string) throws NoSuchAlgorithmException JavaDoc, UnsupportedEncodingException JavaDoc {
106         byte[] b = string.getBytes();
107         MessageDigest JavaDoc md = MessageDigest.getInstance("MD5");
108         md.update(b);
109         byte[] digest = md.digest();
110         return new String JavaDoc(digest);
111     }
112
113 }
114
Popular Tags