1 36 package net.matuschek.util; 37 38 41 42 class MD5State { 43 46 int state[]; 47 48 51 int count[]; 52 53 56 byte buffer[]; 57 58 public MD5State() { 59 buffer = new byte[64]; 60 count = new int[2]; 61 state = new int[4]; 62 63 state[0] = 0x67452301; 64 state[1] = 0xefcdab89; 65 state[2] = 0x98badcfe; 66 state[3] = 0x10325476; 67 68 count[0] = count[1] = 0; 69 } 70 71 72 public MD5State (MD5State from) { 73 this(); 74 75 int i; 76 77 for (i = 0; i < buffer.length; i++) 78 this.buffer[i] = from.buffer[i]; 79 80 for (i = 0; i < state.length; i++) 81 this.state[i] = from.state[i]; 82 83 for (i = 0; i < count.length; i++) 84 this.count[i] = from.count[i]; 85 } 86 }; 87 88 89 95 public class MD5 { 96 99 MD5State state; 100 101 105 MD5State finals; 106 107 110 static byte padding[] = { 111 (byte) 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 113 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 114 }; 115 116 120 public synchronized void Init () { 121 state = new MD5State(); 122 finals = null; 123 } 124 125 128 public MD5 () { 129 this.Init(); 130 } 131 132 138 public MD5 (Object ob) { 139 this(); 140 Update(ob.toString()); 141 } 142 143 private int rotate_left (int x, int n) { 144 return (x << n) | (x >>> (32 - n)); 145 } 146 147 149 150 private int uadd (int a, int b) { 151 long aa, bb; 152 aa = ((long) a) & 0xffffffffL; 153 bb = ((long) b) & 0xffffffffL; 154 155 aa += bb; 156 157 return (int) (aa & 0xffffffffL); 158 } 159 160 private int uadd (int a, int b, int c) { 161 return uadd(uadd(a, b), c); 162 } 163 164 private int uadd (int a, int b, int c, int d) { 165 return uadd(uadd(a, b, c), d); 166 } 167 168 private int FF (int a, int b, int c, int d, int x, int s, int ac) { 169 a = uadd(a, ((b & c) | (~b & d)), x, ac); 170 return uadd(rotate_left(a, s), b); 171 } 172 173 private int GG (int a, int b, int c, int d, int x, int s, int ac) { 174 a = uadd(a, ((b & d) | (c & ~d)), x, ac); 175 return uadd(rotate_left(a, s), b); 176 } 177 178 private int HH (int a, int b, int c, int d, int x, int s, int ac) { 179 a = uadd(a, (b ^ c ^ d), x, ac); 180 return uadd(rotate_left(a, s) , b); 181 } 182 183 private int II (int a, int b, int c, int d, int x, int s, int ac) { 184 a = uadd(a, (c ^ (b | ~d)), x, ac); 185 return uadd(rotate_left(a, s), b); 186 } 187 188 private int[] Decode (byte buffer[], int len, int shift) { 189 int out[]; 190 int i, j; 191 192 out = new int[16]; 193 194 for (i = j = 0; j < len; i++, j += 4) { 195 out[i] = ((int) (buffer[j + shift] & 0xff)) | 196 (((int) (buffer[j + 1 + shift] & 0xff)) << 8) | 197 (((int) (buffer[j + 2 + shift] & 0xff)) << 16) | 198 (((int) (buffer[j + 3 + shift] & 0xff)) << 24); 199 200 205 } 206 207 return out; 208 } 209 210 private void Transform (MD5State state, byte buffer[], int shift) { 211 int 212 a = state.state[0], 213 b = state.state[1], 214 c = state.state[2], 215 d = state.state[3], 216 x[]; 217 218 x = Decode(buffer, 64, shift); 219 220 221 a = FF (a, b, c, d, x[ 0], 7, 0xd76aa478); 222 d = FF (d, a, b, c, x[ 1], 12, 0xe8c7b756); 223 c = FF (c, d, a, b, x[ 2], 17, 0x242070db); 224 b = FF (b, c, d, a, x[ 3], 22, 0xc1bdceee); 225 a = FF (a, b, c, d, x[ 4], 7, 0xf57c0faf); 226 d = FF (d, a, b, c, x[ 5], 12, 0x4787c62a); 227 c = FF (c, d, a, b, x[ 6], 17, 0xa8304613); 228 b = FF (b, c, d, a, x[ 7], 22, 0xfd469501); 229 a = FF (a, b, c, d, x[ 8], 7, 0x698098d8); 230 d = FF (d, a, b, c, x[ 9], 12, 0x8b44f7af); 231 c = FF (c, d, a, b, x[10], 17, 0xffff5bb1); 232 b = FF (b, c, d, a, x[11], 22, 0x895cd7be); 233 a = FF (a, b, c, d, x[12], 7, 0x6b901122); 234 d = FF (d, a, b, c, x[13], 12, 0xfd987193); 235 c = FF (c, d, a, b, x[14], 17, 0xa679438e); 236 b = FF (b, c, d, a, x[15], 22, 0x49b40821); 237 238 239 a = GG (a, b, c, d, x[ 1], 5, 0xf61e2562); 240 d = GG (d, a, b, c, x[ 6], 9, 0xc040b340); 241 c = GG (c, d, a, b, x[11], 14, 0x265e5a51); 242 b = GG (b, c, d, a, x[ 0], 20, 0xe9b6c7aa); 243 a = GG (a, b, c, d, x[ 5], 5, 0xd62f105d); 244 d = GG (d, a, b, c, x[10], 9, 0x2441453); 245 c = GG (c, d, a, b, x[15], 14, 0xd8a1e681); 246 b = GG (b, c, d, a, x[ 4], 20, 0xe7d3fbc8); 247 a = GG (a, b, c, d, x[ 9], 5, 0x21e1cde6); 248 d = GG (d, a, b, c, x[14], 9, 0xc33707d6); 249 c = GG (c, d, a, b, x[ 3], 14, 0xf4d50d87); 250 b = GG (b, c, d, a, x[ 8], 20, 0x455a14ed); 251 a = GG (a, b, c, d, x[13], 5, 0xa9e3e905); 252 d = GG (d, a, b, c, x[ 2], 9, 0xfcefa3f8); 253 c = GG (c, d, a, b, x[ 7], 14, 0x676f02d9); 254 b = GG (b, c, d, a, x[12], 20, 0x8d2a4c8a); 255 256 257 a = HH (a, b, c, d, x[ 5], 4, 0xfffa3942); 258 d = HH (d, a, b, c, x[ 8], 11, 0x8771f681); 259 c = HH (c, d, a, b, x[11], 16, 0x6d9d6122); 260 b = HH (b, c, d, a, x[14], 23, 0xfde5380c); 261 a = HH (a, b, c, d, x[ 1], 4, 0xa4beea44); 262 d = HH (d, a, b, c, x[ 4], 11, 0x4bdecfa9); 263 c = HH (c, d, a, b, x[ 7], 16, 0xf6bb4b60); 264 b = HH (b, c, d, a, x[10], 23, 0xbebfbc70); 265 a = HH (a, b, c, d, x[13], 4, 0x289b7ec6); 266 d = HH (d, a, b, c, x[ 0], 11, 0xeaa127fa); 267 c = HH (c, d, a, b, x[ 3], 16, 0xd4ef3085); 268 b = HH (b, c, d, a, x[ 6], 23, 0x4881d05); 269 a = HH (a, b, c, d, x[ 9], 4, 0xd9d4d039); 270 d = HH (d, a, b, c, x[12], 11, 0xe6db99e5); 271 c = HH (c, d, a, b, x[15], 16, 0x1fa27cf8); 272 b = HH (b, c, d, a, x[ 2], 23, 0xc4ac5665); 273 274 275 a = II (a, b, c, d, x[ 0], 6, 0xf4292244); 276 d = II (d, a, b, c, x[ 7], 10, 0x432aff97); 277 c = II (c, d, a, b, x[14], 15, 0xab9423a7); 278 b = II (b, c, d, a, x[ 5], 21, 0xfc93a039); 279 a = II (a, b, c, d, x[12], 6, 0x655b59c3); 280 d = II (d, a, b, c, x[ 3], 10, 0x8f0ccc92); 281 c = II (c, d, a, b, x[10], 15, 0xffeff47d); 282 b = II (b, c, d, a, x[ 1], 21, 0x85845dd1); 283 a = II (a, b, c, d, x[ 8], 6, 0x6fa87e4f); 284 d = II (d, a, b, c, x[15], 10, 0xfe2ce6e0); 285 c = II (c, d, a, b, x[ 6], 15, 0xa3014314); 286 b = II (b, c, d, a, x[13], 21, 0x4e0811a1); 287 a = II (a, b, c, d, x[ 4], 6, 0xf7537e82); 288 d = II (d, a, b, c, x[11], 10, 0xbd3af235); 289 c = II (c, d, a, b, x[ 2], 15, 0x2ad7d2bb); 290 b = II (b, c, d, a, x[ 9], 21, 0xeb86d391); 291 292 state.state[0] += a; 293 state.state[1] += b; 294 state.state[2] += c; 295 state.state[3] += d; 296 } 297 298 308 public void Update (MD5State stat, byte buffer[], int offset, int length) { 309 int index, partlen, i, start; 310 311 finals = null; 312 313 314 if ((length - offset)> buffer.length) 315 length = buffer.length - offset; 316 317 318 index = (int) (stat.count[0] >>> 3) & 0x3f; 319 320 if ((stat.count[0] += (length << 3)) < 321 (length << 3)) 322 stat.count[1]++; 323 324 stat.count[1] += length >>> 29; 325 326 partlen = 64 - index; 327 328 if (length >= partlen) { 329 for (i = 0; i < partlen; i++) 330 stat.buffer[i + index] = buffer[i + offset]; 331 332 Transform(stat, stat.buffer, 0); 333 334 for (i = partlen; (i + 63) < length; i+= 64) 335 Transform(stat, buffer, i); 336 337 index = 0; 338 } else 339 i = 0; 340 341 342 if (i < length) { 343 start = i; 344 for (; i < length; i++) 345 stat.buffer[index + i - start] = buffer[i + offset]; 346 } 347 } 348 349 353 354 357 358 public void Update (byte buffer[], int offset, int length) { 359 Update(this.state, buffer, offset, length); 360 } 361 362 public void Update (byte buffer[], int length) { 363 Update(this.state, buffer, 0, length); 364 } 365 366 371 public void Update (byte buffer[]) { 372 Update(buffer, 0, buffer.length); 373 } 374 375 380 public void Update (byte b) { 381 byte buffer[] = new byte[1]; 382 buffer[0] = b; 383 384 Update(buffer, 1); 385 } 386 387 393 public void Update (String s) { 394 byte chars[]; 395 396 chars = s.getBytes(); 397 398 Update(chars, chars.length); 399 } 400 401 408 409 public void Update (int i) { 410 Update((byte) (i & 0xff)); 411 } 412 413 private byte[] Encode (int input[], int len) { 414 int i, j; 415 byte out[]; 416 417 out = new byte[len]; 418 419 for (i = j = 0; j < len; i++, j += 4) { 420 out[j] = (byte) (input[i] & 0xff); 421 out[j + 1] = (byte) ((input[i] >>> 8) & 0xff); 422 out[j + 2] = (byte) ((input[i] >>> 16) & 0xff); 423 out[j + 3] = (byte) ((input[i] >>> 24) & 0xff); 424 } 425 426 return out; 427 } 428 429 437 public synchronized byte[] Final () { 438 byte bits[]; 439 int index, padlen; 440 MD5State fin; 441 442 if (finals == null) { 443 fin = new MD5State(state); 444 445 bits = Encode(fin.count, 8); 446 447 index = (int) ((fin.count[0] >>> 3) & 0x3f); 448 padlen = (index < 56) ? (56 - index) : (120 - index); 449 450 Update(fin, padding, 0, padlen); 451 452 Update(fin, bits, 0, 8); 453 454 455 finals = fin; 456 } 457 458 return Encode(finals.state, 16); 459 } 460 461 468 public static String asHex (byte hash[]) { 469 StringBuffer buf = new StringBuffer (hash.length * 2); 470 int i; 471 472 for (i = 0; i < hash.length; i++) { 473 if (((int) hash[i] & 0xff) < 0x10) 474 buf.append("0"); 475 476 buf.append(Long.toString((int) hash[i] & 0xff, 16)); 477 } 478 479 return buf.toString(); 480 } 481 482 487 public String asHex () { 488 return asHex(this.Final()); 489 } 490 } 491 492 493 | Popular Tags |