Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Md5 Calculator - Md5Base.cs

Md5Base.cs

Caricato da: A_butta
Scarica il programma completo

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Forms;
  6.  
  7. namespace Md5_Calculator
  8. {
  9.     class Md5Base
  10.     {
  11.         private static int blockCount = 0;
  12.  
  13.         uint WordA = 0x67452301,
  14.              WordB = 0xefcdab89,
  15.              WordC = 0x98badcfe,
  16.              WordD = 0x10325476;
  17.  
  18.         uint[] Table = new uint[64];
  19.         uint[] X = new uint[16];
  20.  
  21.         uint F(uint x, uint y, uint z) { return ((x & y) | ((~x) & z));}
  22.         uint G(uint x, uint y, uint z) { return (x & z) | ((~z) & y); }
  23.         uint H(uint x, uint y, uint z) { return x ^ y ^ z; }
  24.         uint I(uint x, uint y, uint z) { return (y ^ ((~z) | x)); }
  25.         uint RotateLeft(uint x, int s) { return (x << s) | (x >> (32 - s)); }
  26.  
  27.  
  28.         uint Round1(uint a, uint b, uint c, uint d, byte k, byte s, byte i)
  29.         {            
  30.             return b + RotateLeft((a + F(b, c, d) + X[k] + Table[i - 1]), s);
  31.         }
  32.         uint Round2(uint a, uint b, uint c, uint d, byte k, byte s, byte i)
  33.         {
  34.             return b + RotateLeft((a + G(b, c, d) + X[k] + Table[i - 1]), s);
  35.         }
  36.         uint Round3(uint a, uint b, uint c, uint d, byte k, byte s, byte i)
  37.         {
  38.             return b + RotateLeft((a + H(b, c, d) + X[k] + Table[i - 1]), s);
  39.         }
  40.         uint Round4(uint a, uint b, uint c, uint d, byte k, byte s, byte i)
  41.         {
  42.             return b + RotateLeft((a + I(b, c, d) + X[k] + Table[i - 1]), s);
  43.         }
  44.  
  45.         public void ProcessBlock(uint[] block)
  46.         {
  47.             Array.Copy(block, X, (long) 16);
  48.  
  49.             uint A = WordA;
  50.             uint B = WordB;
  51.             uint C = WordC;
  52.             uint D = WordD;
  53.  
  54.             #region Round1
  55.             A = Round1(A, B, C, D, 0, 7, 1);
  56.             D = Round1(D, A, B, C, 1, 12, 2);
  57.             C = Round1(C, D, A, B, 2, 17, 3);
  58.             B = Round1(B, C, D, A, 3, 22, 4);
  59.  
  60.  
  61.             A = Round1(A, B, C, D, 4, 7, 5);
  62.             D = Round1(D, A, B, C, 5, 12, 6);
  63.             C = Round1(C, D, A, B, 6, 17, 7);
  64.             B = Round1(B, C, D, A, 7, 22, 8);
  65.  
  66.             A = Round1(A, B, C, D, 8, 7, 9);
  67.             D = Round1(D, A, B, C, 9, 12, 10);
  68.             C = Round1(C, D, A, B, 10, 17, 11);
  69.             B = Round1(B, C, D, A, 11, 22, 12);
  70.  
  71.             A = Round1(A, B, C, D, 12, 7, 13);
  72.             D = Round1(D, A, B, C, 13, 12, 14);
  73.             C = Round1(C, D, A, B, 14, 17, 15);
  74.             B = Round1(B, C, D, A, 15, 22, 16);
  75.             #endregion
  76.  
  77.             #region Round2
  78.             A = Round2(A, B, C, D, 1, 5, 17);
  79.             D = Round2(D, A, B, C, 6, 9, 18);
  80.             C = Round2(C, D, A, B, 11, 14, 19);
  81.             B = Round2(B, C, D, A, 0, 20, 20);
  82.  
  83.             A = Round2(A, B, C, D, 5, 5, 21);
  84.             D = Round2(D, A, B, C, 10, 9, 22);
  85.             C = Round2(C, D, A, B, 15, 14, 23);
  86.             B = Round2(B, C, D, A, 4, 20, 24);
  87.  
  88.             A = Round2(A, B, C, D, 9, 5, 25);
  89.             D = Round2(D, A, B, C, 14, 9, 26);
  90.             C = Round2(C, D, A, B, 3, 14, 27);
  91.             B = Round2(B, C, D, A, 8, 20, 28);
  92.  
  93.             A = Round2(A, B, C, D, 13, 5, 29);
  94.             D = Round2(D, A, B, C, 2, 9, 30);
  95.             C = Round2(C, D, A, B, 7, 14, 31);
  96.             B = Round2(B, C, D, A, 12, 20, 32);
  97. #endregion
  98.  
  99.             #region Round3
  100.             A = Round3(A, B, C, D, 5, 4, 33);
  101.             D = Round3(D, A, B, C, 8, 11, 34);
  102.             C = Round3(C, D, A, B, 11, 16, 35);
  103.             B = Round3(B, C, D, A, 14, 23, 36);
  104.  
  105.             A = Round3(A, B, C, D, 1, 4, 37);
  106.             D = Round3(D, A, B, C, 4, 11, 38);
  107.             C = Round3(C, D, A, B, 7, 16, 39);
  108.             B = Round3(B, C, D, A, 10, 23, 40);
  109.  
  110.             A = Round3(A, B, C, D, 13, 4, 41);
  111.             D = Round3(D, A, B, C, 0, 11, 42);
  112.             C = Round3(C, D, A, B, 3, 16, 43);
  113.             B = Round3(B, C, D, A, 6, 23, 44);
  114.  
  115.             A = Round3(A, B, C, D, 9, 4, 45);
  116.             D = Round3(D, A, B, C, 12, 11, 46);
  117.             C = Round3(C, D, A, B, 15, 16, 47);
  118.             B = Round3(B, C, D, A, 2, 23, 48);
  119.             #endregion
  120.  
  121.             #region Round4
  122.             A = Round4(A, B, C, D, 0, 6, 49);
  123.             D = Round4(D, A, B, C, 7, 10, 50);
  124.             C = Round4(C, D, A, B, 14, 15, 51);
  125.             B = Round4(B, C, D, A, 5, 21, 52);
  126.  
  127.             A = Round4(A, B, C, D, 12, 6, 53);
  128.             D = Round4(D, A, B, C, 3, 10, 54);
  129.             C = Round4(C, D, A, B, 10, 15, 55);
  130.             B = Round4(B, C, D, A, 1, 21, 56);
  131.  
  132.             A = Round4(A, B, C, D, 8, 6, 57);
  133.             D = Round4(D, A, B, C, 15, 10, 58);
  134.             C = Round4(C, D, A, B, 6, 15, 59);
  135.             B = Round4(B, C, D, A, 13, 21, 60);
  136.  
  137.             A = Round4(A, B, C, D, 4, 6, 61);
  138.             D = Round4(D, A, B, C, 11, 10, 62);
  139.             C = Round4(C, D, A, B, 2, 15, 63);
  140.             B = Round4(B, C, D, A, 9, 21, 64);
  141.             #endregion
  142.             WordA += A;
  143.             WordB += B;
  144.             WordC += C;
  145.             WordD += D;
  146.  
  147.             blockCount++;
  148.         }
  149.  
  150.         public Md5Base()
  151.         {
  152.             // Costruzione Tabella
  153.             for (int i = 1; i <= 64; i++)
  154.                 Table[i - 1] = (uint)(0x100000000 * (double)Math.Abs(Math.Sin((double)i)));
  155.  
  156.         }
  157.  
  158.         public String returnDigest()
  159.         {
  160.             //Process String Rappresentation
  161.             string wA = null, wB = null, wC = null, wD = null;
  162.             for (int j = 0; j < 32; j += 8)
  163.             {
  164.                 wA += ((byte)(WordA >> j)).ToString("X2");
  165.                 wB += ((byte)(WordB >> j)).ToString("X2");
  166.                 wC += ((byte)(WordC >> j)).ToString("X2");
  167.                 wD += ((byte)(WordD >> j)).ToString("X2");
  168.             }
  169.  
  170.             return wA + wB + wC + wD;
  171.         }
  172.     }
  173. }