KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > lib > MD5


1 /* Copyright (c) 2001-2004, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.lib;
33
34 import java.io.UnsupportedEncodingException JavaDoc;
35 import java.security.MessageDigest JavaDoc;
36 import java.security.NoSuchAlgorithmException JavaDoc;
37
38 // boucherb@users 20030705 - patch 1.7.2 - renamed methods to allow sp/sf calls
39
// under HSQLDB's current inability to resolve method name overloads.
40

41 /**
42  * Provides a static utility interface to an MD5 digest algorithm
43  * obtained through the java.security.MessageDigest spi. <p>
44  *
45  * Database end-users may wish to access the services of this class
46  * to provide, for instance, application user lookup tables with
47  * one-way password encryption. For example: <p>
48  *
49  * <pre>
50  * -- DDL
51  * CREATE TABLE USERS(UID INTEGER IDENTITY, UNAME VARCHAR, UPASS VARCHAR, UNIQUE(UNAME))
52  * CREATE ALIAS MD5 FOR "org.hsqldb.lib.MD5.encodeString"
53  *
54  * -- DML & DQL
55  * INSERT INTO USERS(UNAME, UPASS) VALUES('joe', MD5('passwd'))
56  * UPDATE USERS SET UPASS = MD5('newpasswd') WHERE UNAME = 'joe' AND UPASS = MD5('oldpasswd')
57  * SELECT UID FROM USERS WHERE UNAME = 'joe' AND UPASS = MD5('logonpasswd')
58  * </pre>
59  *
60  * <b>NOTE:</b> <p>
61  *
62  * Although it is possible that a particular JVM / application installation may
63  * encounter NoSuchAlgorithmException when attempting to get a jce MD5 message
64  * digest generator, the likelyhood is very small for almost all JDK/JRE 1.1
65  * and later JVM implementations, as the Sun java.security package has come,
66  * by default, with a jce MD5 message digest generator since JDK 1.1 was
67  * released. The HSLQLDB project could have provided an MD5 implementation to
68  * guarantee presence, but this class is much more lightweight and still allows
69  * clients to install / use custom implementations through the
70  * java.security.MessageDigest spi, for instance if there is no service
71  * provided by default under the target JVM of choice or if a client has
72  * developed / provides, say, a faster MD5 message digest implementation.
73  * In short, this class is a convenience that allows HSQLDB SQL Function and
74  * Stored Procedure style access to any underlying MD5 message digest algorithm
75  * obtained via the java.security.MessageDigest spi
76  *
77  * @author boucherb@users.sourceforge.net
78  * @version 1.7.2
79  * @since 1.7.2
80  */

81 public final class MD5 {
82
83     /**
84      * The jce MD5 message digest generator.
85      */

86     private static MessageDigest JavaDoc md5;
87
88     /**
89      * Retrieves a hexidecimal character sequence representing the MD5
90      * digest of the specified character sequence, using the specified
91      * encoding to first convert the character sequence into a byte sequence.
92      * If the specified encoding is null, then ISO-8859-1 is assumed
93      *
94      * @param string the string to encode.
95      * @param encoding the encoding used to convert the string into the
96      * byte sequence to submit for MD5 digest
97      * @return a hexidecimal character sequence representing the MD5
98      * digest of the specified string
99      * @throws HsqlUnsupportedOperationException if an MD5 digest
100      * algorithm is not available through the
101      * java.security.MessageDigest spi or the requested
102      * encoding is not available
103      */

104     public static final String JavaDoc encodeString(String JavaDoc string,
105             String JavaDoc encoding) throws RuntimeException JavaDoc {
106         return StringConverter.byteToHex(digestString(string, encoding));
107     }
108
109     /**
110      * Retrieves a byte sequence representing the MD5 digest of the
111      * specified character sequence, using the specified encoding to
112      * first convert the character sequence into a byte sequence.
113      * If the specified encoding is null, then ISO-8859-1 is
114      * assumed.
115      *
116      * @param string the string to digest.
117      * @param encoding the character encoding.
118      * @return the digest as an array of 16 bytes.
119      * @throws HsqlUnsupportedOperationException if an MD5 digest
120      * algorithm is not available through the
121      * java.security.MessageDigest spi or the requested
122      * encoding is not available
123      */

124     public static byte[] digestString(String JavaDoc string,
125                                       String JavaDoc encoding)
126                                       throws RuntimeException JavaDoc {
127
128         byte[] data;
129
130         if (encoding == null) {
131             encoding = "ISO-8859-1";
132         }
133
134         try {
135             data = string.getBytes(encoding);
136         } catch (UnsupportedEncodingException JavaDoc x) {
137             throw new RuntimeException JavaDoc(x.toString());
138         }
139
140         return digestBytes(data);
141     }
142
143     /**
144      * Retrieves a byte sequence representing the MD5 digest of the
145      * specified byte sequence.
146      *
147      * @param data the data to digest.
148      * @return the MD5 digest as an array of 16 bytes.
149      * @throws HsqlUnsupportedOperationException if an MD5 digest
150      * algorithm is not available through the
151      * java.security.MessageDigest spi
152      */

153     public static final byte[] digestBytes(byte[] data)
154     throws RuntimeException JavaDoc {
155
156         synchronized (MD5.class) {
157             if (md5 == null) {
158                 try {
159                     md5 = MessageDigest.getInstance("MD5");
160                 } catch (NoSuchAlgorithmException JavaDoc e) {
161                     throw new RuntimeException JavaDoc(e.toString());
162                 }
163             }
164
165             return md5.digest(data);
166         }
167     }
168 }
169
Popular Tags