Advertisement
Guest User

Untitled

a guest
Apr 7th, 2013
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.59 KB | None | 0 0
  1. ###########md5.cs
  2.  
  3. using System;
  4.  
  5.  
  6. namespace MD5
  7. {
  8.     public class MD5
  9.     {
  10.  
  11.         protected readonly static uint[] T = new uint[64]
  12.             {   0xd76aa478,0xe8c7b756,0x242070db,0xc1bdceee,
  13.                 0xf57c0faf,0x4787c62a,0xa8304613,0xfd469501,
  14.                 0x698098d8,0x8b44f7af,0xffff5bb1,0x895cd7be,
  15.                 0x6b901122,0xfd987193,0xa679438e,0x49b40821,
  16.                 0xf61e2562,0xc040b340,0x265e5a51,0xe9b6c7aa,
  17.                 0xd62f105d,0x2441453,0xd8a1e681,0xe7d3fbc8,
  18.                 0x21e1cde6,0xc33707d6,0xf4d50d87,0x455a14ed,
  19.                 0xa9e3e905,0xfcefa3f8,0x676f02d9,0x8d2a4c8a,
  20.                 0xfffa3942,0x8771f681,0x6d9d6122,0xfde5380c,
  21.                 0xa4beea44,0x4bdecfa9,0xf6bb4b60,0xbebfbc70,
  22.                 0x289b7ec6,0xeaa127fa,0xd4ef3085,0x4881d05,
  23.                 0xd9d4d039,0xe6db99e5,0x1fa27cf8,0xc4ac5665,
  24.                 0xf4292244,0x432aff97,0xab9423a7,0xfc93a039,
  25.                 0x655b59c3,0x8f0ccc92,0xffeff47d,0x85845dd1,
  26.                 0x6fa87e4f,0xfe2ce6e0,0xa3014314,0x4e0811a1,
  27.                 0xf7537e82,0xbd3af235,0x2ad7d2bb,0xeb86d391};
  28.  
  29.         protected uint[] X = new uint[16];
  30.         protected Digest dgFingerPrint;
  31.         protected byte[] m_byteInput;
  32.  
  33.  
  34.  
  35.  
  36.  
  37.         public delegate void ValueChanging(object sender, MD5ChangingEventArgs Changing);
  38.         public delegate void ValueChanged(object sender, MD5ChangedEventArgs Changed);
  39.  
  40.  
  41.         public event ValueChanging OnValueChanging;
  42.         public event ValueChanged OnValueChanged;
  43.  
  44.  
  45.  
  46.         public string Value
  47.         {
  48.             get
  49.             {
  50.                 string st;
  51.                 char[] tempCharArray = new Char[m_byteInput.Length];
  52.  
  53.                 for (int i = 0; i < m_byteInput.Length; i++)
  54.                     tempCharArray[i] = (char)m_byteInput[i];
  55.  
  56.                 st = new String(tempCharArray);
  57.                 return st;
  58.             }
  59.             set
  60.             {
  61.                 if (this.OnValueChanging != null)
  62.                     this.OnValueChanging(this, new MD5ChangingEventArgs(value));
  63.  
  64.  
  65.                 m_byteInput = new byte[value.Length];
  66.                 for (int i = 0; i < value.Length; i++)
  67.                     m_byteInput[i] = (byte)value[i];
  68.                 dgFingerPrint = CalculateMD5Value();
  69.  
  70.                 if (this.OnValueChanged != null)
  71.                     this.OnValueChanged(this, new MD5ChangedEventArgs(value, dgFingerPrint.ToString()));
  72.  
  73.             }
  74.         }
  75.  
  76.         public byte[] ValueAsByte
  77.         {
  78.             get
  79.             {
  80.                 byte[] bt = new byte[m_byteInput.Length];
  81.                 for (int i = 0; i < m_byteInput.Length; i++)
  82.                     bt[i] = m_byteInput[i];
  83.                 return bt;
  84.             }
  85.             set
  86.             {
  87.                 if (this.OnValueChanging != null)
  88.                     this.OnValueChanging(this, new MD5ChangingEventArgs(value));
  89.  
  90.                 m_byteInput = new byte[value.Length];
  91.                 for (int i = 0; i < value.Length; i++)
  92.                     m_byteInput[i] = value[i];
  93.                 dgFingerPrint = CalculateMD5Value();
  94.  
  95.                 if (this.OnValueChanged != null)
  96.                     this.OnValueChanged(this, new MD5ChangedEventArgs(value, dgFingerPrint.ToString()));
  97.             }
  98.         }
  99.  
  100.         public string FingerPrint
  101.         {
  102.             get
  103.             {
  104.                 return dgFingerPrint.ToString();
  105.             }
  106.         }
  107.  
  108.         public MD5()
  109.         {
  110.             Value = "";
  111.         }
  112.  
  113.         protected Digest CalculateMD5Value()
  114.         {
  115.             byte[] bMsg;   
  116.             uint N;        
  117.             Digest dg = new Digest();          
  118.  
  119.             bMsg = CreatePaddedBuffer();
  120.  
  121.             N = (uint)(bMsg.Length * 8) / 32;      
  122.  
  123.             for (uint i = 0; i < N / 16; i++)
  124.             {
  125.                 CopyBlock(bMsg, i);
  126.                 PerformTransformation(ref dg.A, ref dg.B, ref dg.C, ref dg.D);
  127.             }
  128.             return dg;
  129.         }
  130.  
  131.  
  132.         protected void TransF(ref uint a, uint b, uint c, uint d, uint k, ushort s, uint i)
  133.         {
  134.             a = b + MD5Helper.RotateLeft((a + ((b & c) | (~(b) & d)) + X[k] + T[i - 1]), s);
  135.         }
  136.  
  137.         protected void TransG(ref uint a, uint b, uint c, uint d, uint k, ushort s, uint i)
  138.         {
  139.             a = b + MD5Helper.RotateLeft((a + ((b & d) | (c & ~d)) + X[k] + T[i - 1]), s);
  140.         }
  141.  
  142.         protected void TransH(ref uint a, uint b, uint c, uint d, uint k, ushort s, uint i)
  143.         {
  144.             a = b + MD5Helper.RotateLeft((a + (b ^ c ^ d) + X[k] + T[i - 1]), s);
  145.         }
  146.  
  147.         protected void TransI(ref uint a, uint b, uint c, uint d, uint k, ushort s, uint i)
  148.         {
  149.             a = b + MD5Helper.RotateLeft((a + (c ^ (b | ~d)) + X[k] + T[i - 1]), s);
  150.         }
  151.  
  152.         protected void PerformTransformation(ref uint A, ref uint B, ref uint C, ref uint D)
  153.         {
  154.  
  155.             uint AA, BB, CC, DD;
  156.  
  157.             AA = A;
  158.             BB = B;
  159.             CC = C;
  160.             DD = D;
  161.  
  162.             TransF(ref A, B, C, D, 0, 7, 1); TransF(ref D, A, B, C, 1, 12, 2); TransF(ref C, D, A, B, 2, 17, 3); TransF(ref B, C, D, A, 3, 22, 4);
  163.             TransF(ref A, B, C, D, 4, 7, 5); TransF(ref D, A, B, C, 5, 12, 6); TransF(ref C, D, A, B, 6, 17, 7); TransF(ref B, C, D, A, 7, 22, 8);
  164.             TransF(ref A, B, C, D, 8, 7, 9); TransF(ref D, A, B, C, 9, 12, 10); TransF(ref C, D, A, B, 10, 17, 11); TransF(ref B, C, D, A, 11, 22, 12);
  165.             TransF(ref A, B, C, D, 12, 7, 13); TransF(ref D, A, B, C, 13, 12, 14); TransF(ref C, D, A, B, 14, 17, 15); TransF(ref B, C, D, A, 15, 22, 16);
  166.             TransG(ref A, B, C, D, 1, 5, 17); TransG(ref D, A, B, C, 6, 9, 18); TransG(ref C, D, A, B, 11, 14, 19); TransG(ref B, C, D, A, 0, 20, 20);
  167.             TransG(ref A, B, C, D, 5, 5, 21); TransG(ref D, A, B, C, 10, 9, 22); TransG(ref C, D, A, B, 15, 14, 23); TransG(ref B, C, D, A, 4, 20, 24);
  168.             TransG(ref A, B, C, D, 9, 5, 25); TransG(ref D, A, B, C, 14, 9, 26); TransG(ref C, D, A, B, 3, 14, 27); TransG(ref B, C, D, A, 8, 20, 28);
  169.             TransG(ref A, B, C, D, 13, 5, 29); TransG(ref D, A, B, C, 2, 9, 30); TransG(ref C, D, A, B, 7, 14, 31); TransG(ref B, C, D, A, 12, 20, 32);
  170.             TransH(ref A, B, C, D, 5, 4, 33); TransH(ref D, A, B, C, 8, 11, 34); TransH(ref C, D, A, B, 11, 14, 35); TransH(ref B, C, D, A, 14, 23, 36);
  171.             TransH(ref A, B, C, D, 1, 4, 37); TransH(ref D, A, B, C, 4, 11, 38); TransH(ref C, D, A, B, 7, 16, 39); TransH(ref B, C, D, A, 10, 23, 40);
  172.             TransH(ref A, B, C, D, 13, 4, 41); TransH(ref D, A, B, C, 0, 11, 42); TransH(ref C, D, A, B, 3, 16, 43); TransH(ref B, C, D, A, 6, 23, 44);
  173.             TransH(ref A, B, C, D, 9, 4, 45); TransH(ref D, A, B, C, 12, 11, 46); TransH(ref C, D, A, B, 15, 16, 47); TransH(ref B, C, D, A, 2, 23, 48);
  174.             TransI(ref A, B, C, D, 0, 6, 49); TransI(ref D, A, B, C, 7, 10, 50); TransI(ref C, D, A, B, 14, 15, 51); TransI(ref B, C, D, A, 5, 21, 52);
  175.             TransI(ref A, B, C, D, 12, 6, 53); TransI(ref D, A, B, C, 3, 10, 54); TransI(ref C, D, A, B, 10, 15, 55); TransI(ref B, C, D, A, 1, 21, 56);
  176.             TransI(ref A, B, C, D, 8, 6, 57); TransI(ref D, A, B, C, 15, 10, 58); TransI(ref C, D, A, B, 6, 15, 59); TransI(ref B, C, D, A, 13, 21, 60);
  177.             TransI(ref A, B, C, D, 4, 6, 61); TransI(ref D, A, B, C, 11, 10, 62); TransI(ref C, D, A, B, 2, 15, 63); TransI(ref B, C, D, A, 9, 21, 64);
  178.             TransI(ref B, C, D, A, 9, 21, 64);
  179.  
  180.  
  181.             A = A + AA;
  182.             B = B + BB;
  183.             C = C + CC;
  184.             D = D + DD;
  185.  
  186.  
  187.         }
  188.  
  189.  
  190.         protected byte[] CreatePaddedBuffer()
  191.         {
  192.             uint pad;      
  193.             byte[] bMsg;   
  194.             ulong sizeMsg;     
  195.             uint sizeMsgBuff;  
  196.             int temp = (448 - ((m_byteInput.Length * 8) % 512));
  197.  
  198.  
  199.             pad = (uint)((temp + 512) % 512);  
  200.             if (pad == 0)              
  201.                 pad = 512;         
  202.  
  203.             sizeMsgBuff = (uint)((m_byteInput.Length) + (pad / 8) + 8);
  204.             sizeMsg = (ulong)m_byteInput.Length * 8;
  205.             bMsg = new byte[sizeMsgBuff];  
  206.            
  207.  
  208.  
  209.  
  210.  
  211.            
  212.             for (int i = 0; i < m_byteInput.Length; i++)
  213.                 bMsg[i] = m_byteInput[i];
  214.             bMsg[m_byteInput.Length] |= 0x80;      
  215.             //wrting the size value
  216.             for (int i = 8; i > 0; i--)
  217.                 bMsg[sizeMsgBuff - i] = (byte)(sizeMsg >> ((8 - i) * 8) & 0x00000000000000ff);
  218.  
  219.             return bMsg;
  220.         }
  221.  
  222.    
  223.         protected void CopyBlock(byte[] bMsg, uint block)
  224.         {
  225.  
  226.             block = block << 6;
  227.             for (uint j = 0; j < 61; j += 4)
  228.             {
  229.                 X[j >> 2] = (((uint)bMsg[block + (j + 3)]) << 24) |
  230.                         (((uint)bMsg[block + (j + 2)]) << 16) |
  231.                         (((uint)bMsg[block + (j + 1)]) << 8) |
  232.                         (((uint)bMsg[block + (j)]));
  233.  
  234.             }
  235.         }
  236.     }
  237. }
  238.  
  239. ##############helper.cs
  240.  
  241. using System;
  242.  
  243. namespace MD5
  244. {
  245.     public enum MD5InitializerConstant : uint
  246.     {
  247.         A = 0x67452301,
  248.         B = 0xEFCDAB89,
  249.         C = 0x98badcfe,
  250.         D = 0X10325476
  251.     }
  252.  
  253.     sealed public class Digest
  254.     {
  255.         public uint A;
  256.         public uint B;
  257.         public uint C;
  258.         public uint D;
  259.  
  260.         public Digest()
  261.         {
  262.  
  263.  
  264.             A = (uint)MD5InitializerConstant.A;
  265.             B = (uint)MD5InitializerConstant.B;
  266.             C = (uint)MD5InitializerConstant.C;
  267.             D = (uint)MD5InitializerConstant.D;
  268.         }
  269.         public override string ToString()
  270.         {
  271.             string st;
  272.             st = MD5Helper.ReverseByte(A).ToString("X8") +
  273.                 MD5Helper.ReverseByte(B).ToString("X8") +
  274.                 MD5Helper.ReverseByte(C).ToString("X8") +
  275.                 MD5Helper.ReverseByte(D).ToString("X8");
  276.             return st;
  277.  
  278.         }
  279.  
  280.     }
  281.  
  282.  
  283.     sealed public class MD5Helper
  284.     {
  285.  
  286.         private MD5Helper() { }
  287.  
  288.        
  289.         public static uint RotateLeft(uint uiNumber, ushort shift)
  290.         {
  291.             return ((uiNumber >> 32 - shift) | (uiNumber << shift));
  292.         }
  293.  
  294.         public static uint ReverseByte(uint uiNumber)
  295.         {
  296.             return (((uiNumber & 0x000000ff) << 24) |
  297.                         (uiNumber >> 24) |
  298.                     ((uiNumber & 0x00ff0000) >> 8) |
  299.                     ((uiNumber & 0x0000ff00) << 8));
  300.         }
  301.     }
  302.  
  303.  
  304.     public class MD5ChangingEventArgs : EventArgs
  305.     {
  306.         public readonly byte[] NewData;
  307.  
  308.         public MD5ChangingEventArgs(byte[] data)
  309.         {
  310.             byte[] NewData = new byte[data.Length];
  311.             for (int i = 0; i < data.Length; i++)
  312.                 NewData[i] = data[i];
  313.         }
  314.  
  315.         public MD5ChangingEventArgs(string data)
  316.         {
  317.             byte[] NewData = new byte[data.Length];
  318.             for (int i = 0; i < data.Length; i++)
  319.                 NewData[i] = (byte)data[i];
  320.         }
  321.  
  322.     }
  323.  
  324.     public class MD5ChangedEventArgs : EventArgs
  325.     {
  326.         public readonly byte[] NewData;
  327.         public readonly string FingerPrint;
  328.  
  329.         public MD5ChangedEventArgs(byte[] data, string HashedValue)
  330.         {
  331.             byte[] NewData = new byte[data.Length];
  332.             for (int i = 0; i < data.Length; i++)
  333.                 NewData[i] = data[i];
  334.             FingerPrint = HashedValue;
  335.         }
  336.  
  337.         public MD5ChangedEventArgs(string data, string HashedValue)
  338.         {
  339.             byte[] NewData = new byte[data.Length];
  340.             for (int i = 0; i < data.Length; i++)
  341.                 NewData[i] = (byte)data[i];
  342.  
  343.             FingerPrint = HashedValue;
  344.         }
  345.  
  346.     }
  347.  
  348.  
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement