1 19 package com.mysql.jdbc; 20 21 import java.io.ObjectInputStream ; 22 import java.io.PrintWriter ; 23 import java.io.StringWriter ; 24 25 26 31 public class Util { 32 private static Util enclosingInstance = new Util(); 33 34 44 public static Object readObject(java.sql.ResultSet resultSet, int index) 45 throws Exception { 46 ObjectInputStream objIn = new ObjectInputStream (resultSet 47 .getBinaryStream(index)); 48 Object obj = objIn.readObject(); 49 objIn.close(); 50 51 return obj; 52 } 53 54 62 public static String scramble(String message, String password) { 63 long[] hashPass; 64 long[] hashMessage; 65 byte[] to = new byte[8]; 66 String 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 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 (to); 93 } 94 95 return val; 96 } 97 98 106 public static String stackTraceToString(Throwable ex) { 107 StringBuffer traceBuf = new StringBuffer (); 108 traceBuf.append("\n\n** BEGIN NESTED EXCEPTION ** \n\n"); 109 110 if (ex != null) { 111 traceBuf.append(ex.getClass().getName()); 112 113 String message = ex.getMessage(); 114 115 if (message != null) { 116 traceBuf.append("\nMESSAGE: "); 117 traceBuf.append(message); 118 } 119 120 StringWriter out = new StringWriter (); 121 122 PrintWriter printOut = new PrintWriter (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 static String newCrypt(String password, String 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 (chars); 169 } 170 171 static long[] newHash(String 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; } 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 oldCrypt(String password, String 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 (chars); 227 } 228 229 static long oldHash(String 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 |