KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mysql > jdbc > Util


1 /*
2    Copyright (C) 2002 MySQL AB
3
4       This program is free software; you can redistribute it and/or modify
5       it under the terms of the GNU General Public License as published by
6       the Free Software Foundation; either version 2 of the License, or
7       (at your option) any later version.
8
9       This program 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
12       GNU General Public License for more details.
13
14       You should have received a copy of the GNU General Public License
15       along with this program; if not, write to the Free Software
16       Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18  */

19 package com.mysql.jdbc;
20
21 import java.io.ObjectInputStream JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.io.StringWriter JavaDoc;
24
25
26 /**
27  * Various utility methods for the driver.
28  *
29  * @author Mark Matthews
30  */

31 public class Util {
32     private static Util enclosingInstance = new Util();
33
34     /**
35      * Given a ResultSet and an index into the columns of that ResultSet,
36      * read binary data from the column which represents a serialized object,
37      * and re-create the object.
38      *
39      * @param resultSet the ResultSet to use.
40      * @param index an index into the ResultSet.
41      * @return the object if it can be de-serialized
42      * @throws Exception if an error occurs
43      */

44     public static Object JavaDoc readObject(java.sql.ResultSet JavaDoc resultSet, int index)
45         throws Exception JavaDoc {
46         ObjectInputStream JavaDoc objIn = new ObjectInputStream JavaDoc(resultSet
47                 .getBinaryStream(index));
48         Object JavaDoc obj = objIn.readObject();
49         objIn.close();
50
51         return obj;
52     }
53
54     /**
55      * DOCUMENT ME!
56      *
57      * @param message DOCUMENT ME!
58      * @param password DOCUMENT ME!
59      *
60      * @return DOCUMENT ME!
61      */

62     public static String JavaDoc scramble(String JavaDoc message, String JavaDoc password) {
63         long[] hashPass;
64         long[] hashMessage;
65         byte[] to = new byte[8];
66         String JavaDoc val = "";
67
68         message = message.substring(0, 8);
69
70         if ((password != null) && (password.length() > 0)) {
71             hashPass = newHash(password);
72             hashMessage = newHash(message);
73
74             RandStructcture randStruct = randomInit(hashPass[0]
75                     ^ hashMessage[0], hashPass[1] ^ hashMessage[1]);
76
77             int msgPos = 0;
78             int msgLength = message.length();
79             int toPos = 0;
80
81             while (msgPos++ < msgLength) {
82                 to[toPos++] = (byte) (Math.floor(rnd(randStruct) * 31) + 64);
83             }
84
85             /* Make it harder to break */
86             byte extra = (byte) (Math.floor(rnd(randStruct) * 31));
87
88             for (int i = 0; i < to.length; i++) {
89                 to[i] ^= extra;
90             }
91
92             val = new String JavaDoc(to);
93         }
94
95         return val;
96     }
97
98     /**
99      * Converts a nested exception into a nicer message
100      *
101      * @param ex the exception to expand into a message.
102      *
103      * @return a message containing the exception, the
104      * message (if any), and a stacktrace.
105      */

106     public static String JavaDoc stackTraceToString(Throwable JavaDoc ex) {
107         StringBuffer JavaDoc traceBuf = new StringBuffer JavaDoc();
108         traceBuf.append("\n\n** BEGIN NESTED EXCEPTION ** \n\n");
109
110         if (ex != null) {
111             traceBuf.append(ex.getClass().getName());
112
113             String JavaDoc message = ex.getMessage();
114
115             if (message != null) {
116                 traceBuf.append("\nMESSAGE: ");
117                 traceBuf.append(message);
118             }
119
120             StringWriter JavaDoc out = new StringWriter JavaDoc();
121
122             PrintWriter JavaDoc printOut = new PrintWriter JavaDoc(out);
123
124             ex.printStackTrace(printOut);
125
126             traceBuf.append("\n\nSTACKTRACE:\n\n");
127             traceBuf.append(out.toString());
128         }
129
130         traceBuf.append("\n\n** END NESTED EXCEPTION **\n\n");
131
132         return traceBuf.toString();
133     }
134
135     // Right from Monty's code
136
static String JavaDoc newCrypt(String JavaDoc password, String JavaDoc seed) {
137         byte b;
138         double d;
139
140         if ((password == null) || (password.length() == 0)) {
141             return password;
142         }
143
144         long[] pw = newHash(seed);
145         long[] msg = newHash(password);
146         long max = 0x3fffffffL;
147         long seed1 = (pw[0] ^ msg[0]) % max;
148         long seed2 = (pw[1] ^ msg[1]) % max;
149         char[] chars = new char[seed.length()];
150
151         for (int i = 0; i < seed.length(); i++) {
152             seed1 = ((seed1 * 3) + seed2) % max;
153             seed2 = (seed1 + seed2 + 33) % max;
154             d = (double) seed1 / (double) max;
155             b = (byte) java.lang.Math.floor((d * 31) + 64);
156             chars[i] = (char) b;
157         }
158
159         seed1 = ((seed1 * 3) + seed2) % max;
160         seed2 = (seed1 + seed2 + 33) % max;
161         d = (double) seed1 / (double) max;
162         b = (byte) java.lang.Math.floor(d * 31);
163
164         for (int i = 0; i < seed.length(); i++) {
165             chars[i] ^= (char) b;
166         }
167
168         return new String JavaDoc(chars);
169     }
170
171     static long[] newHash(String JavaDoc password) {
172         long nr = 1345345333L;
173         long add = 7;
174         long nr2 = 0x12345671L;
175         long tmp;
176
177         for (int i = 0; i < password.length(); ++i) {
178             if ((password.charAt(i) == ' ') || (password.charAt(i) == '\t')) {
179                 continue; // skip spaces
180
}
181
182             tmp = (long) (0xff & password.charAt(i));
183             nr ^= ((((nr & 63) + add) * tmp) + (nr << 8));
184             nr2 += ((nr2 << 8) ^ nr);
185             add += tmp;
186         }
187
188         long[] result = new long[2];
189         result[0] = nr & 0x7fffffffL;
190         result[1] = nr2 & 0x7fffffffL;
191
192         return result;
193     }
194
195     static String JavaDoc oldCrypt(String JavaDoc password, String JavaDoc seed) {
196         long hp;
197         long hm;
198         long s1;
199         long s2;
200         long max = 0x01FFFFFF;
201         double d;
202         byte b;
203
204         if ((password == null) || (password.length() == 0)) {
205             return password;
206         }
207
208         hp = oldHash(seed);
209         hm = oldHash(password);
210
211         long nr = hp ^ hm;
212         nr %= max;
213         s1 = nr;
214         s2 = nr / 2;
215
216         char[] chars = new char[seed.length()];
217
218         for (int i = 0; i < seed.length(); i++) {
219             s1 = ((s1 * 3) + s2) % max;
220             s2 = (s1 + s2 + 33) % max;
221             d = (double) s1 / max;
222             b = (byte) java.lang.Math.floor((d * 31) + 64);
223             chars[i] = (char) b;
224         }
225
226         return new String JavaDoc(chars);
227     }
228
229     static long oldHash(String JavaDoc password) {
230         long nr = 1345345333;
231         long nr2 = 7;
232         long tmp;
233
234         for (int i = 0; i < password.length(); i++) {
235             if ((password.charAt(i) == ' ') || (password.charAt(i) == '\t')) {
236                 continue;
237             }
238
239             tmp = (long) password.charAt(i);
240             nr ^= ((((nr & 63) + nr2) * tmp) + (nr << 8));
241             nr2 += tmp;
242         }
243
244         return nr & (((long) 1L << 31) - 1L);
245     }
246
247     private static RandStructcture randomInit(long seed1, long seed2) {
248         RandStructcture randStruct = enclosingInstance.new RandStructcture();
249
250         randStruct.maxValue = 0x3FFFFFFFL;
251         randStruct.maxValueDbl = (double) randStruct.maxValue;
252         randStruct.seed1 = seed1 % randStruct.maxValue;
253         randStruct.seed2 = seed2 % randStruct.maxValue;
254
255         return randStruct;
256     }
257
258     private static double rnd(RandStructcture randStruct) {
259         randStruct.seed1 = ((randStruct.seed1 * 3) + randStruct.seed2) % randStruct.maxValue;
260         randStruct.seed2 = (randStruct.seed1 + randStruct.seed2 + 33) % randStruct.maxValue;
261
262         return (((double) randStruct.seed1) / randStruct.maxValueDbl);
263     }
264
265     class RandStructcture {
266         double maxValueDbl;
267         long maxValue;
268         long seed1;
269         long seed2;
270     }
271 }
272
Popular Tags