1 package org.python.modules; 2 3 15 16 class md4 extends md { 17 md4(String s) { 18 super(s); 19 } 20 md4(byte in[]) { 21 super(in); 22 } 23 static int F(int x, int y, int z) { 24 return((x & y) | (~x & z)); 25 } 26 static int G(int x, int y, int z) { 27 return((x & y) | (x & z) | (y & z)); 28 } 29 static int H(int x, int y, int z) { 30 return(x ^ y ^ z); 31 } 32 void round1(int blk) { 33 A = rotintlft((A + F(B, C, D) + d[0 + 16 * blk]), 3); 34 D = rotintlft((D + F(A, B, C) + d[1 + 16 * blk]), 7); 35 C = rotintlft((C + F(D, A, B) + d[2 + 16 * blk]), 11); 36 B = rotintlft((B + F(C, D, A) + d[3 + 16 * blk]), 19); 37 38 A = rotintlft((A + F(B, C, D) + d[4 + 16 * blk]), 3); 39 D = rotintlft((D + F(A, B, C) + d[5 + 16 * blk]), 7); 40 C = rotintlft((C + F(D, A, B) + d[6 + 16 * blk]), 11); 41 B = rotintlft((B + F(C, D, A) + d[7 + 16 * blk]), 19); 42 43 A = rotintlft((A + F(B, C, D) + d[8 + 16 * blk]), 3); 44 D = rotintlft((D + F(A, B, C) + d[9 + 16 * blk]), 7); 45 C = rotintlft((C + F(D, A, B) + d[10 + 16 * blk]), 11); 46 B = rotintlft((B + F(C, D, A) + d[11 + 16 * blk]), 19); 47 48 A = rotintlft((A + F(B, C, D) + d[12 + 16 * blk]), 3); 49 D = rotintlft((D + F(A, B, C) + d[13 + 16 * blk]), 7); 50 C = rotintlft((C + F(D, A, B) + d[14 + 16 * blk]), 11); 51 B = rotintlft((B + F(C, D, A) + d[15 + 16 * blk]), 19); 52 } 53 void round2(int blk) { 54 A = rotintlft((A + G(B, C, D) + d[0 + 16 * blk] + 0x5a827999), 3); 55 D = rotintlft((D + G(A, B, C) + d[4 + 16 * blk] + 0x5a827999), 5); 56 C = rotintlft((C + G(D, A, B) + d[8 + 16 * blk] + 0x5a827999), 9); 57 B = rotintlft((B + G(C, D, A) + d[12 + 16 * blk] + 0x5a827999), 13); 58 59 A = rotintlft((A + G(B, C, D) + d[1 + 16 * blk] + 0x5a827999), 3); 60 D = rotintlft((D + G(A, B, C) + d[5 + 16 * blk] + 0x5a827999), 5); 61 C = rotintlft((C + G(D, A, B) + d[9 + 16 * blk] + 0x5a827999), 9); 62 B = rotintlft((B + G(C, D, A) + d[13 + 16 * blk] + 0x5a827999), 13); 63 64 A = rotintlft((A + G(B, C, D) + d[2 + 16 * blk] + 0x5a827999), 3); 65 D = rotintlft((D + G(A, B, C) + d[6 + 16 * blk] + 0x5a827999), 5); 66 C = rotintlft((C + G(D, A, B) + d[10 + 16 * blk] + 0x5a827999), 9); 67 B = rotintlft((B + G(C, D, A) + d[14 + 16 * blk] + 0x5a827999), 13); 68 69 A = rotintlft((A + G(B, C, D) + d[3 + 16 * blk] + 0x5a827999), 3); 70 D = rotintlft((D + G(A, B, C) + d[7 + 16 * blk] + 0x5a827999), 5); 71 C = rotintlft((C + G(D, A, B) + d[11 + 16 * blk] + 0x5a827999), 9); 72 B = rotintlft((B + G(C, D, A) + d[15 + 16 * blk] + 0x5a827999), 13); 73 74 } 75 void round3(int blk) { 76 A = rotintlft((A + H(B, C, D) + d[0 + 16 * blk] + 0x6ed9eba1), 3); 77 D = rotintlft((D + H(A, B, C) + d[8 + 16 * blk] + 0x6ed9eba1), 9); 78 C = rotintlft((C + H(D, A, B) + d[4 + 16 * blk] + 0x6ed9eba1), 11); 79 B = rotintlft((B + H(C, D, A) + d[12 + 16 * blk] + 0x6ed9eba1), 15); 80 81 A = rotintlft((A + H(B, C, D) + d[2 + 16 * blk] + 0x6ed9eba1), 3); 82 D = rotintlft((D + H(A, B, C) + d[10 + 16 * blk] + 0x6ed9eba1), 9); 83 C = rotintlft((C + H(D, A, B) + d[6 + 16 * blk] + 0x6ed9eba1), 11); 84 B = rotintlft((B + H(C, D, A) + d[14 + 16 * blk] + 0x6ed9eba1), 15); 85 86 A = rotintlft((A + H(B, C, D) + d[1 + 16 * blk] + 0x6ed9eba1), 3); 87 D = rotintlft((D + H(A, B, C) + d[9 + 16 * blk] + 0x6ed9eba1), 9); 88 C = rotintlft((C + H(D, A, B) + d[5 + 16 * blk] + 0x6ed9eba1), 11); 89 B = rotintlft((B + H(C, D, A) + d[13 + 16 * blk] + 0x6ed9eba1), 15); 90 91 A = rotintlft((A + H(B, C, D) + d[3 + 16 * blk] + 0x6ed9eba1), 3); 92 D = rotintlft((D + H(A, B, C) + d[11 + 16 * blk] + 0x6ed9eba1), 9); 93 C = rotintlft((C + H(D, A, B) + d[7 + 16 * blk] + 0x6ed9eba1), 11); 94 B = rotintlft((B + H(C, D, A) + d[15 + 16 * blk] + 0x6ed9eba1), 15); 95 96 } 97 void round4(int blk) { 98 System.out.println(" must be md5, in round4!"); 99 } 100 } 101 102 class md5 extends md { 103 md5(String s) { 104 super(s); 105 } 106 md5(byte in[]) { 107 super(in); 108 } 109 static int F(int x, int y, int z) { 110 return((x & y) | (~x & z)); 111 } 112 static int G(int x, int y, int z) { 113 return((x & z) | (y & ~z)); 114 } 115 static int H(int x, int y, int z) { 116 return(x ^ y ^ z); 117 } 118 static int I(int x, int y, int z) { 119 return(y ^ (x | ~z)); 120 } 121 void round1(int blk) { 122 A = rotintlft(A + F(B, C, D) + d[0 + 16 * blk] + 123 0xd76aa478, 7) + B; 124 D = rotintlft(D + F(A, B, C) + d[1 + 16 * blk] + 125 0xe8c7b756, 12) + A; 126 C = rotintlft(C + F(D, A, B) + d[2 + 16 * blk] + 127 0x242070db, 17) + D; 128 B = rotintlft(B + F(C, D, A) + d[3 + 16 * blk] + 129 0xc1bdceee, 22) + C; 130 131 A = rotintlft(A + F(B, C, D) + d[4 + 16 * blk] + 132 0xf57c0faf, 7) + B; 133 D = rotintlft(D + F(A, B, C) + d[5 + 16 * blk] + 134 0x4787c62a, 12) + A; 135 C = rotintlft(C + F(D, A, B) + d[6 + 16 * blk] + 136 0xa8304613, 17) + D; 137 B = rotintlft(B + F(C, D, A) + d[7 + 16 * blk] + 138 0xfd469501, 22) + C; 139 A = rotintlft(A + F(B, C, D) + d[8 + 16 * blk] + 140 0x698098d8, 7) + B; 141 D = rotintlft(D + F(A, B, C) + d[9 + 16 * blk] + 142 0x8b44f7af, 12) + A; 143 C = rotintlft(C + F(D, A, B) + d[10 + 16 * blk] + 144 0xffff5bb1, 17) + D; 145 B = rotintlft(B + F(C, D, A) + d[11 + 16 * blk] + 146 0x895cd7be, 22) + C; 147 A = rotintlft(A + F(B, C, D) + d[12 + 16 * blk] + 148 0x6b901122, 7) + B; 149 D = rotintlft(D + F(A, B, C) + d[13 + 16 * blk] + 150 0xfd987193, 12) + A; 151 C = rotintlft(C + F(D, A, B) + d[14 + 16 * blk] + 152 0xa679438e, 17) + D; 153 B = rotintlft(B + F(C, D, A) + d[15 + 16 * blk] + 154 0x49b40821, 22) + C; 155 } 156 void round2(int blk) { 157 A = rotintlft(A + G(B, C, D) + d[1 + 16 * blk] + 158 0xf61e2562, 5) + B; 159 D = rotintlft(D + G(A, B, C) + d[6 + 16 * blk] + 160 0xc040b340, 9) + A; 161 C = rotintlft(C + G(D, A, B) + d[11 + 16 * blk] + 162 0x265e5a51, 14) + D; 163 B = rotintlft(B + G(C, D, A) + d[0 + 16 * blk] + 164 0xe9b6c7aa, 20) + C; 165 A = rotintlft(A + G(B, C, D) + d[5 + 16 * blk] + 166 0xd62f105d, 5) + B; 167 D = rotintlft(D + G(A, B, C) + d[10 + 16 * blk] + 168 0x02441453, 9) + A; 169 C = rotintlft(C + G(D, A, B) + d[15 + 16 * blk] + 170 0xd8a1e681, 14) + D; 171 B = rotintlft(B + G(C, D, A) + d[4 + 16 * blk] + 172 0xe7d3fbc8, 20) + C; 173 A = rotintlft(A + G(B, C, D) + d[9 + 16 * blk] + 174 0x21e1cde6, 5) + B; 175 D = rotintlft(D + G(A, B, C) + d[14 + 16 * blk] + 176 0xc33707d6, 9) + A; 177 C = rotintlft(C + G(D, A, B) + d[3 + 16 * blk] + 178 0xf4d50d87, 14) + D; 179 B = rotintlft(B + G(C, D, A) + d[8 + 16 * blk] + 180 0x455a14ed, 20) + C; 181 A = rotintlft(A + G(B, C, D) + d[13 + 16 * blk] + 182 0xa9e3e905, 5) + B; 183 D = rotintlft(D + G(A, B, C) + d[2 + 16 * blk] + 184 0xfcefa3f8, 9) + A; 185 C = rotintlft(C + G(D, A, B) + d[7 + 16 * blk] + 186 0x676f02d9, 14) + D; 187 B = rotintlft(B + G(C, D, A) + d[12 + 16 * blk] + 188 0x8d2a4c8a, 20) + C; 189 } 190 void round3(int blk) { 191 A = rotintlft(A + H(B, C, D) + d[5 + 16 * blk] + 192 0xfffa3942, 4) + B; 193 D = rotintlft(D + H(A, B, C) + d[8 + 16 * blk] + 194 0x8771f681, 11) + A; 195 C = rotintlft(C + H(D, A, B) + d[11 + 16 * blk] + 196 0x6d9d6122, 16) + D; 197 B = rotintlft(B + H(C, D, A) + d[14 + 16 * blk] + 198 0xfde5380c, 23) + C; 199 A = rotintlft(A + H(B, C, D) + d[1 + 16 * blk] + 200 0xa4beea44, 4) + B; 201 D = rotintlft(D + H(A, B, C) + d[4 + 16 * blk] + 202 0x4bdecfa9, 11) + A; 203 C = rotintlft(C + H(D, A, B) + d[7 + 16 * blk] + 204 0xf6bb4b60, 16) + D; 205 B = rotintlft(B + H(C, D, A) + d[10 + 16 * blk] + 206 0xbebfbc70, 23) + C; 207 A = rotintlft(A + H(B, C, D) + d[13 + 16 * blk] + 208 0x289b7ec6, 4) + B; 209 D = rotintlft(D + H(A, B, C) + d[0 + 16 * blk] + 210 0xeaa127fa, 11) + A; 211 C = rotintlft(C + H(D, A, B) + d[3 + 16 * blk] + 212 0xd4ef3085, 16) + D; 213 B = rotintlft(B + H(C, D, A) + d[6 + 16 * blk] + 214 0x04881d05, 23) + C; 215 A = rotintlft(A + H(B, C, D) + d[9 + 16 * blk] + 216 0xd9d4d039, 4) + B; 217 D = rotintlft(D + H(A, B, C) + d[12 + 16 * blk] + 218 0xe6db99e5, 11) + A; 219 C = rotintlft(C + H(D, A, B) + d[15 + 16 * blk] + 220 0x1fa27cf8, 16) + D; 221 B = rotintlft(B + H(C, D, A) + d[2 + 16 * blk] + 222 0xc4ac5665, 23) + C; 223 } 224 void round4(int blk) { 225 A = rotintlft(A + I(B, C, D) + d[0 + 16 * blk] + 226 0xf4292244, 6) + B; 227 D = rotintlft(D + I(A, B, C) + d[7 + 16 * blk] + 228 0x432aff97, 10) + A; 229 C = rotintlft(C + I(D, A, B) + d[14 + 16 * blk] + 230 0xab9423a7, 15) + D; 231 B = rotintlft(B + I(C, D, A) + d[5 + 16 * blk] + 232 0xfc93a039, 21) + C; 233 A = rotintlft(A + I(B, C, D) + d[12 + 16 * blk] + 234 0x655b59c3, 6) + B; 235 D = rotintlft(D + I(A, B, C) + d[3 + 16 * blk] + 236 0x8f0ccc92, 10) + A; 237 C = rotintlft(C + I(D, A, B) + d[10 + 16 * blk] + 238 0xffeff47d, 15) + D; 239 B = rotintlft(B + I(C, D, A) + d[1 + 16 * blk] + 240 0x85845dd1, 21) + C; 241 A = rotintlft(A + I(B, C, D) + d[8 + 16 * blk] + 242 0x6fa87e4f, 6) + B; 243 D = rotintlft(D + I(A, B, C) + d[15 + 16 * blk] + 244 0xfe2ce6e0, 10) + A; 245 C = rotintlft(C + I(D, A, B) + d[6 + 16 * blk] + 246 0xa3014314, 15) + D; 247 B = rotintlft(B + I(C, D, A) + d[13 + 16 * blk] + 248 0x4e0811a1, 21) + C; 249 A = rotintlft(A + I(B, C, D) + d[4 + 16 * blk] + 250 0xf7537e82, 6) + B; 251 D = rotintlft(D + I(A, B, C) + d[11 + 16 * blk] + 252 0xbd3af235, 10) + A; 253 C = rotintlft(C + I(D, A, B) + d[2 + 16 * blk] + 254 0x2ad7d2bb, 15) + D; 255 B = rotintlft(B + I(C, D, A) + d[9 + 16 * blk] + 256 0xeb86d391, 21) + C; 257 } 258 } 259 260 class md { 261 int A,B,C,D; 262 int d[]; 263 int numwords; 264 265 268 public static void main(String argv[]) { 269 boolean doinmd4; 270 String mdtype; 271 272 String str[] = { "" , "a", "abc", "message digest", 273 "abcdefghijklmnopqrstuvwxyz", 274 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 275 "12345678901234567890123456789012345678901234567890123456789012" + 276 "345678901234567890", 277 "01234567890123456789012345678901234567890123456789012345"}; 278 279 if (argv.length == 0) { 280 mdtype = "md4"; 281 doinmd4 = true; 282 } else if (argv.length > 1) { 283 System.err.println("Usage: md [4|5|md4|md5]"); 284 return; 285 } else if ((argv[0].equals("4")) || (argv[0].equals("md4"))) { 286 mdtype = "md4"; 287 doinmd4 = true; 288 } else if ((argv[0].equals("5")) || (argv[0].equals("md5"))) { 289 mdtype = "md5"; 290 doinmd4 = false; 291 } else { 292 System.err.println("Usage: md [4|5|md4|md5]"); 293 return; 294 } 295 for(int i = 0; i < str.length; i++) { 296 if (doinmd4) { 297 md4 mdc = new md4(str[i]); 298 mdc.calc(); 299 System.out.println(mdtype + "(\"" + str[i] + "\") = " + mdc); 300 } else { 301 md5 mdc = new md5(str[i]); 302 mdc.calc(); 303 System.out.println(mdtype + "(\"" + str[i] + "\") = " + mdc); 304 } 305 } 306 } 307 md (String s) { 308 byte in[] = new byte[s.length()]; 309 int i; 310 311 for(i=0; i < s.length(); i++) { 312 in[i] = (byte) (s.charAt(i) & 0xff); 313 } 314 mdinit(in); 315 } 316 md (byte in[]) { 317 mdinit(in); 318 } 319 320 void mdinit (byte in[]) { 321 int newlen, endblklen, pad, i; 322 long datalenbits; 323 324 datalenbits = in.length * 8; 325 endblklen = in.length % 64; 326 if (endblklen < 56) { 327 pad = 64 - endblklen; 328 } else { 329 pad = (64 - endblklen) + 64; 330 } 331 newlen = in.length + pad; 332 byte b[] = new byte[newlen]; 333 for(i=0; i < in.length; i++) { 334 b[i] = in[i]; 335 } 336 b[in.length] = (byte) 0x80; 337 for (i = b.length + 1; i < (newlen - 8); i++) { 338 b[i] = 0; 339 } 340 for (i = 0; i < 8; i++) { 341 b[newlen - 8 + i] = (byte) (datalenbits & 0xff); 342 datalenbits >>= 8; 343 } 344 345 A = 0x67452301; 346 B = 0xefcdab89; 347 C = 0x98badcfe; 348 D = 0x10325476; 349 this.numwords = newlen/4; 350 this.d = new int[this.numwords]; 351 for (i = 0; i < newlen; i += 4) { 352 this.d[i/4] = (b[i] & 0xff) + ((b[i+1] & 0xff) << 8) + 353 ((b[i+2] & 0xff) << 16) + ((b[i+3] & 0xff) << 24); 354 } 355 } 356 357 public String toString() { 358 String s; 359 360 return(tohex(A) + tohex(B) + tohex(C) + tohex(D)); 361 } 362 int[] getregs() { 363 int regs[] = {this.A, this.B, this.C, this.D}; 364 365 return regs; 366 } 367 void calc() { 368 int AA, BB, CC, DD, i; 369 370 for(i=0; i < numwords/16; i++) { 371 AA = A; BB = B; CC = C; DD = D; 372 round1(i); 373 round2(i); 374 round3(i); 375 if (this instanceof md5) { 376 round4(i); 377 } 378 A += AA; B+= BB; C+= CC; D+= DD; 379 } 380 } 381 384 void round1(int blk) { 385 System.err.println("Danger! Danger! Someone called md.round1()!"); 386 } 387 void round2(int blk) { 388 System.err.println("Danger! Danger! Someone called md.round2()!"); 389 } 390 void round3(int blk) { 391 System.err.println("Danger! Danger! Someone called md.round3()!"); 392 } 393 void round4(int blk) { 394 System.err.println("Danger! Danger! Someone called md.round4()!"); 395 } 396 static int rotintlft(int val, int numbits) { 397 return((val << numbits) | (val >>> (32 - numbits))); 398 } 399 static String tohex(int i) { 400 int b; 401 String tmpstr; 402 403 tmpstr = ""; 404 for(b = 0; b < 4; b++) { 405 tmpstr += Integer.toString((i >> 4) & 0xf, 16) 406 + Integer.toString(i & 0xf, 16); 407 i >>= 8; 408 } 409 return tmpstr; 410 } 411 static md new_md5(String data) { 412 return new md5(data); 413 } 414 415 } 416 | Popular Tags |