1 19 package com.mysql.jdbc; 20 21 import java.security.MessageDigest ; 22 import java.security.NoSuchAlgorithmException ; 23 24 25 33 class Security { 34 private static final int SHA1_HASH_SIZE = 20; 35 private static final char PVERSION41_CHAR = '*'; 36 37 40 private Security() { 41 super(); 42 } 43 44 59 60 71 static byte[] getBinaryPassword(int[] salt, boolean usingNewPasswords) 72 throws NoSuchAlgorithmException { 73 int val = 0; 74 75 byte[] binaryPassword = new byte[SHA1_HASH_SIZE]; 76 77 if (usingNewPasswords) { 78 int pos = 0; 79 80 for (int i = 0; i < 4; i++) { 81 val = salt[i]; 82 83 for (int t = 3; t >= 0; t--) { 84 binaryPassword[pos++] = (byte) (val & 255); 85 val >>= 8; 86 } 87 } 88 89 return binaryPassword; 90 } else { 91 int offset = 0; 92 93 for (int i = 0; i < 2; i++) { 94 val = salt[i]; 95 96 for (int t = 3; t >= 0; t--) { 97 binaryPassword[t + offset] = (byte) (val % 256); 98 val >>= 8; 99 } 100 101 offset += 4; 102 } 103 104 MessageDigest md = MessageDigest.getInstance("SHA-1"); 105 106 md.update(binaryPassword, 0, 8); 107 108 return md.digest(); 109 } 110 } 111 112 124 static byte[] createKeyFromOldPassword(String passwd) 125 throws NoSuchAlgorithmException { 126 127 passwd = makeScrambledPassword(passwd); 128 129 130 int[] salt = getSaltFromPassword(passwd); 131 132 133 return getBinaryPassword(salt, false); 134 } 135 136 149 static String makeScrambledPassword(String password) 150 throws NoSuchAlgorithmException { 151 long[] passwordHash = Util.newHash(password); 152 StringBuffer scramble = new StringBuffer (); 153 154 scramble.append(longToHex(passwordHash[0])); 155 scramble.append(longToHex(passwordHash[1])); 156 157 return scramble.toString(); 158 } 159 160 170 static void passwordCrypt(byte[] from, byte[] to, byte[] password, 171 int length) { 172 int pos = 0; 173 174 while ((pos < from.length) && (pos < length)) { 175 to[pos] = (byte) (from[pos] ^ password[pos]); 176 pos++; 177 } 178 } 179 180 190 static byte[] passwordHashStage1(String password) 191 throws NoSuchAlgorithmException { 192 MessageDigest md = MessageDigest.getInstance("SHA-1"); 193 StringBuffer cleansedPassword = new StringBuffer (); 194 195 int passwordLength = password.length(); 196 197 for (int i = 0; i < passwordLength; i++) { 198 char c = password.charAt(i); 199 200 if ((c == ' ') || (c == '\t')) { 201 continue; 202 } 203 204 cleansedPassword.append(c); 205 } 206 207 return md.digest(cleansedPassword.toString().getBytes()); 208 } 209 210 222 static byte[] passwordHashStage2(byte[] hashedPassword, byte[] salt) 223 throws NoSuchAlgorithmException { 224 MessageDigest md = MessageDigest.getInstance("SHA-1"); 225 226 md.update(salt, 0, 4); 228 229 md.update(hashedPassword, 0, SHA1_HASH_SIZE); 230 231 return md.digest(); 232 } 233 234 private static int[] getSaltFromPassword(String password) { 235 int[] result = new int[6]; 236 237 if ((password == null) || (password.length() == 0)) { 238 return result; 239 } 240 241 if (password.charAt(0) == PVERSION41_CHAR) { 242 String saltInHex = password.substring(1, 5); 244 245 int val = 0; 246 247 for (int i = 0; i < 4; i++) { 248 val = (val << 4) + charVal(saltInHex.charAt(i)); 249 } 250 251 return result; 252 } else { 253 int resultPos = 0; 254 int pos = 0; 255 int length = password.length(); 256 257 while (pos < length) { 258 int val = 0; 259 260 for (int i = 0; i < 8; i++) { 261 val = (val << 4) + charVal(password.charAt(pos++)); 262 } 263 264 result[resultPos++] = val; 265 } 266 267 return result; 268 } 269 } 270 271 274 private static int charVal(char c) { 275 return (int) (((c >= '0') && (c <= '9')) ? (c - '0') 276 : (((c >= 'A') && (c <= 'Z')) 277 ? (c - 'A' + 10) : (c - 'a' + 10))); 278 } 279 280 private static String longToHex(long val) { 281 String longHex = Long.toHexString(val); 282 283 int length = longHex.length(); 284 285 if (length < 8) { 286 int padding = 8 - length; 287 StringBuffer buf = new StringBuffer (); 288 289 for (int i = 0; i < padding; i++) { 290 buf.append("0"); 291 } 292 293 buf.append(longHex); 294 295 return buf.toString(); 296 } else { 297 return longHex.substring(0, 8); 298 } 299 } 300 301 319 static byte[] scramble411(String password, String seed) throws NoSuchAlgorithmException { 320 MessageDigest md = MessageDigest.getInstance("SHA-1"); 321 322 byte[] passwordHashStage1 = md.digest(password.getBytes()); 323 md.reset(); 324 byte[] passwordHashStage2 = md.digest(passwordHashStage1); 325 md.reset(); 326 byte[] seedAsBytes = seed.getBytes(); md.update(seedAsBytes); 328 md.update(passwordHashStage2); 329 330 byte[] toBeXord = md.digest(); 331 332 int numToXor = toBeXord.length; 333 334 for (int i = 0; i < numToXor; i++) { 335 toBeXord[i] = (byte)(toBeXord[i] ^ passwordHashStage1[i]); 336 } 337 338 return toBeXord; 339 340 } 341 } 342 | Popular Tags |