编程语言
首页 > 编程语言> > 全球首创基于普通三进制逻辑数学(伍氏定律)实现的MD3哈希码算法程序

全球首创基于普通三进制逻辑数学(伍氏定律)实现的MD3哈希码算法程序

作者:互联网

发布 C 源码

  1 /*********************************************************
  2 * 作者:伍耀晖               Author: YaoHui.Wu           *
  3 * 开源日期:2022年6月11日    Open Source Date: 2022-6-11 *
  4 * 国家:中国                 Country: China              *
  5 *********************************************************/
  6 
  7 #include <fcntl.h>
  8 #include <stdio.h>
  9 #include <stdlib.h>
 10 #include <sys/types.h>
 11 #include <sys/stat.h>
 12 
 13 #define    lShift11    2
 14 
 15 #define    lShift12    3
 16 
 17 #define    lShift13    4
 18 
 19 #define    lShift14    5
 20 
 21 #define    lShift21    6
 22 
 23 #define    lShift22    7
 24 
 25 #define    lShift23    8
 26 
 27 #define    lShift24    11
 28 
 29 #define    lShift31    10
 30 
 31 #define    lShift32    13
 32 
 33 #define    lShift33    14
 34 
 35 #define    lShift34    17
 36 
 37 #define    lShift41    16
 38 
 39 #define    lShift42    19
 40 
 41 #define    lShift43    22
 42 
 43 #define    lShift44    23
 44 
 45 void Usage()
 46 {
 47     printf("Usage: MD3Hash YouWantToHash.File\n");
 48 }
 49 
 50 void Ternary(long long lNumeric,
 51              unsigned char *ucpTrinary)
 52 {
 53     ucpTrinary[0] = ucpTrinary[1] = ucpTrinary[2] = ucpTrinary[3] = ucpTrinary[4] = ucpTrinary[5] = ucpTrinary[6] = ucpTrinary[7] = ucpTrinary[8] = ucpTrinary[9] = ucpTrinary[10] = ucpTrinary[11] = ucpTrinary[12] = ucpTrinary[13] = ucpTrinary[14] = ucpTrinary[15] = ucpTrinary[16] = ucpTrinary[17] = ucpTrinary[18] = ucpTrinary[19] = ucpTrinary[20] = ucpTrinary[21] = ucpTrinary[22] = ucpTrinary[23] = 0;
 54 
 55     if(lNumeric != 0)
 56     {
 57         for(long long i = 23; i > -1; --i)
 58         {
 59             ucpTrinary[i] = lNumeric % 3;
 60 
 61             lNumeric /= 3;
 62         }
 63     }
 64 }
 65 
 66 // 0 0 0
 67 // 0 1 1
 68 // 0 1 2
 69 
 70 void TrinaryAnd(unsigned char *w,
 71                 unsigned char *x,
 72                 unsigned char *y)
 73 {
 74     for(long long i = 0; i < 24; ++i)
 75     {
 76         if(x[i] == 0 && y[i] == 0)
 77         {
 78             w[i] = 0;
 79         }
 80         else if(x[i] == 0 && y[i] == 1)
 81         {
 82             w[i] = 0;
 83         }
 84         else if(x[i] == 0 && y[i] == 2)
 85         {
 86             w[i] = 0;
 87         }
 88         else if(x[i] == 1 && y[i] == 0)
 89         {
 90             w[i] = 0;
 91         }
 92         else if(x[i] == 1 && y[i] == 1)
 93         {
 94             w[i] = 1;
 95         }
 96         else if(x[i] == 1 && y[i] == 2)
 97         {
 98             w[i] = 1;
 99         }
100         else if(x[i] == 2 && y[i] == 0)
101         {
102             w[i] = 0;
103         }
104         else if(x[i] == 2 && y[i] == 1)
105         {
106             w[i] = 1;
107         }
108         else if(x[i] == 2 && y[i] == 2)
109         {
110             w[i] = 2;
111         }
112     }
113 }
114 
115 // 0 1 2
116 // 1 1 2
117 // 2 2 2
118 
119 void TrinaryOr(unsigned char *w,
120                unsigned char *x,
121                unsigned char *y)
122 {
123     for(long long j = 0; j < 24; ++j)
124     {
125         if(x[j] == 0 && y[j] == 0)
126         {
127             w[j] = 0;
128         }
129         else if(x[j] == 0 && y[j] == 1)
130         {
131             w[j] = 1;
132         }
133         else if(x[j] == 0 && y[j] == 2)
134         {
135             w[j] = 2;
136         }
137         else if(x[j] == 1 && y[j] == 0)
138         {
139             w[j] = 1;
140         }
141         else if(x[j] == 1 && y[j] == 1)
142         {
143             w[j] = 1;
144         }
145         else if(x[j] == 1 && y[j] == 2)
146         {
147             w[j] = 2;
148         }
149         else if(x[j] == 2 && y[j] == 0)
150         {
151             w[j] = 2;
152         }
153         else if(x[j] == 2 && y[j] == 1)
154         {
155             w[j] = 2;
156         }
157         else if(x[j] == 2 && y[j] == 2)
158         {
159             w[j] = 2;
160         }
161     }
162 }
163 
164 // 0 0 2
165 // 1 1 1
166 // 2 2 0
167 
168 void TrinaryXor(unsigned char *w,
169                 unsigned char *x,
170                 unsigned char *y)
171 {
172     for(long long k = 0; k < 24; ++k)
173     {
174         if(x[k] == 0 && y[k] == 0)
175         {
176             w[k] = 0;
177         }
178         else if(x[k] == 0 && y[k] == 1)
179         {
180             w[k] = 0;
181         }
182         else if(x[k] == 0 && y[k] == 2)
183         {
184             w[k] = 2;
185         }
186         else if(x[k] == 1 && y[k] == 0)
187         {
188             w[k] = 1;
189         }
190         else if(x[k] == 1 && y[k] == 1)
191         {
192             w[k] = 1;
193         }
194         else if(x[k] == 1 && y[k] == 2)
195         {
196             w[k] = 1;
197         }
198         else if(x[k] == 2 && y[k] == 0)
199         {
200             w[k] = 2;
201         }
202         else if(x[k] == 2 && y[k] == 1)
203         {
204             w[k] = 2;
205         }
206         else if(x[k] == 2 && y[k] == 2)
207         {
208             w[k] = 0;
209         }
210     }
211 }
212 
213 // 2 0 0
214 // 1 1 1
215 // 0 2 2
216 
217 void TrinaryXand(unsigned char *w,
218                  unsigned char *x,
219                  unsigned char *y)
220 {
221     for(long long i = 0; i < 24; ++i)
222     {
223         if(x[i] == 0 && y[i] == 0)
224         {
225             w[i] = 2;
226         }
227         else if(x[i] == 0 && y[i] == 1)
228         {
229             w[i] = 0;
230         }
231         else if(x[i] == 0 && y[i] == 2)
232         {
233             w[i] = 0;
234         }
235         else if(x[i] == 1 && y[i] == 0)
236         {
237             w[i] = 1;
238         }
239         else if(x[i] == 1 && y[i] == 1)
240         {
241             w[i] = 1;
242         }
243         else if(x[i] == 1 && y[i] == 2)
244         {
245             w[i] = 1;
246         }
247         else if(x[i] == 2 && y[i] == 0)
248         {
249             w[i] = 0;
250         }
251         else if(x[i] == 2 && y[i] == 1)
252         {
253             w[i] = 2;
254         }
255         else if(x[i] == 2 && y[i] == 2)
256         {
257             w[i] = 2;
258         }
259     }
260 }
261 
262 // 0 1 2
263 // 1 2 0
264 // 2 0 1
265 
266 void TrinaryAdd(unsigned char *w,
267                 unsigned char *x,
268                 unsigned char *y)
269 {
270     for(long long j = 0; j < 24; ++j)
271     {
272         if(x[j] == 0 && y[j] == 0)
273         {
274             w[j] = 0;
275         }
276         else if(x[j] == 0 && y[j] == 1)
277         {
278             w[j] = 1;
279         }
280         else if(x[j] == 0 && y[j] == 2)
281         {
282             w[j] = 2;
283         }
284         else if(x[j] == 1 && y[j] == 0)
285         {
286             w[j] = 1;
287         }
288         else if(x[j] == 1 && y[j] == 1)
289         {
290             w[j] = 2;
291         }
292         else if(x[j] == 1 && y[j] == 2)
293         {
294             w[j] = 0;
295         }
296         else if(x[j] == 2 && y[j] == 0)
297         {
298             w[j] = 2;
299         }
300         else if(x[j] == 2 && y[j] == 1)
301         {
302             w[j] = 0;
303         }
304         else if(x[j] == 2 && y[j] == 2)
305         {
306             w[j] = 1;
307         }
308     }
309 }
310 
311 // 0 0 0
312 // 0 1 2
313 // 0 2 1
314 
315 void TrinaryMultiplication(unsigned char *w,
316                            unsigned char *x,
317                            unsigned char *y)
318 {
319     for(long long k = 0; k < 24; ++k)
320     {
321         if(x[k] == 0 && y[k] == 0)
322         {
323             w[k] = 0;
324         }
325         else if(x[k] == 0 && y[k] == 1)
326         {
327             w[k] = 0;
328         }
329         else if(x[k] == 0 && y[k] == 2)
330         {
331             w[k] = 0;
332         }
333         else if(x[k] == 1 && y[k] == 0)
334         {
335             w[k] = 0;
336         }
337         else if(x[k] == 1 && y[k] == 1)
338         {
339             w[k] = 1;
340         }
341         else if(x[k] == 1 && y[k] == 2)
342         {
343             w[k] = 2;
344         }
345         else if(x[k] == 2 && y[k] == 0)
346         {
347             w[k] = 0;
348         }
349         else if(x[k] == 2 && y[k] == 1)
350         {
351             w[k] = 2;
352         }
353         else if(x[k] == 2 && y[k] == 2)
354         {
355             w[k] = 1;
356         }
357     }
358 }
359 
360 void F(unsigned char *w,
361        unsigned char *x,
362        unsigned char *y,
363        unsigned char *z)
364 {
365     TrinaryAnd(w, x, y);
366 
367     TrinaryOr(w, w, z);
368 }
369 
370 void G(unsigned char *w,
371        unsigned char *x,
372        unsigned char *y,
373        unsigned char *z)
374 {
375     TrinaryAdd(w, x, z);
376 
377     TrinaryMultiplication(w, w, y);
378 }
379 
380 void H(unsigned char *w,
381        unsigned char *x,
382        unsigned char *y,
383        unsigned char *z)
384 {
385     TrinaryXor(w, x, y);
386 
387     TrinaryXand(w, w, z);
388 }
389 
390 void I(unsigned char *w,
391        unsigned char *x,
392        unsigned char *y,
393        unsigned char *z)
394 {
395     TrinaryMultiplication(w, y, z);
396 
397     TrinaryAdd(w, w, x);
398 }
399 
400 void Add(unsigned char *w,
401          unsigned char *z)
402 {
403     long long lCarry = 0;
404 
405     for(long long i = 0; i < 24; ++i)
406     {
407         w[i] += z[i] + lCarry;
408 
409         if(w[i] > 2)
410         {
411             w[i] -= 3;
412 
413             if(lCarry == 0)
414             {
415                 lCarry = 1;
416             }
417         }
418         else
419         {
420             lCarry = 0;
421         }
422     }
423 }
424 
425 void RotateShiftLeft(unsigned char *w,
426                      long long lBit)
427 {
428     unsigned char ucaSwap[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
429 
430     for(long long j = 23; j > -1; --j)
431     {
432         ucaSwap[j] = w[j >= lBit ? j - lBit : 24 - lBit + j];
433     }
434 
435     for(long long k = 0; k < 24; ++k)
436     {
437         w[k] = ucaSwap[k];
438     }
439 }
440 
441 void FF(unsigned char *a,
442         unsigned char *b,
443         unsigned char *c,
444         unsigned char *d,
445         unsigned char *e,
446         long long lShift,
447         const unsigned char *k)
448 {
449     unsigned char ucaTemp[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
450 
451     F(ucaTemp, b, c, d);
452 
453     Add(ucaTemp, a);
454 
455     Add(ucaTemp, e);
456 
457     Add(ucaTemp, (unsigned char*)k);
458 
459     RotateShiftLeft(ucaTemp, lShift);
460 
461     Add(ucaTemp, b);
462 
463     for(long long i = 0; i < 24; ++i)
464     {
465         a[i] = ucaTemp[i];
466     }
467 }
468 
469 void GG(unsigned char *a,
470         unsigned char *b,
471         unsigned char *c,
472         unsigned char *d,
473         unsigned char *e,
474         long long lShift,
475         const unsigned char *k)
476 {
477     unsigned char ucaTemp[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
478 
479     G(ucaTemp, c, d, a);
480 
481     Add(ucaTemp, b);
482 
483     Add(ucaTemp, e);
484 
485     Add(ucaTemp, (unsigned char*)k);
486 
487     RotateShiftLeft(ucaTemp, lShift);
488 
489     Add(ucaTemp, c);
490 
491     for(long long j = 0; j < 24; ++j)
492     {
493         b[j] = ucaTemp[j];
494     }
495 }
496 
497 void HH(unsigned char *a,
498         unsigned char *b,
499         unsigned char *c,
500         unsigned char *d,
501         unsigned char *e,
502         long long lShift,
503         const unsigned char *k)
504 {
505     unsigned char ucaTemp[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
506 
507     H(ucaTemp, d, a, b);
508 
509     Add(ucaTemp, c);
510 
511     Add(ucaTemp, e);
512 
513     Add(ucaTemp, (unsigned char*)k);
514 
515     RotateShiftLeft(ucaTemp, lShift);
516 
517     Add(ucaTemp, d);
518 
519     for(long long i = 0; i < 24; ++i)
520     {
521         c[i] = ucaTemp[i];
522     }
523 }
524 
525 void II(unsigned char *a,
526         unsigned char *b,
527         unsigned char *c,
528         unsigned char *d,
529         unsigned char *e,
530         long long lShift,
531         const unsigned char *k)
532 {
533     unsigned char ucaTemp[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
534 
535     I(ucaTemp, a, b, c);
536 
537     Add(ucaTemp, d);
538 
539     Add(ucaTemp, e);
540 
541     Add(ucaTemp, (unsigned char*)k);
542 
543     RotateShiftLeft(ucaTemp, lShift);
544 
545     Add(ucaTemp, a);
546 
547     for(long long j = 0; j < 24; ++j)
548     {
549         d[j] = ucaTemp[j];
550     }
551 }
552 
553 void MD3Hash(unsigned char *a,
554              unsigned char *b,
555              unsigned char *c,
556              unsigned char *d,
557              unsigned char e[][24])
558 {
559     const unsigned char k0[] = {2, 1, 1, 1, 2, 2, 0, 1, 2, 2, 1, 1, 2, 1, 2, 2, 2, 0, 0, 0, 1, 0, 0, 0},
560 
561                         k1[] = {1, 1, 2, 0, 0, 1, 0, 0, 0, 1, 0, 2, 1, 1, 0, 2, 0, 0, 1, 0, 1, 0, 0, 0},
562 
563                         k2[] = {0, 0, 2, 2, 1, 2, 0, 0, 1, 1, 1, 1, 0, 2, 0, 0, 2, 1, 1, 0, 0, 0, 0, 0},
564 
565                         k3[] = {1, 1, 1, 0, 1, 0, 0, 2, 1, 1, 2, 0, 2, 1, 1, 1, 0, 1, 2, 2, 0, 0, 0, 0},
566 
567                         k4[] = {1, 2, 1, 1, 2, 0, 1, 2, 2, 2, 0, 2, 0, 0, 0, 2, 2, 1, 1, 0, 1, 0, 0, 0},
568 
569                         k5[] = {2, 1, 0, 2, 1, 2, 1, 0, 1, 1, 1, 0, 2, 2, 1, 2, 0, 0, 0, 1, 0, 0, 0, 0},
570 
571                         k6[] = {2, 0, 0, 1, 0, 0, 1, 0, 0, 1, 2, 1, 2, 2, 1, 1, 2, 0, 1, 2, 0, 0, 0, 0},
572 
573                         k7[] = {2, 2, 1, 0, 0, 2, 1, 1, 2, 1, 0, 2, 0, 1, 0, 2, 2, 2, 1, 0, 1, 0, 0, 0},
574 
575                         k8[] = {1, 2, 0, 0, 1, 0, 0, 1, 0, 2, 2, 1, 0, 0, 1, 0, 2, 1, 1, 1, 0, 0, 0, 0},
576 
577                         k9[] = {2, 1, 1, 2, 2, 2, 1, 1, 0, 2, 2, 1, 1, 1, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0},
578 
579                         k10[] = {1, 2, 2, 0, 1, 2, 0, 1, 2, 2, 2, 1, 2, 2, 0, 2, 0, 0, 2, 0, 1, 0, 0, 0},
580 
581                         k11[] = {1, 1, 0, 2, 2, 0, 1, 2, 2, 2, 0, 1, 1, 2, 1, 1, 2, 2, 2, 1, 0, 0, 0, 0},
582 
583                         k12[] = {2, 0, 1, 2, 1, 2, 0, 0, 1, 0, 0, 2, 2, 0, 2, 2, 2, 1, 1, 1, 0, 0, 0, 0},
584 
585                         k13[] = {2, 2, 2, 0, 2 ,2, 1, 0, 1, 1, 1, 2, 1, 1, 1, 2, 2, 2, 1, 0, 1, 0, 0, 0},
586 
587                         k14[] = {1, 0, 1, 1, 0, 2, 2, 2, 0, 0, 1, 1, 2, 2, 1, 2, 1, 0, 1, 2, 0, 0, 0, 0},
588 
589                         k15[] = {0, 1, 2, 0, 2, 1, 1, 1, 1, 2, 0, 2, 1, 1, 0, 2, 1, 0, 0, 1, 0, 0, 0, 0},
590 
591                         k16[] = {0, 0, 1, 0, 1, 1, 1, 2, 1, 2 ,0, 2, 2, 0, 2, 2, 2, 1, 1, 0, 1, 0, 0, 0},
592 
593                         k17[] = {1, 2, 0, 2, 0, 0, 2, 2, 1, 1, 2, 0, 0, 1, 2, 2, 2, 0, 2, 2, 0, 0, 0, 0},
594 
595                         k18[] = {0, 1, 2, 0, 0, 2, 0, 2, 0, 1, 2, 0, 2, 1, 2, 2, 2, 1, 1, 0, 0, 0, 0, 0},
596 
597                         k19[] = {1, 2, 1, 1, 1, 2, 2, 2, 2, 1, 1, 0, 1, 2, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0},
598 
599                         k20[] = {1, 2, 1, 0, 2, 2, 1, 0, 0, 2, 2, 1, 2, 0, 1, 1, 2, 0, 0, 0, 1, 0, 0, 0},
600 
601                         k21[] = {2, 0, 0, 1, 2, 0, 2, 0, 1, 2, 1, 1, 2, 2, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0},
602 
603                         k22[] = {1, 2, 0, 0, 2, 1, 1, 1, 0, 1, 2, 2, 1, 2, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0},
604 
605                         k23[] = {2, 1, 1, 1, 1, 0, 1, 1, 1, 2, 2, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0},
606 
607                         k24[] = {0, 1, 2, 0, 2, 2, 1, 0, 0, 2, 2, 1, 1, 2, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0},
608 
609                         k25[] = {0, 1, 2, 1, 1, 2, 2, 1, 1, 0, 1, 2, 0, 2, 0, 0, 1, 1, 2, 2, 0, 0, 0, 0},
610 
611                         k26[] = {2, 1, 2, 0, 1, 1, 2, 1, 2, 1, 1, 0, 1, 2, 0, 1, 2, 1, 1, 0, 1, 0, 0, 0},
612 
613                         k27[] = {2, 2, 0, 1, 1, 0, 2, 1, 1, 1, 0, 1, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
614 
615                         k28[] = {1, 1, 2, 2, 0, 0, 2, 1, 1, 2, 2, 0, 2, 2, 1, 0, 0, 1, 1, 2, 0, 0, 0, 0},
616 
617                         k29[] = {2, 2, 2, 2, 0, 2, 0, 0, 1, 0, 0, 0, 2, 0, 2, 1, 2, 2, 1, 0, 1, 0, 0, 0},
618 
619                         k30[] = {1, 2, 2, 0, 1, 0, 1, 1, 2, 2, 2, 0, 1, 2, 2, 0, 1, 1, 1, 1, 0, 0, 0, 0},
620 
621                         k31[] = {1, 1, 2, 2, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0},
622 
623                         k32[] = {1, 1, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 2, 2, 0, 2, 0, 0, 2, 0, 1, 0, 0, 0},
624 
625                         k33[] = {2, 1, 1, 1, 2, 2, 1, 1, 1, 0, 2, 2, 0, 0, 1, 2, 1, 2, 2, 1, 0, 0, 0, 0},
626 
627                         k34[] = {1, 0, 0, 0, 0, 2, 2, 0, 1, 0, 1, 1, 1, 1, 0, 2, 0, 2, 1, 1, 0, 0, 0, 0},
628 
629                         k35[] = {1, 1, 1, 0, 2, 2, 0, 0, 0, 2, 2, 0, 2, 1, 2, 2, 2, 2, 1, 0, 1, 0, 0, 0},
630 
631                         k36[] = {2, 1, 0, 0, 2, 0, 1, 1, 1, 0, 2, 2, 1, 2, 1, 0, 1, 0, 1, 2, 0, 0, 0, 0},
632 
633                         k37[] = {1, 2, 0, 2, 0, 1, 0, 0, 2, 1, 1, 0, 1, 0, 2, 1, 2, 0, 0, 1, 0, 0, 0, 0},
634 
635                         k38[] = {1, 2, 0, 2, 1, 2, 1, 1, 2, 0, 1, 0, 1, 1, 1, 0, 0, 2, 1, 0, 1, 0, 0, 0},
636 
637                         k39[] = {0, 2, 1, 1, 0, 1, 2, 1, 2, 0, 1, 2, 0, 0, 0, 1, 2, 0, 2, 2, 0, 0, 0, 0},
638 
639                         k40[] = {0, 0, 0, 0, 0, 1, 0, 2, 1, 1, 2, 2, 0, 1, 1, 2, 0, 2, 1, 0, 0, 0, 0, 0},
640 
641                         k41[] = {0, 1, 1, 0, 2, 2, 0, 0, 1, 2, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0},
642 
643                         k42[] = {2, 1, 1, 0, 0, 2, 0, 0, 0, 2, 1, 0, 2, 2, 2, 2, 1, 0, 0, 0, 1, 0, 0, 0},
644 
645                         k43[] = {0, 2, 2, 2, 0, 1, 0, 0, 2, 1, 0, 0, 2, 2, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0},
646 
647                         k44[] = {1, 2, 1, 0, 2, 1, 1, 0, 0, 0, 1, 2, 0, 0, 2, 2, 0, 1, 0, 0, 1, 0, 0, 0},
648 
649                         k45[] = {0, 1, 0, 2, 2, 2, 0, 1, 1, 0, 0, 0, 1, 2, 2, 2, 2, 2, 0, 0, 1, 0, 0, 0},
650 
651                         k46[] = {1, 0, 1, 2, 1, 2, 1, 1, 1, 0, 0, 2, 2, 2, 2, 0, 0, 1, 1, 0, 0, 0, 0, 0},
652 
653                         k47[] = {0, 0, 1, 0, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, 2, 1, 1, 1, 2, 2, 0, 0, 0, 0},
654 
655                         k48[] = {0, 2, 2, 2, 1, 0, 0, 1, 1, 2, 2, 2, 0, 1, 1, 0, 2, 1, 1, 0, 1, 0, 0, 0},
656 
657                         k49[] = {2, 0, 0, 2, 0, 1, 0, 0, 0, 0, 1, 1, 2, 1, 1, 0, 2, 2, 2, 0, 0, 0, 0, 0},
658 
659                         k50[] = {2, 0, 2, 1, 1, 2, 2, 2, 1, 1, 2, 1, 1, 2, 1, 2, 0, 1, 1, 2, 0, 0, 0, 0},
660 
661                         k51[] = {1, 2, 1, 0, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 0, 1, 2, 2, 1, 0, 1, 0, 0, 0},
662 
663                         k52[] = {2, 0, 0, 0, 0, 2, 1, 2, 1, 2, 0, 2, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0},
664 
665                         k53[] = {1, 0, 2, 0, 2, 1, 2, 2, 1, 2, 2, 2, 0, 2, 0, 2, 1, 0, 0, 2, 0, 0, 0, 0},
666 
667                         k54[] = {2, 1, 1, 2, 0, 0, 2, 1, 1, 2, 0, 2, 0, 2, 0, 2, 0, 0, 2, 0, 1, 0, 0, 0},
668 
669                         k55[] = {0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 2, 2, 1, 0, 0, 0, 0},
670 
671                         k56[] = {1, 2, 0, 1, 1, 2, 1, 1, 0, 2, 2, 2, 2, 1, 1, 1, 1, 2, 1, 1, 0, 0, 0, 0},
672 
673                         k57[] = {2, 1, 1, 2, 0, 0, 1, 0, 2, 0, 1, 0, 2, 1, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0},
674 
675                         k58[] = {2, 2, 1, 0, 0, 2, 2, 2, 1, 1, 2, 2, 0, 2, 1, 1, 0, 0, 1, 2, 0, 0, 0, 0},
676 
677                         k59[] = {0, 1, 0, 0, 1, 1, 0, 1, 2, 1, 0, 1, 0, 2, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0},
678 
679                         k60[] = {1, 1, 1, 1, 0, 1, 1, 2, 1, 0, 2, 2, 1, 1, 0, 1, 0, 2, 1, 0, 1, 0, 0, 0},
680 
681                         k61[] = {2, 1, 1, 2, 0, 2, 0, 0, 1, 2, 1, 2, 0, 2, 0, 2, 1, 0, 2, 2, 0, 0, 0, 0},
682 
683                         k62[] = {0, 0, 1, 2, 0, 2, 1, 1, 0, 2, 1, 1, 2, 0, 0, 2, 1, 2, 1, 0, 0, 0, 0, 0},
684 
685                         k63[] = {2, 0, 2, 0, 2, 2, 1, 0, 0, 2, 0, 1, 1, 0, 1, 2, 1, 0, 1, 0, 1, 0, 0, 0};
686 
687     FF(a, b, c, d, e[ 0], lShift11, k0);
688 
689     FF(b, c, d, a, e[ 1], lShift12, k1);
690 
691     FF(c, d, a, b, e[ 2], lShift13, k2);
692 
693     FF(d, a, b, c, e[ 3], lShift14, k3);
694 
695     FF(a, b, c, d, e[ 4], lShift11, k4);
696 
697     FF(b, c, d, a, e[ 5], lShift12, k5);
698 
699     FF(c, d, a, b, e[ 6], lShift13, k6);
700 
701     FF(d, a, b, c, e[ 7], lShift14, k7);
702 
703     FF(a, b, c, d, e[ 8], lShift11, k8);
704 
705     FF(b, c, d, a, e[ 9], lShift12, k9);
706 
707     FF(c, d, a, b, e[10], lShift13, k10);
708 
709     FF(d, a, b, c, e[11], lShift14, k11);
710 
711     FF(a, b, c, d, e[12], lShift11, k12);
712 
713     FF(b, c, d, a, e[13], lShift12, k13);
714 
715     FF(c, d, a, b, e[14], lShift13, k14);
716 
717     FF(d, a, b, c, e[15], lShift14, k15);
718 
719     GG(a, b, c, d, e[ 1], lShift21, k16);
720 
721     GG(d, a, b, c, e[ 6], lShift22, k17);
722 
723     GG(c, d, a, b, e[11], lShift23, k18);
724 
725     GG(b, c, d, a, e[ 0], lShift24, k19);
726 
727     GG(a, b, c, d, e[ 5], lShift21, k20);
728 
729     GG(d, a, b, c, e[10], lShift22, k21);
730 
731     GG(c, d, a, b, e[15], lShift23, k22);
732 
733     GG(b, c, d, a, e[ 4], lShift24, k23);
734 
735     GG(a, b, c, d, e[ 9], lShift21, k24);
736 
737     GG(d, a, b, c, e[14], lShift22, k25);
738 
739     GG(c, d, a, b, e[ 3], lShift23, k26);
740 
741     GG(b, c, d, a, e[ 8], lShift24, k27);
742 
743     GG(a, b, c, d, e[13], lShift21, k28);
744 
745     GG(d, a, b, c, e[ 2], lShift22, k29);
746 
747     GG(c, d, a, b, e[ 7], lShift23, k30);
748 
749     GG(b, c, d, a, e[12], lShift24, k31);
750 
751     HH(a, b, c, d, e[ 5], lShift31, k32);
752 
753     HH(b, c, d, a, e[ 8], lShift32, k33);
754 
755     HH(c, d, a, b, e[11], lShift33, k34);
756 
757     HH(d, a, b, c, e[14], lShift34, k35);
758 
759     HH(a, b, c, d, e[ 1], lShift31, k36);
760 
761     HH(b, c, d, a, e[ 4], lShift32, k37);
762 
763     HH(c, d, a, b, e[ 7], lShift33, k38);
764 
765     HH(d, a, b, c, e[10], lShift34, k39);
766 
767     HH(a, b, c, d, e[13], lShift31, k40);
768 
769     HH(b, c, d, a, e[ 0], lShift32, k41);
770 
771     HH(c, d, a, b, e[ 3], lShift33, k42);
772 
773     HH(d, a, b, c, e[ 6], lShift34, k43);
774 
775     HH(a, b, c, d, e[ 9], lShift31, k44);
776 
777     HH(b, c, d, a, e[12], lShift32, k45);
778 
779     HH(c, d, a, b, e[15], lShift33, k46);
780 
781     HH(d, a, b, c, e[ 2], lShift34, k47);
782 
783     II(a, b, c, d, e[ 0], lShift41, k48);
784 
785     II(d, a, b, c, e[ 7], lShift42, k49);
786 
787     II(c, d, a, b, e[14], lShift43, k50);
788 
789     II(b, c, d, a, e[ 5], lShift44, k51);
790 
791     II(a, b, c, d, e[12], lShift41, k52);
792 
793     II(d, a, b, c, e[ 3], lShift42, k53);
794 
795     II(c, d, a, b, e[10], lShift43, k54);
796 
797     II(b, c, d, a, e[ 1], lShift44, k55);
798 
799     II(a, b, c, d, e[ 8], lShift41, k56);
800 
801     II(d, a, b, c, e[15], lShift42, k57);
802 
803     II(c, d, a, b, e[ 6], lShift43, k58);
804 
805     II(b, c, d, a, e[13], lShift44, k59);
806 
807     II(a, b, c, d, e[ 4], lShift41, k60);
808 
809     II(d, a, b, c, e[11], lShift42, k61);
810 
811     II(c, d, a, b, e[ 2], lShift43, k62);
812 
813     II(b, c, d, a, e[ 9], lShift44, k63);
814 }
815 
816 long long main(long long argc,
817                char *argv[])
818 {
819     if(argc != 2)
820     {
821         Usage();
822     }
823     else
824     {
825         struct stat tStatFileSize;
826 
827         stat(argv[1], &tStatFileSize);
828 
829         long long lFileSize = tStatFileSize.st_size;
830 
831         if(lFileSize == 0)
832         {
833             printf("There is no data in file [%s], 0 byte.\n", argv[1]);
834 
835             return -1;
836         }
837 
838         long long lBytesLeft = lFileSize & 63;
839 
840         if(lBytesLeft == 0)
841         {
842             lBytesLeft = lFileSize + 64;
843         }
844         else if(lBytesLeft < 54)
845         {
846             lBytesLeft = lFileSize - lBytesLeft + 64;
847         }
848         else if(lBytesLeft > 53)
849         {
850             lBytesLeft = lFileSize - lBytesLeft + 128;
851         }
852 
853         unsigned char *ucpData = malloc(lBytesLeft);
854 
855         int fdData = open(argv[1], O_BINARY | O_RDONLY, S_IREAD | S_IWRITE);
856 
857         read(fdData, ucpData, lFileSize);
858 
859         close(fdData);
860 
861         const unsigned char ucaPadding[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
862             53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149,
863             151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251,
864             15, 25, 35, 45, 55, 65, 75, 85, 95, 105};
865 
866         for(long long i = 0; i < 5; ++i)
867         {
868             ucpData[i + lFileSize] = ((unsigned char*)&lFileSize)[i];
869         }
870 
871         for(long long j = lFileSize + 5, k = 0; j < lBytesLeft - 5; ++j, ++k)
872         {
873             ucpData[j] = ucaPadding[k];
874         }
875 
876         for(long long l = 5; l > 0; --l)
877         {
878             ucpData[lBytesLeft - l] = ((unsigned char*)&lFileSize)[l - 1];
879         }
880 
881         unsigned char ucaDigest0[] = {1, 2, 1, 0, 0, 0, 2, 1, 2, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0},
882 
883                       ucaDigest1[] = {2, 2, 1, 2, 1, 0, 0, 2, 2, 0, 2, 0, 1, 1, 0, 2, 2, 0, 2, 0, 1, 0, 0, 1},
884 
885                       ucaDigest2[] = {2, 1, 2, 2, 2, 0, 1, 2, 1, 1, 2, 0, 0, 0, 1, 0, 2, 0, 2, 0, 0, 2, 1, 0},
886 
887                       ucaDigest3[] = {1, 1, 2, 0, 1, 0, 0, 2, 1, 2, 0, 0, 2, 2, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0};
888 
889         for(long long i = 0; i < lBytesLeft; i += 64)
890         {
891             unsigned int *uipData = (unsigned int*)(ucpData + i);
892 
893             unsigned char ucaTrinary[16][24];
894 
895             for(long long j = 0; j < 16; ++j)
896             {
897                 Ternary(uipData[j], ucaTrinary[j]);
898             }
899 
900             MD3Hash(ucaDigest0, ucaDigest1, ucaDigest2, ucaDigest3, ucaTrinary);
901         }
902 
903         for(long long k = 0; k < 24; ++k)
904         {
905             printf("%hhu%hhu%hhu%hhu", ucaDigest0[k], ucaDigest1[k], ucaDigest2[k], ucaDigest3[k]);
906         }
907 
908         putchar(10);
909 
910         free(ucpData);
911     }
912 }

 

发布 Python 源码

  1 #!/usr/bin/env python3
  2 # -*- coding: utf-8 -*-
  3  
  4 #********************************************************
  5 # 作者:伍耀晖              Author: YaoHui.Wu           *
  6 # 开源日期:2022年6月11日   Open Source Date: 2022-6-11 *
  7 # 国家:中国                Country: China              *
  8 #********************************************************
  9 
 10 import os, sys
 11 
 12 def Usage():
 13     print("Usage: python MD3Hash YouWantToHash.File")
 14 
 15 def Ternary(iNumeric):
 16     i, lTrinary = 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]
 17 
 18     if iNumeric:
 19         while iNumeric:
 20             iNumeric, iRemainder = divmod(iNumeric, 3)
 21 
 22             lTrinary[i] = iRemainder
 23 
 24             i += 1
 25 
 26     return lTrinary[::-1]
 27 
 28 # 0 0 0
 29 # 0 1 1
 30 # 0 1 2
 31 
 32 def TrinaryAnd(w, x, y):
 33     for i in range(24):
 34         if x[i] == y[i] == 0: w[i] = 0
 35 
 36         elif x[i] == 0 and y[i] == 1: w[i] = 0
 37 
 38         elif x[i] == 0 and y[i] == 2: w[i] = 0
 39 
 40         elif x[i] == 1 and y[i] == 0: w[i] = 0
 41 
 42         elif x[i] == y[i] == 1: w[i] = 1
 43 
 44         elif x[i] == 1 and y[i] == 2: w[i] = 1
 45 
 46         elif x[i] == 2 and y[i] == 0: w[i] = 0
 47 
 48         elif x[i] == 2 and y[i] == 1: w[i] = 1
 49 
 50         elif x[i] == y[i] == 2: w[i] = 2
 51 
 52 # 0 1 2
 53 # 1 1 2
 54 # 2 2 2
 55 
 56 def TrinaryOr(w, x, y):
 57     for j in range(24):
 58         if x[j] == y[j] == 0: w[j] = 0
 59 
 60         elif x[j] == 0 and y[j] == 1: w[j] = 1
 61 
 62         elif x[j] == 0 and y[j] == 2: w[j] = 2
 63 
 64         elif x[j] == 1 and y[j] == 0: w[j] = 1
 65 
 66         elif x[j] == y[j] == 1: w[j] = 1
 67 
 68         elif x[j] == 1 and y[j] == 2: w[j] = 2
 69 
 70         elif x[j] == 2 and y[j] == 0: w[j] = 2
 71 
 72         elif x[j] == 2 and y[j] == 1: w[j] = 2
 73 
 74         elif x[j] == y[j] == 2: w[j] = 2
 75 
 76 # 0 0 2
 77 # 1 1 1
 78 # 2 2 0
 79 
 80 def TrinaryXor(w, x, y):
 81     for k in range(24):
 82        if x[k] == y[k] == 0: w[k] = 0
 83 
 84        elif x[k] == 0 and y[k] == 1: w[k] = 0
 85 
 86        elif x[k] == 0 and y[k] == 2: w[k] = 2
 87 
 88        elif x[k] == 1 and y[k] == 0: w[k] = 1
 89 
 90        elif x[k] == y[k] == 1: w[k] = 1
 91 
 92        elif x[k] == 1 and y[k] == 2: w[k] = 1
 93 
 94        elif x[k] == 2 and y[k] == 0: w[k] = 2
 95 
 96        elif x[k] == 2 and y[k] == 1: w[k] = 2
 97 
 98        elif x[k] == y[k] == 2: w[k] = 0
 99 
100 # 2 0 0
101 # 1 1 1
102 # 0 2 2
103 
104 def TrinaryXand(w, x, y):
105     for i in range(24):
106        if x[i] == y[i] == 0: w[i] = 2
107 
108        elif x[i] == 0 and y[i] == 1: w[i] = 0
109 
110        elif x[i] == 0 and y[i] == 2: w[i] = 0
111 
112        elif x[i] == 1 and y[i] == 0: w[i] = 1
113 
114        elif x[i] == y[i] == 1: w[i] = 1
115 
116        elif x[i] == 1 and y[i] == 2: w[i] = 1
117 
118        elif x[i] == 2 and y[i] == 0: w[i] = 0
119 
120        elif x[i] == 2 and y[i] == 1: w[i] = 2
121 
122        elif x[i] == y[i] == 2: w[i] = 2
123 
124 # 0 1 2
125 # 1 2 0
126 # 2 0 1
127 
128 def TrinaryAdd(w, x, y):
129     for j in range(24):
130         if x[j] == [j] == 0: w[j] = 0
131 
132         elif x[j] == 0 and y[j] == 1: w[j] = 1
133 
134         elif x[j] == 0 and y[j] == 2: w[j] = 2
135 
136         elif x[j] == 1 and y[j] == 0: w[j] = 1
137 
138         elif x[j] == y[j] == 1: w[j] = 2
139 
140         elif x[j] == 1 and y[j] == 2: w[j] = 0
141 
142         elif x[j] == 2 and y[j] == 0: w[j] = 2
143 
144         elif x[j] == 2 and y[j] == 1: w[j] = 0
145 
146         elif x[j] == y[j] == 2: w[j] = 1
147 
148 # 0 0 0
149 # 0 1 2
150 # 0 2 1
151 
152 def TrinaryMultiplication(w, x, y):
153     for k in range(24):
154         if x[k] == y[k] == 0: w[k] = 0
155 
156         elif x[k] == 0 and y[k] == 1: w[k] = 0
157 
158         elif x[k] == 0 and y[k] == 2: w[k] = 0
159 
160         elif x[k] == 1 and y[k] == 0: w[k] = 0
161 
162         elif x[k] == y[k] == 1: w[k] = 1
163 
164         elif x[k] == 1 and y[k] == 2: w[k] = 2
165 
166         elif x[k] == 2 and y[k] == 0: w[k] = 0
167 
168         elif x[k] == 2 and y[k] == 1: w[k] = 2
169 
170         elif x[k] == y[k] == 2: w[k] = 1
171 
172 def F(w, x, y, z):
173     TrinaryAnd(w, x, y)
174 
175     TrinaryOr(w, w, z)
176 
177 def G(w, x, y, z):
178     TrinaryAdd(w, x, z)
179 
180     TrinaryMultiplication(w, w, y)
181 
182 def H(w, x, y, z):
183     TrinaryXor(w, x, y)
184 
185     TrinaryXand(w, w, z)
186 
187 def I(w, x, y, z):
188     TrinaryMultiplication(w, y, z)
189 
190     TrinaryAdd(w, w, x)
191 
192 def Add(w, z):
193     iCarry = 0
194 
195     for i in range(24):
196         w[i] += z[i] + iCarry
197 
198         if w[i] > 2:
199             w[i] -= 3
200 
201             if iCarry == 0:
202                 iCarry = 1
203         else:
204             iCarry = 0
205 
206 def RotateShiftLeft(w, iBit):
207     lSwap = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
208 
209     for j in range(23, -1, -1):
210         lSwap[j] = w[j - iBit if j >= iBit else 24 - iBit + j]
211 
212     for k in range(24):
213         w[k] = lSwap[k]
214 
215 def FF(a, b, c, d, e, iShift, k):
216     lTemp = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
217 
218     F(lTemp, b, c, d)
219 
220     Add(lTemp, a)
221 
222     Add(lTemp, e)
223 
224     Add(lTemp, k)
225 
226     RotateShiftLeft(lTemp, iShift)
227 
228     Add(lTemp, b)
229 
230     for i in range(24):
231         a[i] = lTemp[i]
232 
233 def GG(a, b, c, d, e, iShift, k):
234     lTemp = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
235 
236     G(lTemp, c, d, a)
237 
238     Add(lTemp, b)
239 
240     Add(lTemp, e)
241 
242     Add(lTemp, k)
243 
244     RotateShiftLeft(lTemp, iShift)
245 
246     Add(lTemp, c)
247 
248     for j in range(24):
249         b[j] = lTemp[j]
250 
251 def HH(a, b, c, d, e, iShift, k):
252     lTemp = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
253 
254     H(lTemp, d, a, b)
255 
256     Add(lTemp, c)
257 
258     Add(lTemp, e)
259 
260     Add(lTemp, k)
261 
262     RotateShiftLeft(lTemp, iShift)
263 
264     Add(lTemp, d)
265 
266     for i in range(24):
267         c[i] = lTemp[i]
268 
269 def II(a, b, c, d, e, iShift, k):
270     lTemp = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
271 
272     I(lTemp, a, b, c)
273 
274     Add(lTemp, d)
275 
276     Add(lTemp, e)
277 
278     Add(lTemp, k)
279 
280     RotateShiftLeft(lTemp, iShift)
281 
282     Add(lTemp, a)
283 
284     for j in range(24):
285         d[j] = lTemp[j]
286 
287 def MD3Hash(a, b, c, d, e):
288     iShift11 = 2
289 
290     iShift12 = 3
291 
292     iShift13 = 4
293 
294     iShift14 = 5
295 
296     iShift21 = 6
297 
298     iShift22 = 7
299 
300     iShift23 = 8
301 
302     iShift24 = 11
303 
304     iShift31 = 10
305 
306     iShift32 = 13
307 
308     iShift33 = 14
309 
310     iShift34 = 17
311 
312     iShift41 = 16
313 
314     iShift42 = 19
315 
316     iShift43 = 22
317 
318     iShift44 = 23
319 
320     k0 = (2, 1, 1, 1, 2, 2, 0, 1, 2, 2, 1, 1, 2, 1, 2, 2, 2, 0, 0, 0, 1, 0, 0, 0)
321 
322     k1 = (1, 1, 2, 0, 0, 1, 0, 0, 0, 1, 0, 2, 1, 1, 0, 2, 0, 0, 1, 0, 1, 0, 0, 0)
323 
324     k2 = (0, 0, 2, 2, 1, 2, 0, 0, 1, 1, 1, 1, 0, 2, 0, 0, 2, 1, 1, 0, 0, 0, 0, 0)
325 
326     k3 = (1, 1, 1, 0, 1, 0, 0, 2, 1, 1, 2, 0, 2, 1, 1, 1, 0, 1, 2, 2, 0, 0, 0, 0)
327 
328     k4 = (1, 2, 1, 1, 2, 0, 1, 2, 2, 2, 0, 2, 0, 0, 0, 2, 2, 1, 1, 0, 1, 0, 0, 0)
329 
330     k5 = (2, 1, 0, 2, 1, 2, 1, 0, 1, 1, 1, 0, 2, 2, 1, 2, 0, 0, 0, 1, 0, 0, 0, 0)
331 
332     k6 = (2, 0, 0, 1, 0, 0, 1, 0, 0, 1, 2, 1, 2, 2, 1, 1, 2, 0, 1, 2, 0, 0, 0, 0)
333 
334     k7 = (2, 2, 1, 0, 0, 2, 1, 1, 2, 1, 0, 2, 0, 1, 0, 2, 2, 2, 1, 0, 1, 0, 0, 0)
335 
336     k8 = (1, 2, 0, 0, 1, 0, 0, 1, 0, 2, 2, 1, 0, 0, 1, 0, 2, 1, 1, 1, 0, 0, 0, 0)
337 
338     k9 = (2, 1, 1, 2, 2, 2, 1, 1, 0, 2, 2, 1, 1, 1, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0)
339 
340     k10 = (1, 2, 2, 0, 1, 2, 0, 1, 2, 2, 2, 1, 2, 2, 0, 2, 0, 0, 2, 0, 1, 0, 0, 0)
341 
342     k11 = (1, 1, 0, 2, 2, 0, 1, 2, 2, 2, 0, 1, 1, 2, 1, 1, 2, 2, 2, 1, 0, 0, 0, 0)
343 
344     k12 = (2, 0, 1, 2, 1, 2, 0, 0, 1, 0, 0, 2, 2, 0, 2, 2, 2, 1, 1, 1, 0, 0, 0, 0)
345 
346     k13 = (2, 2, 2, 0, 2 ,2, 1, 0, 1, 1, 1, 2, 1, 1, 1, 2, 2, 2, 1, 0, 1, 0, 0, 0)
347 
348     k14 = (1, 0, 1, 1, 0, 2, 2, 2, 0, 0, 1, 1, 2, 2, 1, 2, 1, 0, 1, 2, 0, 0, 0, 0)
349 
350     k15 = (0, 1, 2, 0, 2, 1, 1, 1, 1, 2, 0, 2, 1, 1, 0, 2, 1, 0, 0, 1, 0, 0, 0, 0)
351 
352     k16 = (0, 0, 1, 0, 1, 1, 1, 2, 1, 2 ,0, 2, 2, 0, 2, 2, 2, 1, 1, 0, 1, 0, 0, 0)
353 
354     k17 = (1, 2, 0, 2, 0, 0, 2, 2, 1, 1, 2, 0, 0, 1, 2, 2, 2, 0, 2, 2, 0, 0, 0, 0)
355 
356     k18 = (0, 1, 2, 0, 0, 2, 0, 2, 0, 1, 2, 0, 2, 1, 2, 2, 2, 1, 1, 0, 0, 0, 0, 0)
357 
358     k19 = (1, 2, 1, 1, 1, 2, 2, 2, 2, 1, 1, 0, 1, 2, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0)
359 
360     k20 = (1, 2, 1, 0, 2, 2, 1, 0, 0, 2, 2, 1, 2, 0, 1, 1, 2, 0, 0, 0, 1, 0, 0, 0)
361 
362     k21 = (2, 0, 0, 1, 2, 0, 2, 0, 1, 2, 1, 1, 2, 2, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0)
363 
364     k22 = (1, 2, 0, 0, 2, 1, 1, 1, 0, 1, 2, 2, 1, 2, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0)
365 
366     k23 = (2, 1, 1, 1, 1, 0, 1, 1, 1, 2, 2, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0)
367 
368     k24 = (0, 1, 2, 0, 2, 2, 1, 0, 0, 2, 2, 1, 1, 2, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0)
369 
370     k25 = (0, 1, 2, 1, 1, 2, 2, 1, 1, 0, 1, 2, 0, 2, 0, 0, 1, 1, 2, 2, 0, 0, 0, 0)
371 
372     k26 = (2, 1, 2, 0, 1, 1, 2, 1, 2, 1, 1, 0, 1, 2, 0, 1, 2, 1, 1, 0, 1, 0, 0, 0)
373 
374     k27 = (2, 2, 0, 1, 1, 0, 2, 1, 1, 1, 0, 1, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0)
375 
376     k28 = (1, 1, 2, 2, 0, 0, 2, 1, 1, 2, 2, 0, 2, 2, 1, 0, 0, 1, 1, 2, 0, 0, 0, 0)
377 
378     k29 = (2, 2, 2, 2, 0, 2, 0, 0, 1, 0, 0, 0, 2, 0, 2, 1, 2, 2, 1, 0, 1, 0, 0, 0)
379 
380     k30 = (1, 2, 2, 0, 1, 0, 1, 1, 2, 2, 2, 0, 1, 2, 2, 0, 1, 1, 1, 1, 0, 0, 0, 0)
381 
382     k31 = (1, 1, 2, 2, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0)
383 
384     k32 = (1, 1, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 2, 2, 0, 2, 0, 0, 2, 0, 1, 0, 0, 0)
385 
386     k33 = (2, 1, 1, 1, 2, 2, 1, 1, 1, 0, 2, 2, 0, 0, 1, 2, 1, 2, 2, 1, 0, 0, 0, 0)
387 
388     k34 = (1, 0, 0, 0, 0, 2, 2, 0, 1, 0, 1, 1, 1, 1, 0, 2, 0, 2, 1, 1, 0, 0, 0, 0)
389 
390     k35 = (1, 1, 1, 0, 2, 2, 0, 0, 0, 2, 2, 0, 2, 1, 2, 2, 2, 2, 1, 0, 1, 0, 0, 0)
391 
392     k36 = (2, 1, 0, 0, 2, 0, 1, 1, 1, 0, 2, 2, 1, 2, 1, 0, 1, 0, 1, 2, 0, 0, 0, 0)
393 
394     k37 = (1, 2, 0, 2, 0, 1, 0, 0, 2, 1, 1, 0, 1, 0, 2, 1, 2, 0, 0, 1, 0, 0, 0, 0)
395 
396     k38 = (1, 2, 0, 2, 1, 2, 1, 1, 2, 0, 1, 0, 1, 1, 1, 0, 0, 2, 1, 0, 1, 0, 0, 0)
397 
398     k39 = (0, 2, 1, 1, 0, 1, 2, 1, 2, 0, 1, 2, 0, 0, 0, 1, 2, 0, 2, 2, 0, 0, 0, 0)
399 
400     k40 = (0, 0, 0, 0, 0, 1, 0, 2, 1, 1, 2, 2, 0, 1, 1, 2, 0, 2, 1, 0, 0, 0, 0, 0)
401 
402     k41 = (0, 1, 1, 0, 2, 2, 0, 0, 1, 2, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0)
403 
404     k42 = (2, 1, 1, 0, 0, 2, 0, 0, 0, 2, 1, 0, 2, 2, 2, 2, 1, 0, 0, 0, 1, 0, 0, 0)
405 
406     k43 = (0, 2, 2, 2, 0, 1, 0, 0, 2, 1, 0, 0, 2, 2, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0)
407 
408     k44 = (1, 2, 1, 0, 2, 1, 1, 0, 0, 0, 1, 2, 0, 0, 2, 2, 0, 1, 0, 0, 1, 0, 0, 0)
409 
410     k45 = (0, 1, 0, 2, 2, 2, 0, 1, 1, 0, 0, 0, 1, 2, 2, 2, 2, 2, 0, 0, 1, 0, 0, 0)
411 
412     k46 = (1, 0, 1, 2, 1, 2, 1, 1, 1, 0, 0, 2, 2, 2, 2, 0, 0, 1, 1, 0, 0, 0, 0, 0)
413 
414     k47 = (0, 0, 1, 0, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, 2, 1, 1, 1, 2, 2, 0, 0, 0, 0)
415 
416     k48 = (0, 2, 2, 2, 1, 0, 0, 1, 1, 2, 2, 2, 0, 1, 1, 0, 2, 1, 1, 0, 1, 0, 0, 0)
417 
418     k49 = (2, 0, 0, 2, 0, 1, 0, 0, 0, 0, 1, 1, 2, 1, 1, 0, 2, 2, 2, 0, 0, 0, 0, 0)
419 
420     k50 = (2, 0, 2, 1, 1, 2, 2, 2, 1, 1, 2, 1, 1, 2, 1, 2, 0, 1, 1, 2, 0, 0, 0, 0)
421 
422     k51 = (1, 2, 1, 0, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 0, 1, 2, 2, 1, 0, 1, 0, 0, 0)
423 
424     k52 = (2, 0, 0, 0, 0, 2, 1, 2, 1, 2, 0, 2, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0)
425 
426     k53 = (1, 0, 2, 0, 2, 1, 2, 2, 1, 2, 2, 2, 0, 2, 0, 2, 1, 0, 0, 2, 0, 0, 0, 0)
427 
428     k54 = (2, 1, 1, 2, 0, 0, 2, 1, 1, 2, 0, 2, 0, 2, 0, 2, 0, 0, 2, 0, 1, 0, 0, 0)
429 
430     k55 = (0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 2, 2, 1, 0, 0, 0, 0)
431 
432     k56 = (1, 2, 0, 1, 1, 2, 1, 1, 0, 2, 2, 2, 2, 1, 1, 1, 1, 2, 1, 1, 0, 0, 0, 0)
433 
434     k57 = (2, 1, 1, 2, 0, 0, 1, 0, 2, 0, 1, 0, 2, 1, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0)
435 
436     k58 = (2, 2, 1, 0, 0, 2, 2, 2, 1, 1, 2, 2, 0, 2, 1, 1, 0, 0, 1, 2, 0, 0, 0, 0)
437 
438     k59 = (0, 1, 0, 0, 1, 1, 0, 1, 2, 1, 0, 1, 0, 2, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0)
439 
440     k60 = (1, 1, 1, 1, 0, 1, 1, 2, 1, 0, 2, 2, 1, 1, 0, 1, 0, 2, 1, 0, 1, 0, 0, 0)
441 
442     k61 = (2, 1, 1, 2, 0, 2, 0, 0, 1, 2, 1, 2, 0, 2, 0, 2, 1, 0, 2, 2, 0, 0, 0, 0)
443 
444     k62 = (0, 0, 1, 2, 0, 2, 1, 1, 0, 2, 1, 1, 2, 0, 0, 2, 1, 2, 1, 0, 0, 0, 0, 0)
445 
446     k63 = (2, 0, 2, 0, 2, 2, 1, 0, 0, 2, 0, 1, 1, 0, 1, 2, 1, 0, 1, 0, 1, 0, 0, 0)
447 
448     FF(a, b, c, d, e[ 0], iShift11, k0)
449 
450     FF(b, c, d, a, e[ 1], iShift12, k1)
451 
452     FF(c, d, a, b, e[ 2], iShift13, k2)
453 
454     FF(d, a, b, c, e[ 3], iShift14, k3)
455 
456     FF(a, b, c, d, e[ 4], iShift11, k4)
457 
458     FF(b, c, d, a, e[ 5], iShift12, k5)
459 
460     FF(c, d, a, b, e[ 6], iShift13, k6)
461 
462     FF(d, a, b, c, e[ 7], iShift14, k7)
463 
464     FF(a, b, c, d, e[ 8], iShift11, k8)
465 
466     FF(b, c, d, a, e[ 9], iShift12, k9)
467 
468     FF(c, d, a, b, e[10], iShift13, k10)
469 
470     FF(d, a, b, c, e[11], iShift14, k11)
471 
472     FF(a, b, c, d, e[12], iShift11, k12)
473 
474     FF(b, c, d, a, e[13], iShift12, k13)
475 
476     FF(c, d, a, b, e[14], iShift13, k14)
477 
478     FF(d, a, b, c, e[15], iShift14, k15)
479 
480     GG(a, b, c, d, e[ 1], iShift21, k16)
481 
482     GG(d, a, b, c, e[ 6], iShift22, k17)
483 
484     GG(c, d, a, b, e[11], iShift23, k18)
485 
486     GG(b, c, d, a, e[ 0], iShift24, k19)
487 
488     GG(a, b, c, d, e[ 5], iShift21, k20)
489 
490     GG(d, a, b, c, e[10], iShift22, k21)
491 
492     GG(c, d, a, b, e[15], iShift23, k22)
493 
494     GG(b, c, d, a, e[ 4], iShift24, k23)
495 
496     GG(a, b, c, d, e[ 9], iShift21, k24)
497 
498     GG(d, a, b, c, e[14], iShift22, k25)
499 
500     GG(c, d, a, b, e[ 3], iShift23, k26)
501 
502     GG(b, c, d, a, e[ 8], iShift24, k27)
503 
504     GG(a, b, c, d, e[13], iShift21, k28)
505 
506     GG(d, a, b, c, e[ 2], iShift22, k29)
507 
508     GG(c, d, a, b, e[ 7], iShift23, k30)
509 
510     GG(b, c, d, a, e[12], iShift24, k31)
511 
512     HH(a, b, c, d, e[ 5], iShift31, k32)
513 
514     HH(b, c, d, a, e[ 8], iShift32, k33)
515 
516     HH(c, d, a, b, e[11], iShift33, k34)
517 
518     HH(d, a, b, c, e[14], iShift34, k35)
519 
520     HH(a, b, c, d, e[ 1], iShift31, k36)
521 
522     HH(b, c, d, a, e[ 4], iShift32, k37)
523 
524     HH(c, d, a, b, e[ 7], iShift33, k38)
525 
526     HH(d, a, b, c, e[10], iShift34, k39)
527 
528     HH(a, b, c, d, e[13], iShift31, k40)
529 
530     HH(b, c, d, a, e[ 0], iShift32, k41)
531 
532     HH(c, d, a, b, e[ 3], iShift33, k42)
533 
534     HH(d, a, b, c, e[ 6], iShift34, k43)
535 
536     HH(a, b, c, d, e[ 9], iShift31, k44)
537 
538     HH(b, c, d, a, e[12], iShift32, k45)
539 
540     HH(c, d, a, b, e[15], iShift33, k46)
541 
542     HH(d, a, b, c, e[ 2], iShift34, k47)
543 
544     II(a, b, c, d, e[ 0], iShift41, k48)
545 
546     II(d, a, b, c, e[ 7], iShift42, k49)
547 
548     II(c, d, a, b, e[14], iShift43, k50)
549 
550     II(b, c, d, a, e[ 5], iShift44, k51)
551 
552     II(a, b, c, d, e[12], iShift41, k52)
553 
554     II(d, a, b, c, e[ 3], iShift42, k53)
555 
556     II(c, d, a, b, e[10], iShift43, k54)
557 
558     II(b, c, d, a, e[ 1], iShift44, k55)
559 
560     II(a, b, c, d, e[ 8], iShift41, k56)
561 
562     II(d, a, b, c, e[15], iShift42, k57)
563 
564     II(c, d, a, b, e[ 6], iShift43, k58)
565 
566     II(b, c, d, a, e[13], iShift44, k59)
567 
568     II(a, b, c, d, e[ 4], iShift41, k60)
569 
570     II(d, a, b, c, e[11], iShift42, k61)
571 
572     II(c, d, a, b, e[ 2], iShift43, k62)
573 
574     II(b, c, d, a, e[ 9], iShift44, k63)
575 
576 if __name__ == "__main__":
577     if len(sys.argv) != 2: Usage()
578 
579     else:
580         statFileSize = os.stat(sys.argv[1])
581 
582         iFileSize = statFileSize.st_size
583 
584         if iFileSize == 0:
585             print("There is no data in file [{}], 0 byte.".format(sys.argv[1]))
586 
587             exit(-1)
588 
589         iBytesLeft = iFileSize & 63
590 
591         if iBytesLeft == 0: iBytesLeft = iFileSize + 64
592 
593         elif iBytesLeft < 54: iBytesLeft = iFileSize - iBytesLeft + 64
594 
595         elif iBytesLeft > 53: iBytesLeft = iFileSize - iBytesLeft + 128
596 
597         with open(sys.argv[1], "br") as fdData:
598             baData = bytearray(fdData.read())
599 
600         tPadding = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
601             53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149,
602             151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251,
603             15, 25, 35, 45, 55, 65, 75, 85, 95, 105)
604 
605         while iFileSize < iBytesLeft:
606             for i in range(5):
607                 baData.append(statFileSize.st_size >> 8 * i & 255)
608 
609                 iFileSize += 1
610 
611             j = 0
612 
613             while iFileSize < iBytesLeft - 5:
614                 baData.append(tPadding[j])
615 
616                 j += 1
617 
618                 iFileSize += 1
619 
620             for k in range(4, -1, -1):
621                 baData.append(statFileSize.st_size >> 8 * k & 255)
622 
623                 iFileSize += 1
624 
625         lDigest0 = [1, 2, 1, 0, 0, 0, 2, 1, 2, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0]
626 
627         lDigest1 = [2, 2, 1, 2, 1, 0, 0, 2, 2, 0, 2, 0, 1, 1, 0, 2, 2, 0, 2, 0, 1, 0, 0, 1]
628 
629         lDigest2 = [2, 1, 2, 2, 2, 0, 1, 2, 1, 1, 2, 0, 0, 0, 1, 0, 2, 0, 2, 0, 0, 2, 1, 0]
630 
631         lDigest3 = [1, 1, 2, 0, 1, 0, 0, 2, 1, 2, 0, 0, 2, 2, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0]
632 
633         for i in range(0, iBytesLeft, 64):
634             lTrinary = [];
635 
636             for j in range(16):
637                 lTrinary.append(Ternary( baData[i + 4 * j] + (baData[i + 4 * j + 1] << 8) + (baData[i + 4 * j + 2] << 16) + (baData[i + 4 * j + 3] << 24)))
638 
639             MD3Hash(lDigest0, lDigest1, lDigest2, lDigest3, lTrinary)
640 
641         for k in range(24):
642             print("{:d}{:d}{:d}{:d}".format(lDigest0[k], lDigest1[k], lDigest2[k], lDigest3[k]), sep = "", end = "")
643 
644         print()

 

发布 Java 源码

  1 /*********************************************************
  2 * 作者:伍耀晖               Author: YaoHui.Wu           *
  3 * 开源日期:2022年6月11日    Open Source Date: 2022-6-11 *
  4 * 国家:中国                 Country: China              *
  5 *********************************************************/
  6 
  7 import java.io.*;
  8 
  9 public class MD3Hash
 10 {
 11     final static int iShift11 = 2, iShift12 = 3, iShift13 = 4, iShift14 = 5, iShift21 = 6, iShift22 = 7, iShift23 = 8, iShift24 = 11, iShift31 = 10, iShift32 = 13, iShift33  = 14, iShift34 = 17, iShift41 = 16, iShift42 = 19, iShift43 = 22, iShift44 = 23;
 12 
 13     private static void Usage()
 14     {
 15         System.out.printf("Usage: java MD3Hash YouWantToHash.File\n");
 16     }
 17 
 18     private static void Ternary(long lNumeric,
 19                                 byte[] baTrinary)
 20     {
 21         baTrinary[0] = baTrinary[1] = baTrinary[2] = baTrinary[3] = baTrinary[4] = baTrinary[5] = baTrinary[6] = baTrinary[7] = baTrinary[8] = baTrinary[9] = baTrinary[10] = baTrinary[11] = baTrinary[12] = baTrinary[13] = baTrinary[14] = baTrinary[15] = baTrinary[16] = baTrinary[17] = baTrinary[18] = baTrinary[19] = baTrinary[20] = baTrinary[21] = baTrinary[22] = baTrinary[23] = 0;
 22 
 23         if(lNumeric != 0)
 24         {
 25             for(int i = 23; i > -1; --i)
 26             {
 27                 baTrinary[i] = (byte)(lNumeric % 3 & 255) ;
 28 
 29                 lNumeric /= 3;
 30             }
 31         }
 32     }
 33 
 34 // 0 0 0
 35 // 0 1 1
 36 // 0 1 2
 37 
 38     private static void TrinaryAnd(byte[] w,
 39                                    byte[] x,
 40                                    byte[] y)
 41     {
 42         for(int i = 0; i < 24; ++i)
 43         {
 44             if(x[i] == 0 && y[i] == 0)
 45             {
 46                 w[i] = 0;
 47             }
 48             else if(x[i] == 0 && y[i] == 1)
 49             {
 50                 w[i] = 0;
 51             }
 52             else if(x[i] == 0 && y[i] == 2)
 53             {
 54                 w[i] = 0;
 55             }
 56             else if(x[i] == 1 && y[i] == 0)
 57             {
 58                 w[i] = 0;
 59             }
 60             else if(x[i] == 1 && y[i] == 1)
 61             {
 62                 w[i] = 1;
 63             }
 64             else if(x[i] == 1 && y[i] == 2)
 65             {
 66                 w[i] = 1;
 67             }
 68             else if(x[i] == 2 && y[i] == 0)
 69             {
 70                 w[i] = 0;
 71             }
 72             else if(x[i] == 2 && y[i] == 1)
 73             {
 74                 w[i] = 1;
 75             }
 76             else if(x[i] == 2 && y[i] == 2)
 77             {
 78                 w[i] = 2;
 79             }
 80         }
 81     }
 82 
 83 // 0 1 2
 84 // 1 1 2
 85 // 2 2 2
 86 
 87     private static void TrinaryOr(byte[] w,
 88                                   byte[] x,
 89                                   byte[] y)
 90     {
 91         for(int j = 0; j < 24; ++j)
 92         {
 93             if(x[j] == 0 && y[j] == 0)
 94             {
 95                 w[j] = 0;
 96             }
 97             else if(x[j] == 0 && y[j] == 1)
 98             {
 99                 w[j] = 1;
100             }
101             else if(x[j] == 0 && y[j] == 2)
102             {
103                 w[j] = 2;
104             }
105             else if(x[j] == 1 && y[j] == 0)
106             {
107                 w[j] = 1;
108             }
109             else if(x[j] == 1 && y[j] == 1)
110             {
111                 w[j] = 1;
112             }
113             else if(x[j] == 1 && y[j] == 2)
114             {
115                 w[j] = 2;
116             }
117             else if(x[j] == 2 && y[j] == 0)
118             {
119                 w[j] = 2;
120             }
121             else if(x[j] == 2 && y[j] == 1)
122             {
123                 w[j] = 2;
124             }
125             else if(x[j] == 2 && y[j] == 2)
126             {
127                 w[j] = 2;
128             }
129         }
130     }
131 
132 // 0 0 2
133 // 1 1 1
134 // 2 2 0
135 
136     private static void TrinaryXor(byte[] w,
137                                    byte[] x,
138                                    byte[] y)
139     {
140         for(int k = 0; k < 24; ++k)
141         {
142             if(x[k] == 0 && y[k] == 0)
143             {
144                 w[k] = 0;
145             }
146             else if(x[k] == 0 && y[k] == 1)
147             {
148                 w[k] = 0;
149             }
150             else if(x[k] == 0 && y[k] == 2)
151             {
152                 w[k] = 2;
153             }
154             else if(x[k] == 1 && y[k] == 0)
155             {
156                 w[k] = 1;
157             }
158             else if(x[k] == 1 && y[k] == 1)
159             {
160                 w[k] = 1;
161             }
162             else if(x[k] == 1 && y[k] == 2)
163             {
164                 w[k] = 1;
165             }
166             else if(x[k] == 2 && y[k] == 0)
167             {
168                 w[k] = 2;
169             }
170             else if(x[k] == 2 && y[k] == 1)
171             {
172                 w[k] = 2;
173             }
174             else if(x[k] == 2 && y[k] == 2)
175             {
176                 w[k] = 0;
177             }
178         }
179     }
180 
181 // 2 0 0
182 // 1 1 1
183 // 0 2 2
184 
185     private static void TrinaryXand(byte[] w,
186                                     byte[] x,
187                                     byte[] y)
188     {
189         for(int i = 0; i < 24; ++i)
190         {
191             if(x[i] == 0 && y[i] == 0)
192             {
193                 w[i] = 2;
194             }
195             else if(x[i] == 0 && y[i] == 1)
196             {
197                 w[i] = 0;
198             }
199             else if(x[i] == 0 && y[i] == 2)
200             {
201                 w[i] = 0;
202             }
203             else if(x[i] == 1 && y[i] == 0)
204             {
205                 w[i] = 1;
206             }
207             else if(x[i] == 1 && y[i] == 1)
208             {
209                 w[i] = 1;
210             }
211             else if(x[i] == 1 && y[i] == 2)
212             {
213                 w[i] = 1;
214             }
215             else if(x[i] == 2 && y[i] == 0)
216             {
217                 w[i] = 0;
218             }
219             else if(x[i] == 2 && y[i] == 1)
220             {
221                 w[i] = 2;
222             }
223             else if(x[i] == 2 && y[i] == 2)
224             {
225                 w[i] = 2;
226             }
227         }
228     }
229 
230 // 0 1 2
231 // 1 2 0
232 // 2 0 1
233 
234     private static void TrinaryAdd(byte[] w,
235                                    byte[] x,
236                                    byte[] y)
237     {
238         for(int j = 0; j < 24; ++j)
239         {
240             if(x[j] == 0 && y[j] == 0)
241             {
242                 w[j] = 0;
243             }
244             else if(x[j] == 0 && y[j] == 1)
245             {
246                 w[j] = 1;
247             }
248             else if(x[j] == 0 && y[j] == 2)
249             {
250                 w[j] = 2;
251             }
252             else if(x[j] == 1 && y[j] == 0)
253             {
254                 w[j] = 1;
255             }
256             else if(x[j] == 1 && y[j] == 1)
257             {
258                 w[j] = 2;
259             }
260             else if(x[j] == 1 && y[j] == 2)
261             {
262                 w[j] = 0;
263             }
264             else if(x[j] == 2 && y[j] == 0)
265             {
266                 w[j] = 2;
267             }
268             else if(x[j] == 2 && y[j] == 1)
269             {
270                 w[j] = 0;
271             }
272             else if(x[j] == 2 && y[j] == 2)
273             {
274                 w[j] = 1;
275             }
276         }
277     }
278 
279 // 0 0 0
280 // 0 1 2
281 // 0 2 1
282 
283     private static void TrinaryMultiplication(byte[] w,
284                                               byte[] x,
285                                               byte[] y)
286     {
287         for(int k = 0; k < 24; ++k)
288         {
289             if(x[k] == 0 && y[k] == 0)
290             {
291                 w[k] = 0;
292             }
293             else if(x[k] == 0 && y[k] == 1)
294             {
295                 w[k] = 0;
296             }
297             else if(x[k] == 0 && y[k] == 2)
298             {
299                 w[k] = 0;
300             }
301             else if(x[k] == 1 && y[k] == 0)
302             {
303                 w[k] = 0;
304             }
305             else if(x[k] == 1 && y[k] == 1)
306             {
307                 w[k] = 1;
308             }
309             else if(x[k] == 1 && y[k] == 2)
310             {
311                 w[k] = 2;
312             }
313             else if(x[k] == 2 && y[k] == 0)
314             {
315                 w[k] = 0;
316             }
317             else if(x[k] == 2 && y[k] == 1)
318             {
319                 w[k] = 2;
320             }
321             else if(x[k] == 2 && y[k] == 2)
322             {
323                 w[k] = 1;
324             }
325         }
326     }
327 
328     private static void F(byte[] w,
329                           byte[] x,
330                           byte[] y,
331                           byte[] z)
332     {
333         TrinaryAnd(w, x, y);
334 
335         TrinaryOr(w, w, z);
336     }
337 
338     private static void G(byte[] w,
339                           byte[] x,
340                           byte[] y,
341                           byte[] z)
342     {
343         TrinaryAdd(w, x, z);
344 
345         TrinaryMultiplication(w, w, y);
346     }
347 
348     private static void H(byte[] w,
349                           byte[] x,
350                           byte[] y,
351                           byte[] z)
352     {
353         TrinaryXor(w, x, y);
354 
355         TrinaryXand(w, w, z);
356     }
357 
358     private static void I(byte[] w,
359                           byte[] x,
360                           byte[] y,
361                           byte[] z)
362     {
363         TrinaryMultiplication(w, y, z);
364 
365         TrinaryAdd(w, w, x);
366     }
367 
368     private static void Add(byte[] w,
369                             byte[] z)
370     {
371         int iCarry = 0;
372 
373         for(int i = 0; i < 24; ++i)
374         {
375             w[i] += z[i] + iCarry;
376 
377             if(w[i] > 2)
378             {
379                 w[i] -= 3;
380 
381                 if(iCarry == 0)
382                 {
383                     iCarry = 1;
384                 }
385             }
386             else
387             {
388                 iCarry = 0;
389             }
390         }
391     }
392 
393     private static void RotateShiftLeft(byte[] w,
394                                         int iBit)
395     {
396         byte[] baSwap = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
397 
398         for(int j = 23; j > -1; --j)
399         {
400             baSwap[j] = w[j >= iBit ? j - iBit : 24 - iBit + j];
401         }
402 
403         for(int k = 0; k < 24; ++k)
404         {
405             w[k] = baSwap[k];
406         }
407     }
408 
409     private static void FF(byte[] a,
410                            byte[] b,
411                            byte[] c,
412                            byte[] d,
413                            byte[] e,
414                            int iShift,
415                            byte[] k)
416     {
417         byte[] baTemp = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
418 
419         F(baTemp, b, c, d);
420 
421         Add(baTemp, a);
422 
423         Add(baTemp, e);
424 
425         Add(baTemp, k);
426 
427         RotateShiftLeft(baTemp, iShift);
428 
429         Add(baTemp, b);
430 
431         for(int i = 0; i < 24; ++i)
432         {
433             a[i] = baTemp[i];
434         }
435     }
436 
437     private static void GG(byte[] a,
438                            byte[] b,
439                            byte[] c,
440                            byte[] d,
441                            byte[] e,
442                            int iShift,
443                            byte[] k)
444     {
445         byte[] baTemp = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
446 
447         G(baTemp, c, d, a);
448 
449         Add(baTemp, b);
450 
451         Add(baTemp, e);
452 
453         Add(baTemp, k);
454 
455         RotateShiftLeft(baTemp, iShift);
456 
457         Add(baTemp, c);
458 
459         for(int j = 0; j < 24; ++j)
460         {
461             b[j] = baTemp[j];
462         }
463     }
464 
465     private static void HH(byte[] a,
466                            byte[] b,
467                            byte[] c,
468                            byte[] d,
469                            byte[] e,
470                            int iShift,
471                            byte[] k)
472     {
473         byte[] baTemp = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
474 
475         H(baTemp, d, a, b);
476 
477         Add(baTemp, c);
478 
479         Add(baTemp, e);
480 
481         Add(baTemp, k);
482 
483         RotateShiftLeft(baTemp, iShift);
484 
485         Add(baTemp, d);
486 
487         for(int i = 0; i < 24; ++i)
488         {
489             c[i] = baTemp[i];
490         }
491     }
492 
493     private static void II(byte[] a,
494                            byte[] b,
495                            byte[] c,
496                            byte[] d,
497                            byte[] e,
498                            int iShift,
499                            byte[] k)
500     {
501         byte[] baTemp = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
502 
503         I(baTemp, a, b, c);
504 
505         Add(baTemp, d);
506 
507         Add(baTemp, e);
508 
509         Add(baTemp, k);
510 
511         RotateShiftLeft(baTemp, iShift);
512 
513         Add(baTemp, a);
514 
515         for(int j = 0; j < 24; ++j)
516         {
517             d[j] = baTemp[j];
518         }
519     }
520 
521     private static void MD3HashCode(byte[] a,
522                                     byte[] b,
523                                     byte[] c,
524                                     byte[] d,
525                                     byte[][] e)
526     {
527         final byte[] k0 = {2, 1, 1, 1, 2, 2, 0, 1, 2, 2, 1, 1, 2, 1, 2, 2, 2, 0, 0, 0, 1, 0, 0, 0},
528 
529                      k1 = {1, 1, 2, 0, 0, 1, 0, 0, 0, 1, 0, 2, 1, 1, 0, 2, 0, 0, 1, 0, 1, 0, 0, 0},
530 
531                      k2 = {0, 0, 2, 2, 1, 2, 0, 0, 1, 1, 1, 1, 0, 2, 0, 0, 2, 1, 1, 0, 0, 0, 0, 0},
532 
533                      k3 = {1, 1, 1, 0, 1, 0, 0, 2, 1, 1, 2, 0, 2, 1, 1, 1, 0, 1, 2, 2, 0, 0, 0, 0},
534 
535                      k4 = {1, 2, 1, 1, 2, 0, 1, 2, 2, 2, 0, 2, 0, 0, 0, 2, 2, 1, 1, 0, 1, 0, 0, 0},
536 
537                      k5 = {2, 1, 0, 2, 1, 2, 1, 0, 1, 1, 1, 0, 2, 2, 1, 2, 0, 0, 0, 1, 0, 0, 0, 0},
538 
539                      k6 = {2, 0, 0, 1, 0, 0, 1, 0, 0, 1, 2, 1, 2, 2, 1, 1, 2, 0, 1, 2, 0, 0, 0, 0},
540 
541                      k7 = {2, 2, 1, 0, 0, 2, 1, 1, 2, 1, 0, 2, 0, 1, 0, 2, 2, 2, 1, 0, 1, 0, 0, 0},
542 
543                      k8 = {1, 2, 0, 0, 1, 0, 0, 1, 0, 2, 2, 1, 0, 0, 1, 0, 2, 1, 1, 1, 0, 0, 0, 0},
544 
545                      k9 = {2, 1, 1, 2, 2, 2, 1, 1, 0, 2, 2, 1, 1, 1, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0},
546 
547                     k10 = {1, 2, 2, 0, 1, 2, 0, 1, 2, 2, 2, 1, 2, 2, 0, 2, 0, 0, 2, 0, 1, 0, 0, 0},
548 
549                     k11 = {1, 1, 0, 2, 2, 0, 1, 2, 2, 2, 0, 1, 1, 2, 1, 1, 2, 2, 2, 1, 0, 0, 0, 0},
550 
551                     k12 = {2, 0, 1, 2, 1, 2, 0, 0, 1, 0, 0, 2, 2, 0, 2, 2, 2, 1, 1, 1, 0, 0, 0, 0},
552 
553                     k13 = {2, 2, 2, 0, 2 ,2, 1, 0, 1, 1, 1, 2, 1, 1, 1, 2, 2, 2, 1, 0, 1, 0, 0, 0},
554 
555                     k14 = {1, 0, 1, 1, 0, 2, 2, 2, 0, 0, 1, 1, 2, 2, 1, 2, 1, 0, 1, 2, 0, 0, 0, 0},
556 
557                     k15 = {0, 1, 2, 0, 2, 1, 1, 1, 1, 2, 0, 2, 1, 1, 0, 2, 1, 0, 0, 1, 0, 0, 0, 0},
558 
559                     k16 = {0, 0, 1, 0, 1, 1, 1, 2, 1, 2 ,0, 2, 2, 0, 2, 2, 2, 1, 1, 0, 1, 0, 0, 0},
560 
561                     k17 = {1, 2, 0, 2, 0, 0, 2, 2, 1, 1, 2, 0, 0, 1, 2, 2, 2, 0, 2, 2, 0, 0, 0, 0},
562 
563                     k18 = {0, 1, 2, 0, 0, 2, 0, 2, 0, 1, 2, 0, 2, 1, 2, 2, 2, 1, 1, 0, 0, 0, 0, 0},
564 
565                     k19 = {1, 2, 1, 1, 1, 2, 2, 2, 2, 1, 1, 0, 1, 2, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0},
566 
567                     k20 = {1, 2, 1, 0, 2, 2, 1, 0, 0, 2, 2, 1, 2, 0, 1, 1, 2, 0, 0, 0, 1, 0, 0, 0},
568 
569                     k21 = {2, 0, 0, 1, 2, 0, 2, 0, 1, 2, 1, 1, 2, 2, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0},
570 
571                     k22 = {1, 2, 0, 0, 2, 1, 1, 1, 0, 1, 2, 2, 1, 2, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0},
572 
573                     k23 = {2, 1, 1, 1, 1, 0, 1, 1, 1, 2, 2, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0},
574 
575                     k24 = {0, 1, 2, 0, 2, 2, 1, 0, 0, 2, 2, 1, 1, 2, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0},
576 
577                     k25 = {0, 1, 2, 1, 1, 2, 2, 1, 1, 0, 1, 2, 0, 2, 0, 0, 1, 1, 2, 2, 0, 0, 0, 0},
578 
579                     k26 = {2, 1, 2, 0, 1, 1, 2, 1, 2, 1, 1, 0, 1, 2, 0, 1, 2, 1, 1, 0, 1, 0, 0, 0},
580 
581                     k27 = {2, 2, 0, 1, 1, 0, 2, 1, 1, 1, 0, 1, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
582 
583                     k28 = {1, 1, 2, 2, 0, 0, 2, 1, 1, 2, 2, 0, 2, 2, 1, 0, 0, 1, 1, 2, 0, 0, 0, 0},
584 
585                     k29 = {2, 2, 2, 2, 0, 2, 0, 0, 1, 0, 0, 0, 2, 0, 2, 1, 2, 2, 1, 0, 1, 0, 0, 0},
586 
587                     k30 = {1, 2, 2, 0, 1, 0, 1, 1, 2, 2, 2, 0, 1, 2, 2, 0, 1, 1, 1, 1, 0, 0, 0, 0},
588 
589                     k31 = {1, 1, 2, 2, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0},
590 
591                     k32 = {1, 1, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 2, 2, 0, 2, 0, 0, 2, 0, 1, 0, 0, 0},
592 
593                     k33 = {2, 1, 1, 1, 2, 2, 1, 1, 1, 0, 2, 2, 0, 0, 1, 2, 1, 2, 2, 1, 0, 0, 0, 0},
594 
595                     k34 = {1, 0, 0, 0, 0, 2, 2, 0, 1, 0, 1, 1, 1, 1, 0, 2, 0, 2, 1, 1, 0, 0, 0, 0},
596 
597                     k35 = {1, 1, 1, 0, 2, 2, 0, 0, 0, 2, 2, 0, 2, 1, 2, 2, 2, 2, 1, 0, 1, 0, 0, 0},
598 
599                     k36 = {2, 1, 0, 0, 2, 0, 1, 1, 1, 0, 2, 2, 1, 2, 1, 0, 1, 0, 1, 2, 0, 0, 0, 0},
600 
601                     k37 = {1, 2, 0, 2, 0, 1, 0, 0, 2, 1, 1, 0, 1, 0, 2, 1, 2, 0, 0, 1, 0, 0, 0, 0},
602 
603                     k38 = {1, 2, 0, 2, 1, 2, 1, 1, 2, 0, 1, 0, 1, 1, 1, 0, 0, 2, 1, 0, 1, 0, 0, 0},
604 
605                     k39 = {0, 2, 1, 1, 0, 1, 2, 1, 2, 0, 1, 2, 0, 0, 0, 1, 2, 0, 2, 2, 0, 0, 0, 0},
606 
607                     k40 = {0, 0, 0, 0, 0, 1, 0, 2, 1, 1, 2, 2, 0, 1, 1, 2, 0, 2, 1, 0, 0, 0, 0, 0},
608 
609                     k41 = {0, 1, 1, 0, 2, 2, 0, 0, 1, 2, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0},
610 
611                     k42 = {2, 1, 1, 0, 0, 2, 0, 0, 0, 2, 1, 0, 2, 2, 2, 2, 1, 0, 0, 0, 1, 0, 0, 0},
612 
613                     k43 = {0, 2, 2, 2, 0, 1, 0, 0, 2, 1, 0, 0, 2, 2, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0},
614 
615                     k44 = {1, 2, 1, 0, 2, 1, 1, 0, 0, 0, 1, 2, 0, 0, 2, 2, 0, 1, 0, 0, 1, 0, 0, 0},
616 
617                     k45 = {0, 1, 0, 2, 2, 2, 0, 1, 1, 0, 0, 0, 1, 2, 2, 2, 2, 2, 0, 0, 1, 0, 0, 0},
618 
619                     k46 = {1, 0, 1, 2, 1, 2, 1, 1, 1, 0, 0, 2, 2, 2, 2, 0, 0, 1, 1, 0, 0, 0, 0, 0},
620 
621                     k47 = {0, 0, 1, 0, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, 2, 1, 1, 1, 2, 2, 0, 0, 0, 0},
622 
623                     k48 = {0, 2, 2, 2, 1, 0, 0, 1, 1, 2, 2, 2, 0, 1, 1, 0, 2, 1, 1, 0, 1, 0, 0, 0},
624 
625                     k49 = {2, 0, 0, 2, 0, 1, 0, 0, 0, 0, 1, 1, 2, 1, 1, 0, 2, 2, 2, 0, 0, 0, 0, 0},
626 
627                     k50 = {2, 0, 2, 1, 1, 2, 2, 2, 1, 1, 2, 1, 1, 2, 1, 2, 0, 1, 1, 2, 0, 0, 0, 0},
628 
629                     k51 = {1, 2, 1, 0, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 0, 1, 2, 2, 1, 0, 1, 0, 0, 0},
630 
631                     k52 = {2, 0, 0, 0, 0, 2, 1, 2, 1, 2, 0, 2, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0},
632 
633                     k53 = {1, 0, 2, 0, 2, 1, 2, 2, 1, 2, 2, 2, 0, 2, 0, 2, 1, 0, 0, 2, 0, 0, 0, 0},
634 
635                     k54 = {2, 1, 1, 2, 0, 0, 2, 1, 1, 2, 0, 2, 0, 2, 0, 2, 0, 0, 2, 0, 1, 0, 0, 0},
636 
637                     k55 = {0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 2, 2, 1, 0, 0, 0, 0},
638 
639                     k56 = {1, 2, 0, 1, 1, 2, 1, 1, 0, 2, 2, 2, 2, 1, 1, 1, 1, 2, 1, 1, 0, 0, 0, 0},
640 
641                     k57 = {2, 1, 1, 2, 0, 0, 1, 0, 2, 0, 1, 0, 2, 1, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0},
642 
643                     k58 = {2, 2, 1, 0, 0, 2, 2, 2, 1, 1, 2, 2, 0, 2, 1, 1, 0, 0, 1, 2, 0, 0, 0, 0},
644 
645                     k59 = {0, 1, 0, 0, 1, 1, 0, 1, 2, 1, 0, 1, 0, 2, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0},
646 
647                     k60 = {1, 1, 1, 1, 0, 1, 1, 2, 1, 0, 2, 2, 1, 1, 0, 1, 0, 2, 1, 0, 1, 0, 0, 0},
648 
649                     k61 = {2, 1, 1, 2, 0, 2, 0, 0, 1, 2, 1, 2, 0, 2, 0, 2, 1, 0, 2, 2, 0, 0, 0, 0},
650 
651                     k62 = {0, 0, 1, 2, 0, 2, 1, 1, 0, 2, 1, 1, 2, 0, 0, 2, 1, 2, 1, 0, 0, 0, 0, 0},
652 
653                     k63 = {2, 0, 2, 0, 2, 2, 1, 0, 0, 2, 0, 1, 1, 0, 1, 2, 1, 0, 1, 0, 1, 0, 0, 0};
654 
655         FF(a, b, c, d, e[ 0], iShift11, k0);
656 
657         FF(b, c, d, a, e[ 1], iShift12, k1);
658 
659         FF(c, d, a, b, e[ 2], iShift13, k2);
660 
661         FF(d, a, b, c, e[ 3], iShift14, k3);
662 
663         FF(a, b, c, d, e[ 4], iShift11, k4);
664 
665         FF(b, c, d, a, e[ 5], iShift12, k5);
666 
667         FF(c, d, a, b, e[ 6], iShift13, k6);
668 
669         FF(d, a, b, c, e[ 7], iShift14, k7);
670 
671         FF(a, b, c, d, e[ 8], iShift11, k8);
672 
673         FF(b, c, d, a, e[ 9], iShift12, k9);
674 
675         FF(c, d, a, b, e[10], iShift13, k10);
676 
677         FF(d, a, b, c, e[11], iShift14, k11);
678 
679         FF(a, b, c, d, e[12], iShift11, k12);
680 
681         FF(b, c, d, a, e[13], iShift12, k13);
682 
683         FF(c, d, a, b, e[14], iShift13, k14);
684 
685         FF(d, a, b, c, e[15], iShift14, k15);
686 
687         GG(a, b, c, d, e[ 1], iShift21, k16);
688 
689         GG(d, a, b, c, e[ 6], iShift22, k17);
690 
691         GG(c, d, a, b, e[11], iShift23, k18);
692 
693         GG(b, c, d, a, e[ 0], iShift24, k19);
694 
695         GG(a, b, c, d, e[ 5], iShift21, k20);
696 
697         GG(d, a, b, c, e[10], iShift22, k21);
698 
699         GG(c, d, a, b, e[15], iShift23, k22);
700 
701         GG(b, c, d, a, e[ 4], iShift24, k23);
702 
703         GG(a, b, c, d, e[ 9], iShift21, k24);
704 
705         GG(d, a, b, c, e[14], iShift22, k25);
706 
707         GG(c, d, a, b, e[ 3], iShift23, k26);
708 
709         GG(b, c, d, a, e[ 8], iShift24, k27);
710 
711         GG(a, b, c, d, e[13], iShift21, k28);
712 
713         GG(d, a, b, c, e[ 2], iShift22, k29);
714 
715         GG(c, d, a, b, e[ 7], iShift23, k30);
716 
717         GG(b, c, d, a, e[12], iShift24, k31);
718 
719         HH(a, b, c, d, e[ 5], iShift31, k32);
720 
721         HH(b, c, d, a, e[ 8], iShift32, k33);
722 
723         HH(c, d, a, b, e[11], iShift33, k34);
724 
725         HH(d, a, b, c, e[14], iShift34, k35);
726 
727         HH(a, b, c, d, e[ 1], iShift31, k36);
728 
729         HH(b, c, d, a, e[ 4], iShift32, k37);
730 
731         HH(c, d, a, b, e[ 7], iShift33, k38);
732 
733         HH(d, a, b, c, e[10], iShift34, k39);
734 
735         HH(a, b, c, d, e[13], iShift31, k40);
736 
737         HH(b, c, d, a, e[ 0], iShift32, k41);
738 
739         HH(c, d, a, b, e[ 3], iShift33, k42);
740 
741         HH(d, a, b, c, e[ 6], iShift34, k43);
742 
743         HH(a, b, c, d, e[ 9], iShift31, k44);
744 
745         HH(b, c, d, a, e[12], iShift32, k45);
746 
747         HH(c, d, a, b, e[15], iShift33, k46);
748 
749         HH(d, a, b, c, e[ 2], iShift34, k47);
750 
751         II(a, b, c, d, e[ 0], iShift41, k48);
752 
753         II(d, a, b, c, e[ 7], iShift42, k49);
754 
755         II(c, d, a, b, e[14], iShift43, k50);
756 
757         II(b, c, d, a, e[ 5], iShift44, k51);
758 
759         II(a, b, c, d, e[12], iShift41, k52);
760 
761         II(d, a, b, c, e[ 3], iShift42, k53);
762 
763         II(c, d, a, b, e[10], iShift43, k54);
764 
765         II(b, c, d, a, e[ 1], iShift44, k55);
766 
767         II(a, b, c, d, e[ 8], iShift41, k56);
768 
769         II(d, a, b, c, e[15], iShift42, k57);
770 
771         II(c, d, a, b, e[ 6], iShift43, k58);
772 
773         II(b, c, d, a, e[13], iShift44, k59);
774 
775         II(a, b, c, d, e[ 4], iShift41, k60);
776 
777         II(d, a, b, c, e[11], iShift42, k61);
778 
779         II(c, d, a, b, e[ 2], iShift43, k62);
780 
781         II(b, c, d, a, e[ 9], iShift44, k63);
782     }
783 
784     public static void main(String[] args)
785     {
786         if(args.length != 1)
787         {
788             Usage();
789         }
790         else
791         {
792             File fdData = new File(args[0]);
793 
794             long lFileSize = fdData.length();
795 
796             if(lFileSize == 0)
797             {
798                 System.out.printf("There is no data in file [%s], 0 byte.\n", args[0]);
799 
800                 System.exit(-1);
801             }
802 
803             long lBytesLeft = lFileSize & 63;
804 
805             if(lBytesLeft == 0)
806             {
807                 lBytesLeft = lFileSize + 64;
808             }
809             else if(lBytesLeft < 54)
810             {
811                 lBytesLeft = lFileSize - lBytesLeft + 64;
812             }
813             else if(lBytesLeft > 53)
814             {
815                 lBytesLeft = lFileSize - lBytesLeft + 128;
816             }
817 
818             byte[] baData = new byte[(int)lBytesLeft];
819 
820             try
821             {
822                 FileInputStream fisData = new FileInputStream(fdData);
823 
824                 fisData.read(baData, 0, (int)lFileSize);
825 
826                 fisData.close();
827             }
828             catch(Exception e)
829             {
830                 e.printStackTrace();
831             }
832 
833             final byte[] baPadding = {(byte)2, (byte)3, (byte)5, (byte)7, (byte)11, (byte)13, (byte)17, (byte)19, (byte)23, (byte)29, (byte)31, (byte)37,
834                 (byte)41, (byte)43, (byte)47, (byte)53, (byte)59, (byte)61, (byte)67, (byte)71, (byte)73, (byte)79, (byte)83, (byte)89, (byte)97,
835                 (byte)101, (byte)103, (byte)107, (byte)109, (byte)113, (byte)127, (byte)131, (byte)137, (byte)139, (byte)149,
836                 (byte)151, (byte)157, (byte)163, (byte)167, (byte)173, (byte)179, (byte)181, (byte)191, (byte)193, (byte)197,
837                 (byte)199, (byte)211, (byte)223, (byte)227, (byte)229, (byte)233, (byte)239, (byte)241, (byte)251,
838                 (byte)15, (byte)25, (byte)35, (byte)45, (byte)55, (byte)65, (byte)75, (byte)85, (byte)95, (byte)105};
839 
840             for(int i = 0; i < 5; ++i)
841             {
842                 baData[i + (int)lFileSize] = (byte)(lFileSize >>> 8 * i & 255);
843             }
844 
845             for(int j = (int)(lFileSize + 5), k = 0; j < (int)(lBytesLeft - 5); ++j, ++k)
846             {
847                 baData[j] = baPadding[k];
848             }
849 
850             for(int l = 5; l > 0; --l)
851             {
852                 baData[(int)(lBytesLeft - l)] = (byte)(lFileSize >>> 8 * (l - 1) & 255);
853             }
854 
855             byte[] baDigest0 = {1, 2, 1, 0, 0, 0, 2, 1, 2, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0},
856 
857                    baDigest1 = {2, 2, 1, 2, 1, 0, 0, 2, 2, 0, 2, 0, 1, 1, 0, 2, 2, 0, 2, 0, 1, 0, 0, 1},
858 
859                    baDigest2 = {2, 1, 2, 2, 2, 0, 1, 2, 1, 1, 2, 0, 0, 0, 1, 0, 2, 0, 2, 0, 0, 2, 1, 0},
860 
861                    baDigest3 = {1, 1, 2, 0, 1, 0, 0, 2, 1, 2, 0, 0, 2, 2, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0};
862 
863             for(int i = 0; i < (int)lBytesLeft; i += 64)
864             {
865                 byte[][] baTrinary = new byte[16][24];
866 
867                 for(int j = 0; j < 16; ++j)
868                 {
869                     Ternary(((long)baData[i + 4 * j] & 255) + (((long)baData[i + 4 * j + 1] & 255) << 8) + (((long)baData[i + 4 * j + 2] & 255) << 16) + (((long)baData[i + 4 * j + 3] & 255) << 24), baTrinary[j]);
870                 }
871 
872                 MD3HashCode(baDigest0, baDigest1, baDigest2, baDigest3, baTrinary);
873             }
874 
875             for(int k = 0; k < 24; ++k)
876             {
877                 System.out.printf("%d%d%d%d", baDigest0[k], baDigest1[k], baDigest2[k], baDigest3[k]);
878             }
879 
880             System.out.println();
881         }
882     }
883 }

 

发布 Pascal 源码

   1 (*********************************************************
   2 * 作者:伍耀晖               Author: YaoHui.Wu           *
   3 * 开源日期:2022年6月11日    Open Source Date: 2022-6-11 *
   4 * 国家:中国                 Country: China              *
   5 *********************************************************)
   6 (* Compiled by free pascal. free pascal website: www.freepascal.org *)
   7 
   8 Program MD3Hash;
   9 
  10 Type
  11     TrinaryArray = Array[0..23] Of Byte;
  12 
  13 Const
  14     bShift11 = 2;
  15 
  16     bShift12 = 3;
  17 
  18     bShift13 = 4;
  19 
  20     bShift14 = 5;
  21 
  22     bShift21 = 6;
  23 
  24     bShift22 = 7;
  25 
  26     bShift23 = 8;
  27 
  28     bShift24 = 11;
  29 
  30     bShift31 = 10;
  31 
  32     bShift32 = 13;
  33 
  34     bShift33 = 14;
  35 
  36     bShift34 = 17;
  37 
  38     bShift41 = 16;
  39 
  40     bShift42 = 19;
  41 
  42     bShift43 = 22;
  43 
  44     bShift44 = 23;
  45 
  46     baPadding : Array[0..63] Of Byte = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 15, 25, 35, 45, 55, 65, 75, 85, 95, 105);
  47 
  48 Procedure Usage();
  49 Begin
  50     WriteLn('Usage: MD3Hash YouWantToHash.File');
  51 End;
  52 
  53 Procedure Ternary(uiNumeric : LongWord;
  54                   Var baTrinary : TrinaryArray);
  55 Var
  56     i : Byte;
  57 
  58 Begin
  59     baTrinary[0] := 0;
  60 
  61     baTrinary[1] := 0;
  62 
  63     baTrinary[2] := 0;
  64 
  65     baTrinary[3] := 0;
  66 
  67     baTrinary[4] := 0;
  68 
  69     baTrinary[5] := 0;
  70 
  71     baTrinary[6] := 0;
  72 
  73     baTrinary[7] := 0;
  74 
  75     baTrinary[8] := 0;
  76 
  77     baTrinary[9] := 0;
  78 
  79     baTrinary[10] := 0;
  80 
  81     baTrinary[11] := 0;
  82 
  83     baTrinary[12] := 0;
  84 
  85     baTrinary[13] := 0;
  86 
  87     baTrinary[14] := 0;
  88 
  89     baTrinary[15] := 0;
  90 
  91     baTrinary[16] := 0;
  92 
  93     baTrinary[17] := 0;
  94 
  95     baTrinary[18] := 0;
  96 
  97     baTrinary[19] := 0;
  98 
  99     baTrinary[20] := 0;
 100 
 101     baTrinary[21] := 0;
 102 
 103     baTrinary[22] := 0;
 104 
 105     baTrinary[23] := 0;
 106 
 107     If uiNumeric <> 0 Then
 108     Begin
 109         For i := 23 DownTo 0 Do
 110         Begin
 111             baTrinary[i] := uiNumeric Mod 3;
 112 
 113             uiNumeric := uiNumeric Div 3;
 114         End;
 115     End;
 116 End;
 117 
 118 (*
 119  0 0 0
 120  0 1 1
 121  0 1 2
 122 *)
 123 
 124 Procedure TrinaryAnd(Var w : TrinaryArray;
 125                      Var x : TrinaryArray;
 126                      Var y : TrinaryArray);
 127 Var
 128     i : Byte;
 129 
 130 Begin
 131     For i := 0 To 23 Do
 132     Begin
 133         If (x[i] = 0) And (y[i] = 0) Then
 134         Begin
 135             w[i] := 0;
 136         End
 137         Else If (x[i] = 0) And (y[i] = 1) Then
 138         Begin
 139             w[i] := 0;
 140         End
 141         Else If (x[i] = 0) And (y[i] = 2) Then
 142         Begin
 143             w[i] := 0;
 144         End
 145         Else If (x[i] = 1) And (y[i] = 0) Then
 146         Begin
 147             w[i] := 0;
 148         End
 149         Else If (x[i] = 1) And (y[i] = 1) Then
 150         Begin
 151             w[i] := 1;
 152         End
 153         Else If (x[i] = 1) And (y[i] = 2) Then
 154         Begin
 155             w[i] := 1;
 156         End
 157         Else If (x[i] = 2) And (y[i] = 0) Then
 158         Begin
 159             w[i] := 0;
 160         End
 161         Else If (x[i] = 2) And (y[i] = 1) Then
 162         Begin
 163             w[i] := 1;
 164         End
 165         Else If (x[i] = 2) And (y[i] = 2) Then
 166         Begin
 167             w[i] := 2;
 168         End;
 169     End;
 170 End;
 171 
 172 (*
 173  0 1 2
 174  1 1 2
 175  2 2 2
 176 *)
 177 
 178 Procedure TrinaryOr(Var w : TrinaryArray;
 179                     Var x : TrinaryArray;
 180                     Var y : TrinaryArray);
 181 Var
 182     j : Byte;
 183 
 184 Begin
 185     For j := 0 To 23 Do
 186     Begin
 187         If (x[j] = 0) And (y[j] = 0) Then
 188         Begin
 189             w[j] := 0;
 190         End
 191         Else If (x[j] = 0) And (y[j] = 1) Then
 192         Begin
 193             w[j] := 1;
 194         End
 195         Else If (x[j] = 0) And (y[j] = 2) Then
 196         Begin
 197             w[j] := 2;
 198         End
 199         Else If (x[j] = 1) And (y[j] = 0) Then
 200         Begin
 201             w[j] := 1;
 202         End
 203         Else If (x[j] = 1) And (y[j] = 1) Then
 204         Begin
 205             w[j] := 1;
 206         End
 207         Else If (x[j] = 1) And (y[j] = 2) Then
 208         Begin
 209             w[j] := 2;
 210         End
 211         Else If (x[j] = 2) And (y[j] = 0) Then
 212         Begin
 213             w[j] := 2;
 214         End
 215         Else If (x[j] = 2) And (y[j] = 1) Then
 216         Begin
 217             w[j] := 2;
 218         End
 219         Else If (x[j] = 2) And (y[j] = 2) Then
 220         Begin
 221             w[j] := 2;
 222         End;
 223     End;
 224 End;
 225 
 226 // 0 0 2
 227 // 1 1 1
 228 // 2 2 0
 229 
 230 Procedure TrinaryXor(Var w : TrinaryArray;
 231                      Var x : TrinaryArray;
 232                      Var y : TrinaryArray);
 233 Var
 234     k : Byte;
 235 
 236 Begin
 237     For k := 0 To 23 Do
 238     Begin
 239         If (x[k] = 0) And (y[k] = 0) Then
 240         Begin
 241             w[k] := 0;
 242         End
 243         Else If (x[k] = 0) And (y[k] = 1) Then
 244         Begin
 245             w[k] := 0;
 246         End
 247         Else If (x[k] = 0) And (y[k] = 2) Then
 248         Begin
 249             w[k] := 2;
 250         End
 251         Else If (x[k] = 1) And (y[k] = 0) Then
 252         Begin
 253             w[k] := 1;
 254         End
 255         Else If (x[k] = 1) And (y[k] = 1) Then
 256         Begin
 257             w[k] := 1;
 258         End
 259         Else If (x[k] = 1) And (y[k] = 2) Then
 260         Begin
 261             w[k] := 1;
 262         End
 263         Else If (x[k] = 2) And (y[k] = 0) Then
 264         Begin
 265             w[k] := 2;
 266         End
 267         Else If (x[k] = 2) And (y[k] = 1) Then
 268         Begin
 269             w[k] := 2;
 270         End
 271         Else If (x[k] = 2) And (y[k] = 2) Then
 272         Begin
 273             w[k] := 0;
 274         End;
 275     End;
 276 End;
 277 
 278 (*
 279  2 0 0
 280  1 1 1
 281  0 2 2
 282 *)
 283 
 284 Procedure TrinaryXand(Var w : TrinaryArray;
 285                       Var x : TrinaryArray;
 286                       Var y : TrinaryArray);
 287 Var
 288     i : Byte;
 289 
 290 Begin
 291     For i := 0 To 23 Do
 292     Begin
 293         If (x[i] = 0) And (y[i] = 0) Then
 294         Begin
 295             w[i] := 2;
 296         End
 297         Else If (x[i] = 0) And (y[i] = 1) Then
 298         Begin
 299             w[i] := 0;
 300         End
 301         Else If (x[i] = 0) And (y[i] = 2) Then
 302         Begin
 303             w[i] := 0;
 304         End
 305         Else If (x[i] = 1) And (y[i] = 0) Then
 306         Begin
 307             w[i] := 1;
 308         End
 309         Else If (x[i] = 1) And (y[i] = 1) Then
 310         Begin
 311             w[i] := 1;
 312         End
 313         Else If (x[i] = 1) And (y[i] = 2) Then
 314         Begin
 315             w[i] := 1;
 316         End
 317         Else If (x[i] = 2) And (y[i] = 0) Then
 318         Begin
 319             w[i] := 0;
 320         End
 321         Else If (x[i] = 2) And (y[i] = 1) Then
 322         Begin
 323             w[i] := 2;
 324         End
 325         Else If (x[i] = 2) And (y[i] = 2) Then
 326         Begin
 327             w[i] := 2;
 328         End;
 329     End;
 330 End;
 331 
 332 (*
 333  0 1 2
 334  1 2 0
 335  2 0 1
 336 *)
 337 
 338 Procedure TrinaryAdd(Var w : TrinaryArray;
 339                      Var x : TrinaryArray;
 340                      Var y : TrinaryArray);
 341 Var
 342     j : Byte;
 343 
 344 Begin
 345     For j := 0 To 23 Do
 346     Begin
 347         If (x[j] = 0) And (y[j] = 0) Then
 348         Begin
 349             w[j] := 0;
 350         End
 351         Else If (x[j] = 0) And (y[j] = 1) Then
 352         Begin
 353             w[j] := 1;
 354         End
 355         Else If (x[j] = 0) And (y[j] = 2) Then
 356         Begin
 357             w[j] := 2;
 358         End
 359         Else If (x[j] = 1) And (y[j] = 0) Then
 360         Begin
 361             w[j] := 1;
 362         End
 363         Else If (x[j] = 1) And (y[j] = 1) Then
 364         Begin
 365             w[j] := 2;
 366         End
 367         Else If (x[j] = 1) And (y[j] = 2) Then
 368         Begin
 369             w[j] := 0;
 370         End
 371         Else If (x[j] = 2) And (y[j] = 0) Then
 372         Begin
 373             w[j] := 2;
 374         End
 375         Else If (x[j] = 2) And (y[j] = 1) Then
 376         Begin
 377             w[j] := 0;
 378         End
 379         Else If (x[j] = 2) And (y[j] = 2) Then
 380         Begin
 381             w[j] := 1;
 382         End;
 383     End;
 384 End;
 385 
 386 (*
 387  0 0 0
 388  0 1 2
 389  0 2 1
 390 *)
 391 
 392 Procedure TrinaryMultiplication(Var w : TrinaryArray;
 393                                 Var x : TrinaryArray;
 394                                 Var y : TrinaryArray);
 395 Var
 396     k : Byte;
 397 
 398 Begin
 399     For k := 0 To 23 Do
 400     Begin
 401         If (x[k] = 0) And (y[k] = 0) Then
 402         Begin
 403             w[k] := 0;
 404         End
 405         Else If (x[k] = 0) And (y[k] = 1) Then
 406         Begin
 407             w[k] := 0;
 408         End
 409         Else If (x[k] = 0) And (y[k] = 2) Then
 410         Begin
 411             w[k] := 0;
 412         End
 413         Else If (x[k] = 1) And (y[k] = 0) Then
 414         Begin
 415             w[k] := 0;
 416         End
 417         Else If (x[k] = 1) And (y[k] = 1) Then
 418         Begin
 419             w[k] := 1;
 420         End
 421         Else If (x[k] = 1) And (y[k] = 2) Then
 422         Begin
 423             w[k] := 2;
 424         End
 425         Else If (x[k] = 2) And (y[k] = 0) Then
 426         Begin
 427             w[k] := 0;
 428         End
 429         Else If (x[k] = 2) And (y[k] = 1) Then
 430         Begin
 431             w[k] := 2;
 432         End
 433         Else If (x[k] = 2) And (y[k] = 2) Then
 434         Begin
 435             w[k] := 1;
 436         End;
 437     End;
 438 End;
 439 
 440 Procedure F(Var w : TrinaryArray;
 441             Var x : TrinaryArray;
 442             Var y : TrinaryArray;
 443             Var z : TrinaryArray);
 444 Begin
 445     TrinaryAnd(w, x, y);
 446 
 447     TrinaryOr(w, w, z);
 448 End;
 449 
 450 Procedure G(Var w : TrinaryArray;
 451             Var x : TrinaryArray;
 452             Var y : TrinaryArray;
 453             Var z : TrinaryArray);
 454 Begin
 455     TrinaryAdd(w, x, z);
 456 
 457     TrinaryMultiplication(w, w, y);
 458 End;
 459 
 460 Procedure H(Var w : TrinaryArray;
 461             Var x : TrinaryArray;
 462             Var y : TrinaryArray;
 463             Var z : TrinaryArray);
 464 Begin
 465     TrinaryXor(w, x, y);
 466 
 467     TrinaryXand(w, w, z);
 468 End;
 469 
 470 Procedure I1(Var w : TrinaryArray;
 471             Var x : TrinaryArray;
 472             Var y : TrinaryArray;
 473             Var z : TrinaryArray);
 474 Begin
 475     TrinaryMultiplication(w, y, z);
 476 
 477     TrinaryAdd(w, w, x);
 478 End;
 479 
 480 Procedure Add(Var w : TrinaryArray;
 481               Var z : TrinaryArray);
 482 Var
 483     bCarry, i : Byte;
 484 
 485 Begin
 486     bCarry := 0;
 487 
 488     For i := 0 To 23 Do
 489     Begin
 490         w[i] := w[i] + z[i] + bCarry;
 491 
 492         If w[i] > 2 Then
 493         Begin
 494             w[i] := w[i] - 3;
 495 
 496             If bCarry = 0 Then
 497             Begin
 498                 bCarry := 1;
 499             End;
 500         End
 501         Else
 502         Begin
 503             bCarry := 0;
 504         End;
 505     End;
 506 End;
 507 
 508 Procedure RotateShiftLeft(Var w : TrinaryArray;
 509                           bBit: Byte);
 510 Var
 511     baSwap : TrinaryArray = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
 512 
 513     i, j, k : Byte;
 514 
 515 Begin
 516     For i := 23 DownTo 0 Do
 517     Begin
 518         If i >= bBit Then j := i - bBit Else j := 24 - bBit + i;
 519 
 520         baSwap[i] := w[j];
 521     End;
 522 
 523     For k := 0 To 23 Do
 524     Begin
 525         w[k] := baSwap[k];
 526     End;
 527 End;
 528 
 529 Procedure FF(Var a : TrinaryArray;
 530              Var b : TrinaryArray;
 531              Var c : TrinaryArray;
 532              Var d : TrinaryArray;
 533              Var e : TrinaryArray;
 534              bShift : Byte;
 535              Var k : TrinaryArray);
 536 Var
 537     baTemp : TrinaryArray = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
 538 
 539     i : Byte;
 540 
 541 Begin
 542     F(baTemp, b, c, d);
 543 
 544     Add(baTemp, a);
 545 
 546     Add(baTemp, e);
 547 
 548     Add(baTemp, k);
 549 
 550     RotateShiftLeft(baTemp, bShift);
 551 
 552     Add(baTemp, b);
 553 
 554     For i := 0 To 23 Do
 555     Begin
 556         a[i] := baTemp[i];
 557     End;
 558 End;
 559 
 560 Procedure GG(Var a : TrinaryArray;
 561              Var b : TrinaryArray;
 562              Var c : TrinaryArray;
 563              Var d : TrinaryArray;
 564              Var e : TrinaryArray;
 565              bShift : Byte;
 566              Var k : TrinaryArray);
 567 Var
 568     baTemp : TrinaryArray = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
 569 
 570     j : Byte;
 571 
 572 Begin
 573     G(baTemp, c, d, a);
 574 
 575     Add(baTemp, b);
 576 
 577     Add(baTemp, e);
 578 
 579     Add(baTemp, k);
 580 
 581     RotateShiftLeft(baTemp, bShift);
 582 
 583     Add(baTemp, c);
 584 
 585     for j := 0 To 23 Do
 586     Begin
 587         b[j] := baTemp[j];
 588     End;
 589 End;
 590 
 591 Procedure HH(Var a : TrinaryArray;
 592              Var b : TrinaryArray;
 593              Var c : TrinaryArray;
 594              Var d : TrinaryArray;
 595              Var e : TrinaryArray;
 596              bShift : Byte;
 597              Var k : TrinaryArray);
 598 Var
 599     baTemp : TrinaryArray = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
 600 
 601     i : Byte;
 602 
 603 Begin
 604     H(baTemp, d, a, b);
 605 
 606     Add(baTemp, c);
 607 
 608     Add(baTemp, e);
 609 
 610     Add(baTemp, k);
 611 
 612     RotateShiftLeft(baTemp, bShift);
 613 
 614     Add(baTemp, d);
 615 
 616     For i := 0 To 23 Do
 617     Begin
 618         c[i] := baTemp[i];
 619     End;
 620 End;
 621 
 622 Procedure II(Var a : TrinaryArray;
 623              Var b : TrinaryArray;
 624              Var c : TrinaryArray;
 625              Var d : TrinaryArray;
 626              Var e : TrinaryArray;
 627              bShift : Byte;
 628              Var k : TrinaryArray);
 629 Var
 630     baTemp : TrinaryArray = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
 631 
 632     j : Byte;
 633 
 634 Begin
 635     I1(baTemp, a, b, c);
 636 
 637     Add(baTemp, d);
 638 
 639     Add(baTemp, e);
 640 
 641     Add(baTemp, k);
 642 
 643     RotateShiftLeft(baTemp, bShift);
 644 
 645     Add(baTemp, a);
 646 
 647     For j := 0 To 23 Do
 648     Begin
 649         d[j] := baTemp[j];
 650     End;
 651 End;
 652 
 653 Procedure MD3Hash(Var a : TrinaryArray;
 654                   Var b : TrinaryArray;
 655                   Var c : TrinaryArray;
 656                   Var d : TrinaryArray;
 657                   Var e : Array Of TrinaryArray);
 658 Const
 659     k0 : TrinaryArray = (2, 1, 1, 1, 2, 2, 0, 1, 2, 2, 1, 1, 2, 1, 2, 2, 2, 0, 0, 0, 1, 0, 0, 0);
 660 
 661     k1 : TrinaryArray = (1, 1, 2, 0, 0, 1, 0, 0, 0, 1, 0, 2, 1, 1, 0, 2, 0, 0, 1, 0, 1, 0, 0, 0);
 662 
 663     k2 : TrinaryArray = (0, 0, 2, 2, 1, 2, 0, 0, 1, 1, 1, 1, 0, 2, 0, 0, 2, 1, 1, 0, 0, 0, 0, 0);
 664 
 665     k3 : TrinaryArray = (1, 1, 1, 0, 1, 0, 0, 2, 1, 1, 2, 0, 2, 1, 1, 1, 0, 1, 2, 2, 0, 0, 0, 0);
 666 
 667     k4 : TrinaryArray = (1, 2, 1, 1, 2, 0, 1, 2, 2, 2, 0, 2, 0, 0, 0, 2, 2, 1, 1, 0, 1, 0, 0, 0);
 668 
 669     k5 : TrinaryArray = (2, 1, 0, 2, 1, 2, 1, 0, 1, 1, 1, 0, 2, 2, 1, 2, 0, 0, 0, 1, 0, 0, 0, 0);
 670 
 671     k6 : TrinaryArray = (2, 0, 0, 1, 0, 0, 1, 0, 0, 1, 2, 1, 2, 2, 1, 1, 2, 0, 1, 2, 0, 0, 0, 0);
 672 
 673     k7 : TrinaryArray = (2, 2, 1, 0, 0, 2, 1, 1, 2, 1, 0, 2, 0, 1, 0, 2, 2, 2, 1, 0, 1, 0, 0, 0);
 674 
 675     k8 : TrinaryArray = (1, 2, 0, 0, 1, 0, 0, 1, 0, 2, 2, 1, 0, 0, 1, 0, 2, 1, 1, 1, 0, 0, 0, 0);
 676 
 677     k9 : TrinaryArray = (2, 1, 1, 2, 2, 2, 1, 1, 0, 2, 2, 1, 1, 1, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0);
 678 
 679     k10 : TrinaryArray = (1, 2, 2, 0, 1, 2, 0, 1, 2, 2, 2, 1, 2, 2, 0, 2, 0, 0, 2, 0, 1, 0, 0, 0);
 680 
 681     k11 : TrinaryArray = (1, 1, 0, 2, 2, 0, 1, 2, 2, 2, 0, 1, 1, 2, 1, 1, 2, 2, 2, 1, 0, 0, 0, 0);
 682 
 683     k12 : TrinaryArray = (2, 0, 1, 2, 1, 2, 0, 0, 1, 0, 0, 2, 2, 0, 2, 2, 2, 1, 1, 1, 0, 0, 0, 0);
 684 
 685     k13 : TrinaryArray = (2, 2, 2, 0, 2 ,2, 1, 0, 1, 1, 1, 2, 1, 1, 1, 2, 2, 2, 1, 0, 1, 0, 0, 0);
 686 
 687     k14 : TrinaryArray = (1, 0, 1, 1, 0, 2, 2, 2, 0, 0, 1, 1, 2, 2, 1, 2, 1, 0, 1, 2, 0, 0, 0, 0);
 688 
 689     k15 : TrinaryArray = (0, 1, 2, 0, 2, 1, 1, 1, 1, 2, 0, 2, 1, 1, 0, 2, 1, 0, 0, 1, 0, 0, 0, 0);
 690 
 691     k16 : TrinaryArray = (0, 0, 1, 0, 1, 1, 1, 2, 1, 2 ,0, 2, 2, 0, 2, 2, 2, 1, 1, 0, 1, 0, 0, 0);
 692 
 693     k17 : TrinaryArray = (1, 2, 0, 2, 0, 0, 2, 2, 1, 1, 2, 0, 0, 1, 2, 2, 2, 0, 2, 2, 0, 0, 0, 0);
 694 
 695     k18 : TrinaryArray = (0, 1, 2, 0, 0, 2, 0, 2, 0, 1, 2, 0, 2, 1, 2, 2, 2, 1, 1, 0, 0, 0, 0, 0);
 696 
 697     k19 : TrinaryArray = (1, 2, 1, 1, 1, 2, 2, 2, 2, 1, 1, 0, 1, 2, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0);
 698 
 699     k20 : TrinaryArray = (1, 2, 1, 0, 2, 2, 1, 0, 0, 2, 2, 1, 2, 0, 1, 1, 2, 0, 0, 0, 1, 0, 0, 0);
 700 
 701     k21 : TrinaryArray = (2, 0, 0, 1, 2, 0, 2, 0, 1, 2, 1, 1, 2, 2, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0);
 702 
 703     k22 : TrinaryArray = (1, 2, 0, 0, 2, 1, 1, 1, 0, 1, 2, 2, 1, 2, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0);
 704 
 705     k23 : TrinaryArray = (2, 1, 1, 1, 1, 0, 1, 1, 1, 2, 2, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0);
 706 
 707     k24 : TrinaryArray = (0, 1, 2, 0, 2, 2, 1, 0, 0, 2, 2, 1, 1, 2, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0);
 708 
 709     k25 : TrinaryArray = (0, 1, 2, 1, 1, 2, 2, 1, 1, 0, 1, 2, 0, 2, 0, 0, 1, 1, 2, 2, 0, 0, 0, 0);
 710 
 711     k26 : TrinaryArray = (2, 1, 2, 0, 1, 1, 2, 1, 2, 1, 1, 0, 1, 2, 0, 1, 2, 1, 1, 0, 1, 0, 0, 0);
 712 
 713     k27 : TrinaryArray = (2, 2, 0, 1, 1, 0, 2, 1, 1, 1, 0, 1, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0);
 714 
 715     k28 : TrinaryArray = (1, 1, 2, 2, 0, 0, 2, 1, 1, 2, 2, 0, 2, 2, 1, 0, 0, 1, 1, 2, 0, 0, 0, 0);
 716 
 717     k29 : TrinaryArray = (2, 2, 2, 2, 0, 2, 0, 0, 1, 0, 0, 0, 2, 0, 2, 1, 2, 2, 1, 0, 1, 0, 0, 0);
 718 
 719     k30 : TrinaryArray = (1, 2, 2, 0, 1, 0, 1, 1, 2, 2, 2, 0, 1, 2, 2, 0, 1, 1, 1, 1, 0, 0, 0, 0);
 720 
 721     k31 : TrinaryArray = (1, 1, 2, 2, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0);
 722 
 723     k32 : TrinaryArray = (1, 1, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 2, 2, 0, 2, 0, 0, 2, 0, 1, 0, 0, 0);
 724 
 725     k33 : TrinaryArray = (2, 1, 1, 1, 2, 2, 1, 1, 1, 0, 2, 2, 0, 0, 1, 2, 1, 2, 2, 1, 0, 0, 0, 0);
 726 
 727     k34 : TrinaryArray = (1, 0, 0, 0, 0, 2, 2, 0, 1, 0, 1, 1, 1, 1, 0, 2, 0, 2, 1, 1, 0, 0, 0, 0);
 728 
 729     k35 : TrinaryArray = (1, 1, 1, 0, 2, 2, 0, 0, 0, 2, 2, 0, 2, 1, 2, 2, 2, 2, 1, 0, 1, 0, 0, 0);
 730 
 731     k36 : TrinaryArray = (2, 1, 0, 0, 2, 0, 1, 1, 1, 0, 2, 2, 1, 2, 1, 0, 1, 0, 1, 2, 0, 0, 0, 0);
 732 
 733     k37 : TrinaryArray = (1, 2, 0, 2, 0, 1, 0, 0, 2, 1, 1, 0, 1, 0, 2, 1, 2, 0, 0, 1, 0, 0, 0, 0);
 734 
 735     k38 : TrinaryArray = (1, 2, 0, 2, 1, 2, 1, 1, 2, 0, 1, 0, 1, 1, 1, 0, 0, 2, 1, 0, 1, 0, 0, 0);
 736 
 737     k39 : TrinaryArray = (0, 2, 1, 1, 0, 1, 2, 1, 2, 0, 1, 2, 0, 0, 0, 1, 2, 0, 2, 2, 0, 0, 0, 0);
 738 
 739     k40 : TrinaryArray = (0, 0, 0, 0, 0, 1, 0, 2, 1, 1, 2, 2, 0, 1, 1, 2, 0, 2, 1, 0, 0, 0, 0, 0);
 740 
 741     k41 : TrinaryArray = (0, 1, 1, 0, 2, 2, 0, 0, 1, 2, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0);
 742 
 743     k42 : TrinaryArray = (2, 1, 1, 0, 0, 2, 0, 0, 0, 2, 1, 0, 2, 2, 2, 2, 1, 0, 0, 0, 1, 0, 0, 0);
 744 
 745     k43 : TrinaryArray = (0, 2, 2, 2, 0, 1, 0, 0, 2, 1, 0, 0, 2, 2, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0);
 746 
 747     k44 : TrinaryArray = (1, 2, 1, 0, 2, 1, 1, 0, 0, 0, 1, 2, 0, 0, 2, 2, 0, 1, 0, 0, 1, 0, 0, 0);
 748 
 749     k45 : TrinaryArray = (0, 1, 0, 2, 2, 2, 0, 1, 1, 0, 0, 0, 1, 2, 2, 2, 2, 2, 0, 0, 1, 0, 0, 0);
 750 
 751     k46 : TrinaryArray = (1, 0, 1, 2, 1, 2, 1, 1, 1, 0, 0, 2, 2, 2, 2, 0, 0, 1, 1, 0, 0, 0, 0, 0);
 752 
 753     k47 : TrinaryArray = (0, 0, 1, 0, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, 2, 1, 1, 1, 2, 2, 0, 0, 0, 0);
 754 
 755     k48 : TrinaryArray = (0, 2, 2, 2, 1, 0, 0, 1, 1, 2, 2, 2, 0, 1, 1, 0, 2, 1, 1, 0, 1, 0, 0, 0);
 756 
 757     k49 : TrinaryArray = (2, 0, 0, 2, 0, 1, 0, 0, 0, 0, 1, 1, 2, 1, 1, 0, 2, 2, 2, 0, 0, 0, 0, 0);
 758 
 759     k50 : TrinaryArray = (2, 0, 2, 1, 1, 2, 2, 2, 1, 1, 2, 1, 1, 2, 1, 2, 0, 1, 1, 2, 0, 0, 0, 0);
 760 
 761     k51 : TrinaryArray = (1, 2, 1, 0, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 0, 1, 2, 2, 1, 0, 1, 0, 0, 0);
 762 
 763     k52 : TrinaryArray = (2, 0, 0, 0, 0, 2, 1, 2, 1, 2, 0, 2, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0);
 764 
 765     k53 : TrinaryArray = (1, 0, 2, 0, 2, 1, 2, 2, 1, 2, 2, 2, 0, 2, 0, 2, 1, 0, 0, 2, 0, 0, 0, 0);
 766 
 767     k54 : TrinaryArray = (2, 1, 1, 2, 0, 0, 2, 1, 1, 2, 0, 2, 0, 2, 0, 2, 0, 0, 2, 0, 1, 0, 0, 0);
 768 
 769     k55 : TrinaryArray = (0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 2, 2, 1, 0, 0, 0, 0);
 770 
 771     k56 : TrinaryArray = (1, 2, 0, 1, 1, 2, 1, 1, 0, 2, 2, 2, 2, 1, 1, 1, 1, 2, 1, 1, 0, 0, 0, 0);
 772 
 773     k57 : TrinaryArray = (2, 1, 1, 2, 0, 0, 1, 0, 2, 0, 1, 0, 2, 1, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0);
 774 
 775     k58 : TrinaryArray = (2, 2, 1, 0, 0, 2, 2, 2, 1, 1, 2, 2, 0, 2, 1, 1, 0, 0, 1, 2, 0, 0, 0, 0);
 776 
 777     k59 : TrinaryArray = (0, 1, 0, 0, 1, 1, 0, 1, 2, 1, 0, 1, 0, 2, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0);
 778 
 779     k60 : TrinaryArray = (1, 1, 1, 1, 0, 1, 1, 2, 1, 0, 2, 2, 1, 1, 0, 1, 0, 2, 1, 0, 1, 0, 0, 0);
 780 
 781     k61 : TrinaryArray = (2, 1, 1, 2, 0, 2, 0, 0, 1, 2, 1, 2, 0, 2, 0, 2, 1, 0, 2, 2, 0, 0, 0, 0);
 782 
 783     k62 : TrinaryArray = (0, 0, 1, 2, 0, 2, 1, 1, 0, 2, 1, 1, 2, 0, 0, 2, 1, 2, 1, 0, 0, 0, 0, 0);
 784 
 785     k63 : TrinaryArray = (2, 0, 2, 0, 2, 2, 1, 0, 0, 2, 0, 1, 1, 0, 1, 2, 1, 0, 1, 0, 1, 0, 0, 0);
 786 
 787 Begin
 788 
 789     FF(a, b, c, d, e[ 0], bShift11, k0);
 790 
 791     FF(b, c, d, a, e[ 1], bShift12, k1);
 792 
 793     FF(c, d, a, b, e[ 2], bShift13, k2);
 794 
 795     FF(d, a, b, c, e[ 3], bShift14, k3);
 796 
 797     FF(a, b, c, d, e[ 4], bShift11, k4);
 798 
 799     FF(b, c, d, a, e[ 5], bShift12, k5);
 800 
 801     FF(c, d, a, b, e[ 6], bShift13, k6);
 802 
 803     FF(d, a, b, c, e[ 7], bShift14, k7);
 804 
 805     FF(a, b, c, d, e[ 8], bShift11, k8);
 806 
 807     FF(b, c, d, a, e[ 9], bShift12, k9);
 808 
 809     FF(c, d, a, b, e[10], bShift13, k10);
 810 
 811     FF(d, a, b, c, e[11], bShift14, k11);
 812 
 813     FF(a, b, c, d, e[12], bShift11, k12);
 814 
 815     FF(b, c, d, a, e[13], bShift12, k13);
 816 
 817     FF(c, d, a, b, e[14], bShift13, k14);
 818 
 819     FF(d, a, b, c, e[15], bShift14, k15);
 820 
 821     GG(a, b, c, d, e[ 1], bShift21, k16);
 822 
 823     GG(d, a, b, c, e[ 6], bShift22, k17);
 824 
 825     GG(c, d, a, b, e[11], bShift23, k18);
 826 
 827     GG(b, c, d, a, e[ 0], bShift24, k19);
 828 
 829     GG(a, b, c, d, e[ 5], bShift21, k20);
 830 
 831     GG(d, a, b, c, e[10], bShift22, k21);
 832 
 833     GG(c, d, a, b, e[15], bShift23, k22);
 834 
 835     GG(b, c, d, a, e[ 4], bShift24, k23);
 836 
 837     GG(a, b, c, d, e[ 9], bShift21, k24);
 838 
 839     GG(d, a, b, c, e[14], bShift22, k25);
 840 
 841     GG(c, d, a, b, e[ 3], bShift23, k26);
 842 
 843     GG(b, c, d, a, e[ 8], bShift24, k27);
 844 
 845     GG(a, b, c, d, e[13], bShift21, k28);
 846 
 847     GG(d, a, b, c, e[ 2], bShift22, k29);
 848 
 849     GG(c, d, a, b, e[ 7], bShift23, k30);
 850 
 851     GG(b, c, d, a, e[12], bShift24, k31);
 852 
 853     HH(a, b, c, d, e[ 5], bShift31, k32);
 854 
 855     HH(b, c, d, a, e[ 8], bShift32, k33);
 856 
 857     HH(c, d, a, b, e[11], bShift33, k34);
 858 
 859     HH(d, a, b, c, e[14], bShift34, k35);
 860 
 861     HH(a, b, c, d, e[ 1], bShift31, k36);
 862 
 863     HH(b, c, d, a, e[ 4], bShift32, k37);
 864 
 865     HH(c, d, a, b, e[ 7], bShift33, k38);
 866 
 867     HH(d, a, b, c, e[10], bShift34, k39);
 868 
 869     HH(a, b, c, d, e[13], bShift31, k40);
 870 
 871     HH(b, c, d, a, e[ 0], bShift32, k41);
 872 
 873     HH(c, d, a, b, e[ 3], bShift33, k42);
 874 
 875     HH(d, a, b, c, e[ 6], bShift34, k43);
 876 
 877     HH(a, b, c, d, e[ 9], bShift31, k44);
 878 
 879     HH(b, c, d, a, e[12], bShift32, k45);
 880 
 881     HH(c, d, a, b, e[15], bShift33, k46);
 882 
 883     HH(d, a, b, c, e[ 2], bShift34, k47);
 884 
 885     II(a, b, c, d, e[ 0], bShift41, k48);
 886 
 887     II(d, a, b, c, e[ 7], bShift42, k49);
 888 
 889     II(c, d, a, b, e[14], bShift43, k50);
 890 
 891     II(b, c, d, a, e[ 5], bShift44, k51);
 892 
 893     II(a, b, c, d, e[12], bShift41, k52);
 894 
 895     II(d, a, b, c, e[ 3], bShift42, k53);
 896 
 897     II(c, d, a, b, e[10], bShift43, k54);
 898 
 899     II(b, c, d, a, e[ 1], bShift44, k55);
 900 
 901     II(a, b, c, d, e[ 8], bShift41, k56);
 902 
 903     II(d, a, b, c, e[15], bShift42, k57);
 904 
 905     II(c, d, a, b, e[ 6], bShift43, k58);
 906 
 907     II(b, c, d, a, e[13], bShift44, k59);
 908 
 909     II(a, b, c, d, e[ 4], bShift41, k60);
 910 
 911     II(d, a, b, c, e[11], bShift42, k61);
 912 
 913     II(c, d, a, b, e[ 2], bShift43, k62);
 914 
 915     II(b, c, d, a, e[ 9], bShift44, k63);
 916 End;
 917 
 918 Var
 919     fdData : File;
 920 
 921     ulFileSize, ulBytesLeft : QWord;
 922 
 923     bpData : PByte;
 924 
 925     i, j, k, l : LongWord;
 926 
 927     baDigest0 : TrinaryArray = (1, 2, 1, 0, 0, 0, 2, 1, 2, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0);
 928 
 929     baDigest1 : TrinaryArray = (2, 2, 1, 2, 1, 0, 0, 2, 2, 0, 2, 0, 1, 1, 0, 2, 2, 0, 2, 0, 1, 0, 0, 1);
 930 
 931     baDigest2 : TrinaryArray = (2, 1, 2, 2, 2, 0, 1, 2, 1, 1, 2, 0, 0, 0, 1, 0, 2, 0, 2, 0, 0, 2, 1, 0);
 932 
 933     baDigest3 : TrinaryArray = (1, 1, 2, 0, 1, 0, 0, 2, 1, 2, 0, 0, 2, 2, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0);
 934 
 935     baTrinary : Array[0..15] Of TrinaryArray;
 936 
 937 Begin
 938     If ParamCount <> 1 Then
 939     Begin
 940         Usage();
 941     End
 942     Else
 943     Begin
 944         Assign(fdData, ParamStr(1));
 945 
 946         Reset(fdData, 1);
 947 
 948         ulFileSize := FileSize(fdData);
 949 
 950         If ulFileSize = 0 Then
 951         Begin
 952             WriteLn('There is no data in file [', ParamStr(1), '], 0 byte.');
 953 
 954             Halt(-1);
 955         End;
 956 
 957         ulBytesLeft := ulFileSize And 63;
 958 
 959         If ulBytesLeft = 0 Then
 960         Begin
 961             ulBytesLeft := ulFileSize + 64;
 962         End
 963         Else If ulBytesLeft < 54 Then
 964         Begin
 965             ulBytesLeft := ulFileSize - ulBytesLeft + 64;
 966         End
 967         Else If ulBytesLeft > 53 Then
 968         Begin
 969             ulBytesLeft := ulFileSize - ulBytesLeft + 128;
 970         End;
 971 
 972         bpData := GetMem(ulBytesLeft);
 973 
 974         BlockRead(fdData, bpData^, ulFileSize);
 975 
 976         Close(fdData);
 977 
 978         For i := 0 To 4 Do
 979         Begin
 980             bpData[i + ulFileSize] := ulFileSize Shr (8 * i);
 981         End;
 982 
 983         k := 0;
 984 
 985         For j := ulFileSize + 5 To ulBytesLeft - 6 Do
 986         Begin
 987             bpData[j] := baPadding[k];
 988 
 989             k := k + 1;
 990         End;
 991 
 992         For l := 4 DownTo 0 Do
 993         Begin
 994             bpData[ulBytesLeft - l - 1] := ulFileSize Shr (8 * l);
 995         End;
 996 
 997         i := 0;
 998 
 999         While i < ulBytesLeft Do
1000         Begin
1001             For j := 0 To 15 Do
1002             Begin
1003                 Ternary(bpData[i + 4 * j] + (bpData[i + 4 * j + 1] << 8) + (bpData[i + 4 * j + 2] << 16) + (bpData[i + 4 * j + 3] << 24), baTrinary[j]);
1004             End;
1005 
1006             MD3Hash(baDigest0, baDigest1, baDigest2, baDigest3, baTrinary);
1007 
1008             i := i + 64;
1009         End;
1010 
1011         For k := 0 To 23 Do
1012         Begin
1013             Write(baDigest0[k], baDigest1[k], baDigest2[k], baDigest3[k]);
1014         End;
1015 
1016         WriteLn();
1017 
1018         FreeMem(bpData);
1019     End;
1020 End.

 

发布 Basic 源码

  1 /'********************************************************
  2 * 作者:伍耀晖               Author: YaoHui.Wu           *
  3 * 开源日期:2022年6月11日    Open Source Date: 2022-6-11 *
  4 * 国家:中国                 Country: China              *
  5 ********************************************************'/
  6 ' Compiled by free basic. free basic website: www.freebasic.net
  7 
  8 #include "file.bi"
  9 
 10 Sub Usage()
 11     Print "Usage: MD3Hash YouWantToHash.File"
 12 End Sub
 13 
 14 Sub Ternary(ByVal ulNumeric As ULongInt, ubpTrinary As UByte Pointer)
 15     ubpTrinary[0] = 0
 16 
 17     ubpTrinary[1] = 0
 18 
 19     ubpTrinary[2] = 0
 20 
 21     ubpTrinary[3] = 0
 22 
 23     ubpTrinary[4] = 0
 24 
 25     ubpTrinary[5] = 0
 26 
 27     ubpTrinary[6] = 0
 28 
 29     ubpTrinary[7] = 0
 30 
 31     ubpTrinary[8] = 0
 32 
 33     ubpTrinary[9] = 0
 34 
 35     ubpTrinary[10] = 0
 36 
 37     ubpTrinary[11] = 0
 38 
 39     ubpTrinary[12] = 0
 40 
 41     ubpTrinary[13] = 0
 42 
 43     ubpTrinary[14] = 0
 44 
 45     ubpTrinary[15] = 0
 46 
 47     ubpTrinary[16] = 0
 48 
 49     ubpTrinary[17] = 0
 50 
 51     ubpTrinary[18] = 0
 52 
 53     ubpTrinary[19] = 0
 54 
 55     ubpTrinary[20] = 0
 56 
 57     ubpTrinary[21] = 0
 58 
 59     ubpTrinary[22] = 0
 60 
 61     ubpTrinary[23] = 0
 62 
 63     If ulNumeric <> 0 Then
 64         For i As Byte = 23 To 0 Step -1
 65             ubpTrinary[i] = ulNumeric Mod 3
 66 
 67             ulNumeric \= 3
 68         Next i
 69     End If
 70 End Sub
 71 
 72 /'
 73  0 0 0
 74  0 1 1
 75  0 1 2
 76 '/
 77 
 78 Sub TrinaryAnd(w As UByte Pointer, x As UByte Pointer, y As UByte Pointer)
 79     For i As UByte = 0 To 23
 80         If x[i] = 0 AndAlso y[i] = 0 Then
 81             w[i] = 0
 82         ElseIf x[i] = 0 AndAlso y[i] = 1 Then
 83             w[i] = 0
 84         ElseIf x[i] = 0 AndAlso y[i] = 2 Then
 85             w[i] = 0
 86         ElseIf x[i] = 1 AndAlso y[i] = 0 Then
 87             w[i] = 0
 88         ElseIf x[i] = 1 AndAlso y[i] = 1 Then
 89             w[i] = 1
 90         ElseIf x[i] = 1 AndAlso y[i] = 2 Then
 91             w[i] = 1
 92         ElseIf x[i] = 2 AndAlso y[i] = 0 Then
 93             w[i] = 0
 94         ElseIf x[i] = 2 AndAlso y[i] = 1 Then
 95             w[i] = 1
 96         ElseIf x[i] = 2 AndAlso y[i] = 2 Then
 97             w[i] = 2
 98         End If
 99     Next i
100 End Sub
101 
102 /'
103  0 1 2
104  1 1 2
105  2 2 2
106 '/
107 
108 Sub TrinaryOr(w As UByte Pointer, x As UByte Pointer, y As UByte Pointer)
109     For j As UByte = 0 To 23
110         If x[j] = 0 AndAlso y[j] = 0 Then
111             w[j] = 0
112         ElseIf x[j] = 0 AndAlso y[j] = 1 Then
113             w[j] = 1
114         ElseIf x[j] = 0 AndAlso y[j] = 2 Then
115             w[j] = 2
116         ElseIf x[j] = 1 AndAlso y[j] = 0 Then
117             w[j] = 1
118         ElseIf x[j] = 1 AndAlso y[j] = 1 Then
119             w[j] = 1
120         ElseIf x[j] = 1 AndAlso y[j] = 2 Then
121             w[j] = 2
122         ElseIf x[j] = 2 AndAlso y[j] = 0 Then
123             w[j] = 2
124         ElseIf x[j] = 2 AndAlso y[j] = 1 Then
125             w[j] = 2
126         ElseIf x[j] = 2 AndAlso y[j] = 2 Then
127             w[j] = 2
128         End If
129     Next j
130 End Sub
131 
132 /'
133  0 0 2
134  1 1 1
135  2 2 0
136 '/
137 
138 Sub TrinaryXor(w As UByte Pointer, x As UByte Pointer, y As UByte Pointer)
139     For k As UByte = 0 To 23
140         If x[k] = 0 AndAlso y[k] = 0 Then
141             w[k] = 0
142         ElseIf x[k] = 0 AndAlso y[k] = 1 Then
143             w[k] = 0
144         ElseIf x[k] = 0 AndAlso y[k] = 2 Then
145             w[k] = 2
146         ElseIf x[k] = 1 AndAlso y[k] = 0 Then
147             w[k] = 1
148         ElseIf x[k] = 1 AndAlso y[k] = 1 Then
149             w[k] = 1
150         ElseIf x[k] = 1 AndAlso y[k] = 2 Then
151             w[k] = 1
152         ElseIf x[k] = 2 AndAlso y[k] = 0 Then
153             w[k] = 2
154         ElseIf x[k] = 2 AndAlso y[k] = 1 Then
155             w[k] = 2
156         ElseIf x[k] = 2 AndAlso y[k] = 2 Then
157             w[k] = 0
158         End If
159     Next k
160 End Sub
161 
162 /'
163  2 0 0
164  1 1 1
165  0 2 2
166 '/
167 
168 Sub TrinaryXand(w As UByte Pointer, x As UByte Pointer, y As UByte Pointer)
169     For i As UByte = 0 To 23
170         If x[i] = 0 AndAlso y[i] = 0 Then
171             w[i] = 2
172         ElseIf x[i] = 0 AndAlso y[i] = 1 Then
173             w[i] = 0
174         ElseIf x[i] = 0 AndAlso y[i] = 2 Then
175             w[i] = 0
176         Elseif x[i] = 1 AndAlso y[i] = 0 Then
177             w[i] = 1
178         Elseif x[i] = 1 AndAlso y[i] = 1 Then
179             w[i] = 1
180         Elseif x[i] = 1 AndAlso y[i] = 2 Then
181             w[i] = 1
182         Elseif x[i] = 2 AndAlso y[i] = 0 Then
183             w[i] = 0
184         Elseif x[i] = 2 AndAlso y[i] = 1 Then
185             w[i] = 2
186         Elseif x[i] = 2 AndAlso y[i] = 2 Then
187             w[i] = 2
188         End If
189     Next i
190 End Sub
191 
192 /'
193  0 1 2
194  1 2 0
195  2 0 1
196 '/
197 
198 Sub TrinaryAdd(w As UByte Pointer, x As UByte Pointer, y As UByte Pointer)
199     For j As UByte = 0 To 23
200         If x[j] = 0 AndAlso y[j] = 0 Then
201             w[j] = 0
202         ElseIf x[j] = 0 AndAlso y[j] = 1 Then
203             w[j] = 1
204         ElseIf x[j] = 0 AndAlso y[j] = 2 Then
205             w[j] = 2
206         ElseIf x[j] = 1 AndAlso y[j] = 0 Then
207             w[j] = 1
208         ElseIf x[j] = 1 AndAlso y[j] = 1 Then
209             w[j] = 2
210         ElseIf x[j] = 1 AndAlso y[j] = 2 Then
211             w[j] = 0
212         ElseIf x[j] = 2 AndAlso y[j] = 0 Then
213             w[j] = 2
214         ElseIf x[j] = 2 AndAlso y[j] = 1 Then
215             w[j] = 0
216         ElseIf x[j] = 2 AndAlso y[j] = 2 Then
217             w[j] = 1
218         End If
219     Next j
220 End Sub
221 
222 /'
223  0 0 0
224  0 1 2
225  0 2 1
226 '/
227 
228 Sub TrinaryMultiplication(w As UByte Pointer, x As UByte Pointer, y As UByte Pointer)
229     For k As UByte = 0 To 23
230         If x[k] = 0 AndAlso y[k] = 0 Then
231             w[k] = 0
232         ElseIf x[k] = 0 AndAlso y[k] = 1 Then
233             w[k] = 0
234         ElseIf x[k] = 0 AndAlso y[k] = 2 Then
235             w[k] = 0
236         ElseIf x[k] = 1 AndAlso y[k] = 0 Then
237             w[k] = 0
238         ElseIf x[k] = 1 AndAlso y[k] = 1 Then
239             w[k] = 1
240         ElseIf x[k] = 1 AndAlso y[k] = 2 Then
241             w[k] = 2
242         ElseIf x[k] = 2 AndAlso y[k] = 0 Then
243             w[k] = 0
244         ElseIf x[k] = 2 AndAlso y[k] = 1 Then
245             w[k] = 2
246         ElseIf x[k] = 2 AndAlso y[k] = 2 Then
247             w[k] = 1
248         End If
249     Next k
250 End Sub
251 
252 Sub F(w As UByte Pointer, x As UByte Pointer, y As UByte Pointer, z As UByte Pointer)
253     TrinaryAnd(w, x, y)
254 
255     TrinaryOr(w, w, z)
256 End Sub
257 
258 Sub G(w As UByte Pointer, x As UByte Pointer, y As UByte Pointer, z As UByte Pointer)
259     TrinaryAdd(w, x, z)
260 
261     TrinaryMultiplication(w, w, y)
262 End Sub
263 
264 Sub H(w As UByte Pointer, x As UByte Pointer, y As UByte Pointer, z As UByte Pointer)
265     TrinaryXor(w, x, y)
266 
267     TrinaryXand(w, w, z)
268 End Sub
269 
270 Sub I1(w As UByte Pointer, x As UByte Pointer, y As UByte Pointer, z As UByte Pointer)
271     TrinaryMultiplication(w, y, z)
272 
273     TrinaryAdd(w, w, x)
274 End Sub
275 
276 Sub Add(w As UByte Pointer, z As UByte Pointer)
277     Dim As Byte bCarry = 0
278 
279     For i As UByte = 0 To 23
280         w[i] += z[i] + bCarry
281 
282         If w[i] > 2 Then
283             w[i] -= 3
284 
285             If bCarry = 0 Then
286                 bCarry = 1
287             End If
288         Else
289             bCarry = 0
290         End If
291     Next i
292 End Sub
293 
294 Sub RotateShiftLeft(w As UByte Pointer, bBit As UByte)
295     Dim As UByte ubaSwap(23) => {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
296 
297     For i As Byte = 23 To 0 Step -1
298         Dim As UByte j
299 
300         If i >= bBit Then
301             j = i - bBit
302         Else
303             j = 24 - bBit + i
304         End If
305 
306         ubaSwap(i) = w[j]
307     Next i
308 
309     For k As UByte = 0 To 23
310         w[k] = ubaSwap(k)
311     Next k
312 End Sub
313 
314 Sub FF(a As UByte Pointer, b As UByte Pointer, c As UByte Pointer, d As UByte Pointer, e As UByte Pointer, bShift As UByte, k As UByte Pointer)
315     Dim As UByte ubaTemp(23) => {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
316 
317     F(@ubaTemp(0), b, c, d)
318 
319     Add(@ubaTemp(0), a)
320 
321     Add(@ubaTemp(0), e)
322 
323     Add(@ubaTemp(0), k)
324 
325     RotateShiftLeft(@ubaTemp(0), bShift)
326 
327     Add(@ubaTemp(0), b)
328 
329     For i As UByte = 0 To 23
330         a[i] = ubaTemp(i)
331     Next i
332 End Sub
333 
334 Sub GG(a As UByte Pointer, b As UByte Pointer, c As UByte Pointer, d As UByte Pointer, e As UByte Pointer, bShift As UByte, k As UByte Pointer)
335     Dim As UByte ubaTemp(23) => {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
336 
337     G(@ubaTemp(0), c, d, a)
338 
339     Add(@ubaTemp(0), b)
340 
341     Add(@ubaTemp(0), e)
342 
343     Add(@ubaTemp(0), k)
344 
345     RotateShiftLeft(@ubaTemp(0), bShift)
346 
347     Add(@ubaTemp(0), c)
348 
349     For j As UByte = 0 To 23
350         b[j] = ubaTemp(j)
351     Next j
352 End Sub
353 
354 Sub HH(a As UByte Pointer, b As UByte Pointer, c As UByte Pointer, d As UByte Pointer, e As UByte Pointer, bShift As UByte, k As UByte Pointer)
355     Dim As UByte ubaTemp(23) => {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
356 
357     H(@ubaTemp(0), d, a, b)
358 
359     Add(@ubaTemp(0), c)
360 
361     Add(@ubaTemp(0), e)
362 
363     Add(@ubaTemp(0), k)
364 
365     RotateShiftLeft(@ubaTemp(0), bShift)
366 
367     Add(@ubaTemp(0), d)
368 
369     For i As UByte = 0 To 23
370         c[i] = ubaTemp(i)
371     Next i
372 End Sub
373 
374 Sub II(a As UByte Pointer, b As UByte Pointer, c As UByte Pointer, d As UByte Pointer, e As UByte Pointer, bShift As UByte, k As UByte Pointer)
375     Dim As UByte ubaTemp(23) => {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
376 
377     I1(@ubaTemp(0), a, b, c)
378 
379     Add(@ubaTemp(0), d)
380 
381     Add(@ubaTemp(0), e)
382 
383     Add(@ubaTemp(0), k)
384 
385     RotateShiftLeft(@ubaTemp(0), bShift)
386 
387     Add(@ubaTemp(0), a)
388 
389     For j As UByte = 0 To 23
390         d[j] = ubaTemp(j)
391     Next j
392 End Sub
393 
394 Sub MD3Hash(a As UByte Pointer, b As UByte Pointer, c As UByte Pointer, d As UByte Pointer, e() As UByte)
395     Const bShift11 = 2, bShift12 = 3, bShift13 = 4, bShift14 = 5, bShift21 = 6, bShift22 = 7, bShift23 = 8, bShift24 = 11, bShift31 = 10, bShift32 = 13, bShift33 = 14, bShift34 = 17, bShift41 = 16, bShift42 = 19, bShift43 = 22, bShift44 = 23
396 
397     Dim As UByte k0(23) => {2, 1, 1, 1, 2, 2, 0, 1, 2, 2, 1, 1, 2, 1, 2, 2, 2, 0, 0, 0, 1, 0, 0, 0}
398 
399     Dim As UByte k1(23) => {1, 1, 2, 0, 0, 1, 0, 0, 0, 1, 0, 2, 1, 1, 0, 2, 0, 0, 1, 0, 1, 0, 0, 0}
400 
401     Dim As UByte k2(23) => {0, 0, 2, 2, 1, 2, 0, 0, 1, 1, 1, 1, 0, 2, 0, 0, 2, 1, 1, 0, 0, 0, 0, 0}
402 
403     Dim As UByte k3(23) => {1, 1, 1, 0, 1, 0, 0, 2, 1, 1, 2, 0, 2, 1, 1, 1, 0, 1, 2, 2, 0, 0, 0, 0}
404 
405     Dim As UByte k4(23) => {1, 2, 1, 1, 2, 0, 1, 2, 2, 2, 0, 2, 0, 0, 0, 2, 2, 1, 1, 0, 1, 0, 0, 0}
406 
407     Dim As UByte k5(23) => {2, 1, 0, 2, 1, 2, 1, 0, 1, 1, 1, 0, 2, 2, 1, 2, 0, 0, 0, 1, 0, 0, 0, 0}
408 
409     Dim As UByte k6(23) => {2, 0, 0, 1, 0, 0, 1, 0, 0, 1, 2, 1, 2, 2, 1, 1, 2, 0, 1, 2, 0, 0, 0, 0}
410 
411     Dim As UByte k7(23) => {2, 2, 1, 0, 0, 2, 1, 1, 2, 1, 0, 2, 0, 1, 0, 2, 2, 2, 1, 0, 1, 0, 0, 0}
412 
413     Dim As UByte k8(23) => {1, 2, 0, 0, 1, 0, 0, 1, 0, 2, 2, 1, 0, 0, 1, 0, 2, 1, 1, 1, 0, 0, 0, 0}
414 
415     Dim As UByte k9(23) => {2, 1, 1, 2, 2, 2, 1, 1, 0, 2, 2, 1, 1, 1, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0}
416 
417     Dim As UByte k10(23) => {1, 2, 2, 0, 1, 2, 0, 1, 2, 2, 2, 1, 2, 2, 0, 2, 0, 0, 2, 0, 1, 0, 0, 0}
418 
419     Dim As UByte k11(23) => {1, 1, 0, 2, 2, 0, 1, 2, 2, 2, 0, 1, 1, 2, 1, 1, 2, 2, 2, 1, 0, 0, 0, 0}
420 
421     Dim As UByte k12(23) => {2, 0, 1, 2, 1, 2, 0, 0, 1, 0, 0, 2, 2, 0, 2, 2, 2, 1, 1, 1, 0, 0, 0, 0}
422 
423     Dim As UByte k13(23) => {2, 2, 2, 0, 2 ,2, 1, 0, 1, 1, 1, 2, 1, 1, 1, 2, 2, 2, 1, 0, 1, 0, 0, 0}
424 
425     Dim As UByte k14(23) => {1, 0, 1, 1, 0, 2, 2, 2, 0, 0, 1, 1, 2, 2, 1, 2, 1, 0, 1, 2, 0, 0, 0, 0}
426 
427     Dim As UByte k15(23) => {0, 1, 2, 0, 2, 1, 1, 1, 1, 2, 0, 2, 1, 1, 0, 2, 1, 0, 0, 1, 0, 0, 0, 0}
428 
429     Dim As UByte k16(23) => {0, 0, 1, 0, 1, 1, 1, 2, 1, 2 ,0, 2, 2, 0, 2, 2, 2, 1, 1, 0, 1, 0, 0, 0}
430 
431     Dim As UByte k17(23) => {1, 2, 0, 2, 0, 0, 2, 2, 1, 1, 2, 0, 0, 1, 2, 2, 2, 0, 2, 2, 0, 0, 0, 0}
432 
433     Dim As UByte k18(23) => {0, 1, 2, 0, 0, 2, 0, 2, 0, 1, 2, 0, 2, 1, 2, 2, 2, 1, 1, 0, 0, 0, 0, 0}
434 
435     Dim As UByte k19(23) => {1, 2, 1, 1, 1, 2, 2, 2, 2, 1, 1, 0, 1, 2, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0}
436 
437     Dim As UByte k20(23) => {1, 2, 1, 0, 2, 2, 1, 0, 0, 2, 2, 1, 2, 0, 1, 1, 2, 0, 0, 0, 1, 0, 0, 0}
438 
439     Dim As UByte k21(23) => {2, 0, 0, 1, 2, 0, 2, 0, 1, 2, 1, 1, 2, 2, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0}
440 
441     Dim As UByte k22(23) => {1, 2, 0, 0, 2, 1, 1, 1, 0, 1, 2, 2, 1, 2, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0}
442 
443     Dim As UByte k23(23) => {2, 1, 1, 1, 1, 0, 1, 1, 1, 2, 2, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0}
444 
445     Dim As UByte k24(23) => {0, 1, 2, 0, 2, 2, 1, 0, 0, 2, 2, 1, 1, 2, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0}
446 
447     Dim As UByte k25(23) => {0, 1, 2, 1, 1, 2, 2, 1, 1, 0, 1, 2, 0, 2, 0, 0, 1, 1, 2, 2, 0, 0, 0, 0}
448 
449     Dim As UByte k26(23) => {2, 1, 2, 0, 1, 1, 2, 1, 2, 1, 1, 0, 1, 2, 0, 1, 2, 1, 1, 0, 1, 0, 0, 0}
450 
451     Dim As UByte k27(23) => {2, 2, 0, 1, 1, 0, 2, 1, 1, 1, 0, 1, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0}
452 
453     Dim As UByte k28(23) => {1, 1, 2, 2, 0, 0, 2, 1, 1, 2, 2, 0, 2, 2, 1, 0, 0, 1, 1, 2, 0, 0, 0, 0}
454 
455     Dim As UByte k29(23) => {2, 2, 2, 2, 0, 2, 0, 0, 1, 0, 0, 0, 2, 0, 2, 1, 2, 2, 1, 0, 1, 0, 0, 0}
456 
457     Dim As UByte k30(23) => {1, 2, 2, 0, 1, 0, 1, 1, 2, 2, 2, 0, 1, 2, 2, 0, 1, 1, 1, 1, 0, 0, 0, 0}
458 
459     Dim As UByte k31(23) => {1, 1, 2, 2, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0}
460 
461     Dim As UByte k32(23) => {1, 1, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 2, 2, 0, 2, 0, 0, 2, 0, 1, 0, 0, 0}
462 
463     Dim As UByte k33(23) => {2, 1, 1, 1, 2, 2, 1, 1, 1, 0, 2, 2, 0, 0, 1, 2, 1, 2, 2, 1, 0, 0, 0, 0}
464 
465     Dim As UByte k34(23) => {1, 0, 0, 0, 0, 2, 2, 0, 1, 0, 1, 1, 1, 1, 0, 2, 0, 2, 1, 1, 0, 0, 0, 0}
466 
467     Dim As UByte k35(23) => {1, 1, 1, 0, 2, 2, 0, 0, 0, 2, 2, 0, 2, 1, 2, 2, 2, 2, 1, 0, 1, 0, 0, 0}
468 
469     Dim As UByte k36(23) => {2, 1, 0, 0, 2, 0, 1, 1, 1, 0, 2, 2, 1, 2, 1, 0, 1, 0, 1, 2, 0, 0, 0, 0}
470 
471     Dim As UByte k37(23) => {1, 2, 0, 2, 0, 1, 0, 0, 2, 1, 1, 0, 1, 0, 2, 1, 2, 0, 0, 1, 0, 0, 0, 0}
472 
473     Dim As UByte k38(23) => {1, 2, 0, 2, 1, 2, 1, 1, 2, 0, 1, 0, 1, 1, 1, 0, 0, 2, 1, 0, 1, 0, 0, 0}
474 
475     Dim As UByte k39(23) => {0, 2, 1, 1, 0, 1, 2, 1, 2, 0, 1, 2, 0, 0, 0, 1, 2, 0, 2, 2, 0, 0, 0, 0}
476 
477     Dim As UByte k40(23) => {0, 0, 0, 0, 0, 1, 0, 2, 1, 1, 2, 2, 0, 1, 1, 2, 0, 2, 1, 0, 0, 0, 0, 0}
478 
479     Dim As UByte k41(23) => {0, 1, 1, 0, 2, 2, 0, 0, 1, 2, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0}
480 
481     Dim As UByte k42(23) => {2, 1, 1, 0, 0, 2, 0, 0, 0, 2, 1, 0, 2, 2, 2, 2, 1, 0, 0, 0, 1, 0, 0, 0}
482 
483     Dim As UByte k43(23) => {0, 2, 2, 2, 0, 1, 0, 0, 2, 1, 0, 0, 2, 2, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0}
484 
485     Dim As UByte k44(23) => {1, 2, 1, 0, 2, 1, 1, 0, 0, 0, 1, 2, 0, 0, 2, 2, 0, 1, 0, 0, 1, 0, 0, 0}
486 
487     Dim As UByte k45(23) => {0, 1, 0, 2, 2, 2, 0, 1, 1, 0, 0, 0, 1, 2, 2, 2, 2, 2, 0, 0, 1, 0, 0, 0}
488 
489     Dim As UByte k46(23) => {1, 0, 1, 2, 1, 2, 1, 1, 1, 0, 0, 2, 2, 2, 2, 0, 0, 1, 1, 0, 0, 0, 0, 0}
490 
491     Dim As UByte k47(23) => {0, 0, 1, 0, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, 2, 1, 1, 1, 2, 2, 0, 0, 0, 0}
492 
493     Dim As UByte k48(23) => {0, 2, 2, 2, 1, 0, 0, 1, 1, 2, 2, 2, 0, 1, 1, 0, 2, 1, 1, 0, 1, 0, 0, 0}
494 
495     Dim As UByte k49(23) => {2, 0, 0, 2, 0, 1, 0, 0, 0, 0, 1, 1, 2, 1, 1, 0, 2, 2, 2, 0, 0, 0, 0, 0}
496 
497     Dim As UByte k50(23) => {2, 0, 2, 1, 1, 2, 2, 2, 1, 1, 2, 1, 1, 2, 1, 2, 0, 1, 1, 2, 0, 0, 0, 0}
498 
499     Dim As UByte k51(23) => {1, 2, 1, 0, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 0, 1, 2, 2, 1, 0, 1, 0, 0, 0}
500 
501     Dim As UByte k52(23) => {2, 0, 0, 0, 0, 2, 1, 2, 1, 2, 0, 2, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0}
502 
503     Dim As UByte k53(23) => {1, 0, 2, 0, 2, 1, 2, 2, 1, 2, 2, 2, 0, 2, 0, 2, 1, 0, 0, 2, 0, 0, 0, 0}
504 
505     Dim As UByte k54(23) => {2, 1, 1, 2, 0, 0, 2, 1, 1, 2, 0, 2, 0, 2, 0, 2, 0, 0, 2, 0, 1, 0, 0, 0}
506 
507     Dim As UByte k55(23) => {0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 2, 2, 1, 0, 0, 0, 0}
508 
509     Dim As UByte k56(23) => {1, 2, 0, 1, 1, 2, 1, 1, 0, 2, 2, 2, 2, 1, 1, 1, 1, 2, 1, 1, 0, 0, 0, 0}
510 
511     Dim As UByte k57(23) => {2, 1, 1, 2, 0, 0, 1, 0, 2, 0, 1, 0, 2, 1, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0}
512 
513     Dim As UByte k58(23) => {2, 2, 1, 0, 0, 2, 2, 2, 1, 1, 2, 2, 0, 2, 1, 1, 0, 0, 1, 2, 0, 0, 0, 0}
514 
515     Dim As UByte k59(23) => {0, 1, 0, 0, 1, 1, 0, 1, 2, 1, 0, 1, 0, 2, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0}
516 
517     Dim As UByte k60(23) => {1, 1, 1, 1, 0, 1, 1, 2, 1, 0, 2, 2, 1, 1, 0, 1, 0, 2, 1, 0, 1, 0, 0, 0}
518 
519     Dim As UByte k61(23) => {2, 1, 1, 2, 0, 2, 0, 0, 1, 2, 1, 2, 0, 2, 0, 2, 1, 0, 2, 2, 0, 0, 0, 0}
520 
521     Dim As UByte k62(23) => {0, 0, 1, 2, 0, 2, 1, 1, 0, 2, 1, 1, 2, 0, 0, 2, 1, 2, 1, 0, 0, 0, 0, 0}
522 
523     Dim As UByte k63(23) => {2, 0, 2, 0, 2, 2, 1, 0, 0, 2, 0, 1, 1, 0, 1, 2, 1, 0, 1, 0, 1, 0, 0, 0}
524 
525     FF(a, b, c, d, @e( 0, 0), bShift11, @k0(0))
526 
527     FF(b, c, d, a, @e( 1, 0), bShift12, @k1(0))
528 
529     FF(c, d, a, b, @e( 2, 0), bShift13, @k2(0))
530 
531     FF(d, a, b, c, @e( 3, 0), bShift14, @k3(0))
532 
533     FF(a, b, c, d, @e( 4, 0), bShift11, @k4(0))
534 
535     FF(b, c, d, a, @e( 5, 0), bShift12, @k5(0))
536 
537     FF(c, d, a, b, @e( 6, 0), bShift13, @k6(0))
538 
539     FF(d, a, b, c, @e( 7, 0), bShift14, @k7(0))
540 
541     FF(a, b, c, d, @e( 8, 0), bShift11, @k8(0))
542 
543     FF(b, c, d, a, @e( 9, 0), bShift12, @k9(0))
544 
545     FF(c, d, a, b, @e(10, 0), bShift13, @k10(0))
546 
547     FF(d, a, b, c, @e(11, 0), bShift14, @k11(0))
548 
549     FF(a, b, c, d, @e(12, 0), bShift11, @k12(0))
550 
551     FF(b, c, d, a, @e(13, 0), bShift12, @k13(0))
552 
553     FF(c, d, a, b, @e(14, 0), bShift13, @k14(0))
554 
555     FF(d, a, b, c, @e(15, 0), bShift14, @k15(0))
556 
557     GG(a, b, c, d, @e( 1, 0), bShift21, @k16(0))
558 
559     GG(d, a, b, c, @e( 6, 0), bShift22, @k17(0))
560 
561     GG(c, d, a, b, @e(11, 0), bShift23, @k18(0))
562 
563     GG(b, c, d, a, @e( 0, 0), bShift24, @k19(0))
564 
565     GG(a, b, c, d, @e( 5, 0), bShift21, @k20(0))
566 
567     GG(d, a, b, c, @e(10, 0), bShift22, @k21(0))
568 
569     GG(c, d, a, b, @e(15, 0), bShift23, @k22(0))
570 
571     GG(b, c, d, a, @e( 4, 0), bShift24, @k23(0))
572 
573     GG(a, b, c, d, @e( 9, 0), bShift21, @k24(0))
574 
575     GG(d, a, b, c, @e(14, 0), bShift22, @k25(0))
576 
577     GG(c, d, a, b, @e( 3, 0), bShift23, @k26(0))
578 
579     GG(b, c, d, a, @e( 8, 0), bShift24, @k27(0))
580 
581     GG(a, b, c, d, @e(13, 0), bShift21, @k28(0))
582 
583     GG(d, a, b, c, @e( 2, 0), bShift22, @k29(0))
584 
585     GG(c, d, a, b, @e( 7, 0), bShift23, @k30(0))
586 
587     GG(b, c, d, a, @e(12, 0), bShift24, @k31(0))
588 
589     HH(a, b, c, d, @e( 5, 0), bShift31, @k32(0))
590 
591     HH(b, c, d, a, @e( 8, 0), bShift32, @k33(0))
592 
593     HH(c, d, a, b, @e(11, 0), bShift33, @k34(0))
594 
595     HH(d, a, b, c, @e(14, 0), bShift34, @k35(0))
596 
597     HH(a, b, c, d, @e( 1, 0), bShift31, @k36(0))
598 
599     HH(b, c, d, a, @e( 4, 0), bShift32, @k37(0))
600 
601     HH(c, d, a, b, @e( 7, 0), bShift33, @k38(0))
602 
603     HH(d, a, b, c, @e(10, 0), bShift34, @k39(0))
604 
605     HH(a, b, c, d, @e(13, 0), bShift31, @k40(0))
606 
607     HH(b, c, d, a, @e( 0, 0), bShift32, @k41(0))
608 
609     HH(c, d, a, b, @e( 3, 0), bShift33, @k42(0))
610 
611     HH(d, a, b, c, @e( 6, 0), bShift34, @k43(0))
612 
613     HH(a, b, c, d, @e( 9, 0), bShift31, @k44(0))
614 
615     HH(b, c, d, a, @e(12, 0), bShift32, @k45(0))
616 
617     HH(c, d, a, b, @e(15, 0), bShift33, @k46(0))
618 
619     HH(d, a, b, c, @e( 2, 0), bShift34, @k47(0))
620 
621     II(a, b, c, d, @e( 0, 0), bShift41, @k48(0))
622 
623     II(d, a, b, c, @e( 7, 0), bShift42, @k49(0))
624 
625     II(c, d, a, b, @e(14, 0), bShift43, @k50(0))
626 
627     II(b, c, d, a, @e( 5, 0), bShift44, @k51(0))
628 
629     II(a, b, c, d, @e(12, 0), bShift41, @k52(0))
630 
631     II(d, a, b, c, @e( 3, 0), bShift42, @k53(0))
632 
633     II(c, d, a, b, @e(10, 0), bShift43, @k54(0))
634 
635     II(b, c, d, a, @e( 1, 0), bShift44, @k55(0))
636 
637     II(a, b, c, d, @e( 8, 0), bShift41, @k56(0))
638 
639     II(d, a, b, c, @e(15, 0), bShift42, @k57(0))
640 
641     II(c, d, a, b, @e( 6, 0), bShift43, @k58(0))
642 
643     II(b, c, d, a, @e(13, 0), bShift44, @k59(0))
644 
645     II(a, b, c, d, @e( 4, 0), bShift41, @k60(0))
646 
647     II(d, a, b, c, @e(11, 0), bShift42, @k61(0))
648 
649     II(c, d, a, b, @e( 2, 0), bShift43, @k62(0))
650 
651     II(b, c, d, a, @e( 9, 0), bShift44, @k63(0))
652 End Sub
653 
654 Sub Main()
655     Dim As UByte ubCLAA = 1, k, ubaTrinary(15, 23)
656 
657     Dim As Byte l
658 
659     Dim As ULongInt ulFileSize, ulBytesLeft, i, j
660 
661     Dim As UByte Pointer ubpData
662 
663     Dim As UByte ubaPadding(63) => {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 15, 25, 35, 45, 55, 65, 75, 85, 95, 105}
664 
665     Dim As UByte ubaDigest0(23) => {1, 2, 1, 0, 0, 0, 2, 1, 2, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0}
666 
667     Dim As UByte ubaDigest1(23) => {2, 2, 1, 2, 1, 0, 0, 2, 2, 0, 2, 0, 1, 1, 0, 2, 2, 0, 2, 0, 1, 0, 0, 1}
668 
669     Dim As UByte ubaDigest2(23) => {2, 1, 2, 2, 2, 0, 1, 2, 1, 1, 2, 0, 0, 0, 1, 0, 2, 0, 2, 0, 0, 2, 1, 0}
670 
671     Dim As UByte ubaDigest3(23) => {1, 1, 2, 0, 1, 0, 0, 2, 1, 2, 0, 0, 2, 2, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0}
672 
673     Do
674         Dim As String strCLA = Command(ubCLAA)
675 
676         If Len(strCLA) = 0 Then
677             Exit Do
678         End If
679 
680         ubCLAA += 1
681     Loop
682 
683     If ubCLAA <> 2 Then
684         Usage()
685     Else
686         ulFileSize = FileLen(Command(1))
687 
688         If ulFileSize = 0 Then
689             Print "There is no data in file [" & Command(1) & "], 0 byte."
690 
691             End -1
692         End If
693 
694         ulBytesLeft = ulFileSize And 63
695 
696         If ulBytesLeft = 0 Then
697             ulBytesLeft = ulFileSize + 64
698         ElseIf ulBytesLeft < 54 Then
699             ulBytesLeft = ulFileSize - ulBytesLeft + 64
700         ElseIf ulBytesLeft > 53 Then
701             ulBytesLeft = ulFileSize - ulBytesLeft + 128
702         End If
703 
704         ubpData = New UByte[ulBytesLeft]
705 
706         Open Command(1) For Binary Access Read As #3
707 
708         Get #3, , *ubpData, ulFileSize
709 
710         Close #3
711 
712         For i = 0 To 4
713             ubpData[i + ulFileSize] = ulFileSize Shr 8 * i
714         Next i
715 
716         k = 0
717 
718         For j = ulFileSize + 5 To ulBytesLeft - 6
719             ubpData[j] = ubaPadding(k)
720 
721             k += 1
722         Next j
723 
724         For l = 4 To 0 Step -1
725             ubpData[ulBytesLeft - l - 1] = ulFileSize Shr 8 * l
726         Next l
727 
728         For i = 0 To ulBytesLeft - 1 Step 64
729             For j = 0 To 15
730                 Ternary(ubpData[i + 4 * j] + ubpData[i + 4 * j + 1] Shl 8 + ubpData[i + 4 * j + 2] Shl 16 + ubpData[i + 4 * j + 3] Shl 24, @ubaTrinary(j, 0))
731             Next j
732 
733             MD3Hash(@ubaDigest0(0), @ubaDigest1(0), @ubaDigest2(0), @ubaDigest3(0), ubaTrinary())
734         Next i
735 
736         For k = 0 To 23
737             Print ubaDigest0(k); ubaDigest1(k); ubaDigest2(k); ubaDigest3(k);
738         Next k
739 
740         Print
741 
742         Delete ubpData
743     End If
744 End Sub
745 
746 Main()

标签:End,&&,23,伍氏,哈希,MD3,byte,TrinaryArray,UByte
来源: https://www.cnblogs.com/Wu-Yao-hui/p/16366604.html