1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
|
/*
* MD5 in Java JDK Beta-2
* written Santeri Paavolainen, Helsinki Finland 1996
* (c) Santeri Paavolainen, Helsinki Finland 1996
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* See http://www.cs.hut.fi/~santtu/java/ for more information on this
* class.
*
* This is rather straight re-implementation of the reference implementation
* given in RFC1321 by RSA.
*
* Passes MD5 test suite as defined in RFC1321.
*
*
* This Java class has been derived from the RSA Data Security, Inc. MD5
* Message-Digest Algorithm and its reference implementation.
*
*/
package openlink.util;
/**
* Contains internal state of the MD5 class
*/
class MD5State {
/**
* 128-byte state
*/
int state[];
/**
* 64-bit character count (could be true Java long?)
*/
int count[];
/**
* 64-byte buffer (512 bits) for storing to-be-hashed characters
*/
byte buffer[];
public MD5State() {
buffer = new byte[64];
count = new int[2];
state = new int[4];
state[0] = 0x67452301;
state[1] = 0xefcdab89;
state[2] = 0x98badcfe;
state[3] = 0x10325476;
count[0] = count[1] = 0;
}
/** Create this State as a copy of another state */
public MD5State (MD5State from) {
this();
int i;
for (i = 0; i < buffer.length; i++)
this.buffer[i] = from.buffer[i];
for (i = 0; i < state.length; i++)
this.state[i] = from.state[i];
for (i = 0; i < count.length; i++)
this.count[i] = from.count[i];
}
};
/**
* Implementation of RSA's MD5 hash generator
*
* @version $Revision: 1.1.1.1.2.1 $
* @author Santeri Paavolainen <sjpaavol@cc.helsinki.fi>
*/
public class MD5 {
/**
* MD5 state
*/
MD5State state;
/**
* If Final() has been called, finals is set to the current finals
* state. Any Update() causes this to be set to null.
*/
MD5State finals;
/**
* Padding for Final()
*/
static byte padding[] = {
(byte) 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
/**
* Initialize MD5 internal state (object can be reused just by
* calling Init() after every Final()
*/
public synchronized void Init () {
state = new MD5State();
finals = null;
}
/**
* Class constructor
*/
public MD5 () {
this.Init();
}
/**
* Initialize class, and update hash with ob.toString()
*
* @param ob Object, ob.toString() is used to update hash
* after initialization
*/
public MD5 (Object ob) {
this();
Update(ob.toString());
}
private int rotate_left (int x, int n) {
return (x << n) | (x >>> (32 - n));
}
/* I wonder how many loops and hoops you'll have to go through to
get unsigned add for longs in java */
private int uadd (int a, int b) {
long aa, bb;
aa = ((long) a) & 0xffffffffL;
bb = ((long) b) & 0xffffffffL;
aa += bb;
return (int) (aa & 0xffffffffL);
}
private int uadd (int a, int b, int c) {
return uadd(uadd(a, b), c);
}
private int uadd (int a, int b, int c, int d) {
return uadd(uadd(a, b, c), d);
}
private int FF (int a, int b, int c, int d, int x, int s, int ac) {
a = uadd(a, ((b & c) | (~b & d)), x, ac);
return uadd(rotate_left(a, s), b);
}
private int GG (int a, int b, int c, int d, int x, int s, int ac) {
a = uadd(a, ((b & d) | (c & ~d)), x, ac);
return uadd(rotate_left(a, s), b);
}
private int HH (int a, int b, int c, int d, int x, int s, int ac) {
a = uadd(a, (b ^ c ^ d), x, ac);
return uadd(rotate_left(a, s) , b);
}
private int II (int a, int b, int c, int d, int x, int s, int ac) {
a = uadd(a, (c ^ (b | ~d)), x, ac);
return uadd(rotate_left(a, s), b);
}
private int[] Decode (byte buffer[], int len, int shift) {
int out[];
int i, j;
out = new int[16];
for (i = j = 0; j < len; i++, j += 4) {
out[i] = ((int) (buffer[j + shift] & 0xff)) |
(((int) (buffer[j + 1 + shift] & 0xff)) << 8) |
(((int) (buffer[j + 2 + shift] & 0xff)) << 16) |
(((int) (buffer[j + 3 + shift] & 0xff)) << 24);
/* System.out.println("out[" + i + "] = \t" +
((int) buffer[j + 0 + shift] & 0xff) + "\t|\t" +
((int) buffer[j + 1 + shift] & 0xff) + "\t|\t" +
((int) buffer[j + 2 + shift] & 0xff) + "\t|\t" +
((int) buffer[j + 3 + shift] & 0xff));*/
}
return out;
}
private void Transform (MD5State state, byte buffer[], int shift) {
int
a = state.state[0],
b = state.state[1],
c = state.state[2],
d = state.state[3],
x[];
x = Decode(buffer, 64, shift);
/* Round 1 */
a = FF (a, b, c, d, x[ 0], 7, 0xd76aa478); /* 1 */
d = FF (d, a, b, c, x[ 1], 12, 0xe8c7b756); /* 2 */
c = FF (c, d, a, b, x[ 2], 17, 0x242070db); /* 3 */
b = FF (b, c, d, a, x[ 3], 22, 0xc1bdceee); /* 4 */
a = FF (a, b, c, d, x[ 4], 7, 0xf57c0faf); /* 5 */
d = FF (d, a, b, c, x[ 5], 12, 0x4787c62a); /* 6 */
c = FF (c, d, a, b, x[ 6], 17, 0xa8304613); /* 7 */
b = FF (b, c, d, a, x[ 7], 22, 0xfd469501); /* 8 */
a = FF (a, b, c, d, x[ 8], 7, 0x698098d8); /* 9 */
d = FF (d, a, b, c, x[ 9], 12, 0x8b44f7af); /* 10 */
c = FF (c, d, a, b, x[10], 17, 0xffff5bb1); /* 11 */
b = FF (b, c, d, a, x[11], 22, 0x895cd7be); /* 12 */
a = FF (a, b, c, d, x[12], 7, 0x6b901122); /* 13 */
d = FF (d, a, b, c, x[13], 12, 0xfd987193); /* 14 */
c = FF (c, d, a, b, x[14], 17, 0xa679438e); /* 15 */
b = FF (b, c, d, a, x[15], 22, 0x49b40821); /* 16 */
/* Round 2 */
a = GG (a, b, c, d, x[ 1], 5, 0xf61e2562); /* 17 */
d = GG (d, a, b, c, x[ 6], 9, 0xc040b340); /* 18 */
c = GG (c, d, a, b, x[11], 14, 0x265e5a51); /* 19 */
b = GG (b, c, d, a, x[ 0], 20, 0xe9b6c7aa); /* 20 */
a = GG (a, b, c, d, x[ 5], 5, 0xd62f105d); /* 21 */
d = GG (d, a, b, c, x[10], 9, 0x2441453); /* 22 */
c = GG (c, d, a, b, x[15], 14, 0xd8a1e681); /* 23 */
b = GG (b, c, d, a, x[ 4], 20, 0xe7d3fbc8); /* 24 */
a = GG (a, b, c, d, x[ 9], 5, 0x21e1cde6); /* 25 */
d = GG (d, a, b, c, x[14], 9, 0xc33707d6); /* 26 */
c = GG (c, d, a, b, x[ 3], 14, 0xf4d50d87); /* 27 */
b = GG (b, c, d, a, x[ 8], 20, 0x455a14ed); /* 28 */
a = GG (a, b, c, d, x[13], 5, 0xa9e3e905); /* 29 */
d = GG (d, a, b, c, x[ 2], 9, 0xfcefa3f8); /* 30 */
c = GG (c, d, a, b, x[ 7], 14, 0x676f02d9); /* 31 */
b = GG (b, c, d, a, x[12], 20, 0x8d2a4c8a); /* 32 */
/* Round 3 */
a = HH (a, b, c, d, x[ 5], 4, 0xfffa3942); /* 33 */
d = HH (d, a, b, c, x[ 8], 11, 0x8771f681); /* 34 */
c = HH (c, d, a, b, x[11], 16, 0x6d9d6122); /* 35 */
b = HH (b, c, d, a, x[14], 23, 0xfde5380c); /* 36 */
a = HH (a, b, c, d, x[ 1], 4, 0xa4beea44); /* 37 */
d = HH (d, a, b, c, x[ 4], 11, 0x4bdecfa9); /* 38 */
c = HH (c, d, a, b, x[ 7], 16, 0xf6bb4b60); /* 39 */
b = HH (b, c, d, a, x[10], 23, 0xbebfbc70); /* 40 */
a = HH (a, b, c, d, x[13], 4, 0x289b7ec6); /* 41 */
d = HH (d, a, b, c, x[ 0], 11, 0xeaa127fa); /* 42 */
c = HH (c, d, a, b, x[ 3], 16, 0xd4ef3085); /* 43 */
b = HH (b, c, d, a, x[ 6], 23, 0x4881d05); /* 44 */
a = HH (a, b, c, d, x[ 9], 4, 0xd9d4d039); /* 45 */
d = HH (d, a, b, c, x[12], 11, 0xe6db99e5); /* 46 */
c = HH (c, d, a, b, x[15], 16, 0x1fa27cf8); /* 47 */
b = HH (b, c, d, a, x[ 2], 23, 0xc4ac5665); /* 48 */
/* Round 4 */
a = II (a, b, c, d, x[ 0], 6, 0xf4292244); /* 49 */
d = II (d, a, b, c, x[ 7], 10, 0x432aff97); /* 50 */
c = II (c, d, a, b, x[14], 15, 0xab9423a7); /* 51 */
b = II (b, c, d, a, x[ 5], 21, 0xfc93a039); /* 52 */
a = II (a, b, c, d, x[12], 6, 0x655b59c3); /* 53 */
d = II (d, a, b, c, x[ 3], 10, 0x8f0ccc92); /* 54 */
c = II (c, d, a, b, x[10], 15, 0xffeff47d); /* 55 */
b = II (b, c, d, a, x[ 1], 21, 0x85845dd1); /* 56 */
a = II (a, b, c, d, x[ 8], 6, 0x6fa87e4f); /* 57 */
d = II (d, a, b, c, x[15], 10, 0xfe2ce6e0); /* 58 */
c = II (c, d, a, b, x[ 6], 15, 0xa3014314); /* 59 */
b = II (b, c, d, a, x[13], 21, 0x4e0811a1); /* 60 */
a = II (a, b, c, d, x[ 4], 6, 0xf7537e82); /* 61 */
d = II (d, a, b, c, x[11], 10, 0xbd3af235); /* 62 */
c = II (c, d, a, b, x[ 2], 15, 0x2ad7d2bb); /* 63 */
b = II (b, c, d, a, x[ 9], 21, 0xeb86d391); /* 64 */
state.state[0] += a;
state.state[1] += b;
state.state[2] += c;
state.state[3] += d;
}
/**
* Updates hash with the bytebuffer given (using at maximum length bytes from
* that buffer)
*
* @param state Which state is updated
* @param buffer Array of bytes to be hashed
* @param offset Offset to buffer array
* @param length Use at maximum `length' bytes (absolute
* maximum is buffer.length)
*/
public void Update (MD5State stat, byte buffer[], int offset, int length) {
int index, partlen, i, start;
/* System.out.print("Offset = " + offset + "\tLength = " + length + "\t");
System.out.print("Buffer = ");
for (i = 0; i < buffer.length; i++)
System.out.print((int) (buffer[i] & 0xff) + " ");
System.out.print("\n");*/
finals = null;
/* Length can be told to be shorter, but not inter */
if ((length - offset)> buffer.length)
length = buffer.length - offset;
/* compute number of bytes mod 64 */
index = (int) (stat.count[0] >>> 3) & 0x3f;
if ((stat.count[0] += (length << 3)) <
(length << 3))
stat.count[1]++;
stat.count[1] += length >>> 29;
partlen = 64 - index;
if (length >= partlen) {
for (i = 0; i < partlen; i++)
stat.buffer[i + index] = buffer[i + offset];
Transform(stat, stat.buffer, 0);
for (i = partlen; (i + 63) < length; i+= 64)
Transform(stat, buffer, i);
index = 0;
} else
i = 0;
/* buffer remaining input */
if (i < length) {
start = i;
for (; i < length; i++)
stat.buffer[index + i - start] = buffer[i + offset];
}
}
/*
* Update()s for other datatypes than byte[] also. Update(byte[], int)
* is only the main driver.
*/
/**
* Plain update, updates this object
*/
public void Update (byte buffer[], int offset, int length) {
Update(this.state, buffer, offset, length);
}
public void Update (byte buffer[], int length) {
Update(this.state, buffer, 0, length);
}
/**
* Updates hash with given array of bytes
*
* @param buffer Array of bytes to use for updating the hash
*/
public void Update (byte buffer[]) {
Update(buffer, 0, buffer.length);
}
/**
* Updates hash with a single byte
*
* @param b Single byte to update the hash
*/
public void Update (byte b) {
byte buffer[] = new byte[1];
buffer[0] = b;
Update(buffer, 1);
}
/**
* Update buffer with given string.
*
* @param s String to be update to hash (is used as
* s.getBytes())
*/
public void Update (String s) {
byte chars[];
try
{
chars = s.getBytes ("8859_1");
Update(chars, chars.length);
}
catch (java.io.UnsupportedEncodingException e)
{
}
}
/**
* Update buffer with a single integer (only & 0xff part is used,
* as a byte)
*
* @param i Integer value, which is then converted to
* byte as i & 0xff
*/
public void Update (int i) {
Update((byte) (i & 0xff));
}
private byte[] Encode (int input[], int len) {
int i, j;
byte out[];
out = new byte[len];
for (i = j = 0; j < len; i++, j += 4) {
out[j] = (byte) (input[i] & 0xff);
out[j + 1] = (byte) ((input[i] >>> 8) & 0xff);
out[j + 2] = (byte) ((input[i] >>> 16) & 0xff);
out[j + 3] = (byte) ((input[i] >>> 24) & 0xff);
}
return out;
}
/**
* Returns array of bytes (16 bytes) representing hash as of the
* current state of this object. Note: getting a hash does not
* invalidate the hash object, it only creates a copy of the real
* state which is finalized.
*
* @return Array of 16 bytes, the hash of all updated bytes
*/
public synchronized byte[] Final () {
byte bits[];
int index, padlen;
MD5State fin;
if (finals == null) {
fin = new MD5State(state);
bits = Encode(fin.count, 8);
index = (int) ((fin.count[0] >>> 3) & 0x3f);
padlen = (index < 56) ? (56 - index) : (120 - index);
Update(fin, padding, 0, padlen);
/**/
Update(fin, bits, 0, 8);
/* Update() sets finalds to null */
finals = fin;
}
return Encode(finals.state, 16);
}
/**
* Turns array of bytes into string representing each byte as
* unsigned hex number.
*
* @param hash Array of bytes to convert to hex-string
* @return Generated hex string
*/
public static String asHex (byte hash[]) {
StringBuffer buf = new StringBuffer(hash.length * 2);
int i;
for (i = 0; i < hash.length; i++) {
if (((int) hash[i] & 0xff) < 0x10)
buf.append("0");
buf.append(Long.toString((int) hash[i] & 0xff, 16));
}
return buf.toString();
}
/**
* Returns 32-character hex representation of this objects hash
*
* @return String of this object's hash
*/
public String asHex () {
return asHex(this.Final());
}
/* Virtuoso stuff starts here */
public static String md5_digest (String user, String password, String ses_name)
{
MD5 md5 = new MD5();
try
{
byte uid_bytes [] = user.getBytes ("8859_1");
byte pwd_bytes [] = password.getBytes ("8859_1");
byte ses_bytes [] = ses_name.getBytes ("8859_1");
md5.Init();
md5.Update(ses_bytes, ses_bytes.length);
md5.Update(uid_bytes, uid_bytes.length);
md5.Update(pwd_bytes, pwd_bytes.length);
return new String (md5.Final (), "8859_1");
}
catch (java.io.UnsupportedEncodingException e)
{
}
return null;
}
private static final String spass1 = "7rLrT7iG3kWWLuSDYdS/KIXO8JF86h12KyCTG1Mh0qxWdSZ6ezHRST0UuGl6xkbMgsXj4+eZbXNyYijRmoaaJm+hQCWSOW+0OHGCnYWB4upxi0Fogdu0gb+q4VFzyUFknEpZPg==";
private static final String spass2 = "PCuJhpWX5eApg2mRs0bvSIdfwSDUa0kjiSdd76ORgXYyhtLbHm4Uq6afLbfROLi5pDpjKVS9Vr9aZo+F3IpyZ6Zn6m/Xf1PRtq3jdseJht4VSduxHrpocKVdRh3LixXKr6Ue6A==";
private static byte the_pass[] = new String ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").getBytes();
private static void calculate_pass ()
{
byte pass1[] = spass1.getBytes();
byte pass2[] = spass2.getBytes();
synchronized (the_pass)
{
if (the_pass[0] == 'x')
{
int inx;
for (inx = 0; inx < pass2.length; inx++)
{
the_pass[inx] = (byte) (pass1[inx] ^ pass2[inx]);
if (the_pass[inx] == 0)
the_pass[inx] = pass1[inx];
}
the_pass[136] = 0;
}
}
//System.err.println ("the_pass is :" + asHex (the_pass));
}
private static void xx_encrypt_passwd (byte thing[], int thing_ofs, byte user_name[])
{
MD5 md5 = new MD5();
byte md5_res[];
int md5_inx;
int inx;
calculate_pass ();
md5.Init ();
//System.err.println ("thing is :" + asHex (thing));
//System.err.println ("user_name is :" + asHex (user_name));
if (user_name != null && user_name[0] != 0)
md5.Update (user_name, user_name.length);
md5.Update (the_pass, the_pass.length);
md5_res = md5.Final ();
//System.err.println ("md5 is :" + asHex (md5_res));
for (md5_inx = 0, inx = thing_ofs; inx < thing.length; inx++, md5_inx = (++md5_inx) % md5_res.length)
thing[inx] = (byte) (thing[inx] ^ md5_res[md5_inx]);
//System.err.println ("2thing is :" + asHex (thing));
}
public static String pwd_magic_encrypt (String user_name, String password)
{
try
{
byte passwd_bytes[] = password.getBytes("8859_1");
byte uname_bytes[] = user_name.getBytes("8859_1");
byte pwd[] = new byte[passwd_bytes.length + 1];
pwd[0] = 0;
System.arraycopy (passwd_bytes, 0, pwd, 1, passwd_bytes.length);
xx_encrypt_passwd (pwd, 1, uname_bytes);
return new String (pwd, "8859_1");
}
catch (java.io.UnsupportedEncodingException e)
{
}
return null;
}
}
|