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 - Md5StringDigest.cs

Md5StringDigest.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 Md5StringDigest
  10.     {
  11.         private Md5Base md5;
  12.         private byte[] Message;
  13.  
  14.         public const int ENCODING_VALUE_UNICODE = 0;
  15.         public const int ENCODING_VALUE_ASCII = 1;
  16.         public const int ENCODING_VALUE_UTF7 = 2;
  17.         public const int ENCODING_VALUE_UTF8 = 3;
  18.         public const int ENCODING_VALUE_UTF32 = 4;
  19.  
  20.  
  21.         public Md5StringDigest(String message):this(message, 0)
  22.         {
  23.  
  24.         }
  25.  
  26.  
  27.         /// <summary>
  28.         /// Initialiser of the class Md5StringDigest
  29.         /// </summary>
  30.         /// <param name="message">The message to get the digest from</param>
  31.         /// <param name="EncodingValue">An int value representing a specific encoder for the message</param>
  32.  
  33.         public Md5StringDigest(String message, int EncodingValue)
  34.         {
  35.             switch (EncodingValue)
  36.             {
  37.                 case ENCODING_VALUE_UNICODE:
  38.                     Message = Encoding.Unicode.GetBytes(message);
  39.                     break;
  40.                 case ENCODING_VALUE_ASCII:
  41.                     Message = Encoding.ASCII.GetBytes(message);
  42.                     break;
  43.                 case ENCODING_VALUE_UTF7:
  44.                     Message = Encoding.UTF7.GetBytes(message);
  45.                     break;
  46.                 case ENCODING_VALUE_UTF8:
  47.                     Message = Encoding.UTF8.GetBytes(message);
  48.                     break;
  49.                 case ENCODING_VALUE_UTF32:
  50.                     Message = Encoding.UTF32.GetBytes(message);
  51.                     break;
  52.                 default:
  53.                     Message = Encoding.Default.GetBytes(message);
  54.                     break;
  55.             }
  56.  
  57.             padding();
  58.             ProcessMessage();
  59.         }
  60.  
  61.         private void padding()
  62.         {
  63.             // Dato LONG della lunghezza del messaggio originale in bits.
  64.             ulong MessageLength = (ulong) Message.Length * 8;
  65.  
  66.             // Divido il dato LONG della lunghezza dell'array in array di byte.
  67.             byte[] MessageLengthArray = new byte[8];
  68.             for (int i = 0, k = 0; i < 64; i += 8, k++)
  69.                 MessageLengthArray[k] = (byte)(MessageLength >> i);
  70.  
  71.             // Lunghezza messaggio in bytes
  72.             MessageLength = (ulong)Message.Length;
  73.  
  74.             // Aggiungo il primo byte (0x80)
  75.             Array.Resize(ref Message, Message.Length + 1);
  76.             Message[Message.Length - 1] = 0x80;
  77.             MessageLength++;
  78.  
  79.             //Padding congruent 54 mod 64
  80.             while (Math.Abs((uint) MessageLength - 56) % 64 != 0)
  81.                MessageLength++;
  82.             Array.Resize(ref Message, (int) MessageLength + 8);
  83.            
  84.             // Aggiungo la lunghezza del messaggio (dato LONG) al messaggio.
  85.             long h = (Message.Length - 8);
  86.             foreach (int value in MessageLengthArray)
  87.             {
  88.                 Message[h] = (byte) value;
  89.                 h++;
  90.             }
  91.  
  92.         }
  93.  
  94.         private void ProcessMessage()
  95.         {
  96.             md5 = new Md5Base();
  97.  
  98.             // Divido il messagio in WORD
  99.             uint[] Mword = new uint[Message.Length / 4];
  100.  
  101.             for (int i = 0, k = 0; i < Mword.Length; i++, k += 4)
  102.             {
  103.                 uint word = (uint)Message[k + 3] << 24 | (uint)Message[k + 2] << 16 | (uint)Message[k + 1] << 8 | (uint)Message[k];
  104.                 Mword[i] = word;
  105.             }
  106.  
  107.             // Spezzo in blocchi e lavoro su ogni blocco
  108.             long N = Message.Length / 4;
  109.             for (int i = 0; i <= N / 16 - 1; i++)
  110.             {
  111.                 // Creo il blocco
  112.                 uint[] block = new uint[16];
  113.                 for (int k = 0; k < 16; k++)
  114.                     block[k] = Mword[i * 16 + k];
  115.                 md5.ProcessBlock(block);
  116.  
  117.             }
  118.  
  119.          }
  120.  
  121.         public String getDigest()
  122.         {
  123.             return md5.returnDigest();
  124.         }
  125.  
  126.     }
  127. }