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

Md5FileDigest.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.IO;
  6.  
  7. namespace Md5_Calculator
  8. {
  9.     class Md5FileDigest
  10.     {
  11.         String path;
  12.         long FileLength;
  13.        
  14.         // Inizializzo il processore MD5
  15.         Md5Base md5;
  16.  
  17.         public Md5FileDigest(String FilePath)
  18.         {
  19.             path = FilePath;
  20.             md5 = new Md5Base();
  21.             processFile();
  22.         }
  23.  
  24.         private uint[] ByteToWordBlock(byte[] block)
  25.         {
  26.             uint[] wordBlock = new uint[16];
  27.             for (int i = 0, k = 0; i < 64; i += 4, k++)
  28.                 wordBlock[k] = (uint)block[i + 3] << 24 | (uint)block[i + 2] << 16 | (uint)block[i + 1] << 8 | (uint)block[i];
  29.             return wordBlock;
  30.         }
  31.  
  32.         public void processFile()
  33.         {
  34.             FileStream file = new FileStream(path, System.IO.FileMode.Open, FileAccess.Read);
  35.             FileLength = file.Length * 8;
  36.  
  37.             long TempFileLength = file.Length;
  38.  
  39.             // Creo l'array di byte che contiene la lunghezza originale
  40.             byte[] FileLengthArray = new byte[8];
  41.             for (int i = 0, k = 0; i < 64; i += 8, k++)
  42.                 FileLengthArray[k] = (byte) (FileLength >> i);
  43.  
  44.             // Leggo blocchi di 64 byte alla volta (512 bit). Nell'ultimo blocco processo il padding
  45.             long fromRead = 0;
  46.             int NumberOfByteRead = 0;
  47.             byte[] block = new byte[64];
  48.             uint[] wordBlock = new uint[16];
  49.  
  50.             while (TempFileLength >= 0)
  51.             {
  52.                 Array.Clear(block, 0, 64);
  53.                 NumberOfByteRead = file.Read(block, 0, 64);
  54.  
  55.                 fromRead += NumberOfByteRead;
  56.                 TempFileLength -= 64;
  57.  
  58.                 // Creo il blocco di word
  59.                 uint[] word = ByteToWordBlock(block);
  60.                
  61.                 // Se questo non è stato l'ultimo blocco
  62.                 if (NumberOfByteRead == 64 && TempFileLength > 0)
  63.                     md5.ProcessBlock(word);
  64.  
  65.                 if (TempFileLength <= 0)
  66.                     break;
  67.             }
  68.  
  69.             blockPadding(ref block, ref FileLengthArray, NumberOfByteRead);
  70.             file.Close();
  71.         }
  72.  
  73.         private void blockPadding(ref byte[] block, ref byte[] FileLengthArray, int NumberOfByteLasted)
  74.         {
  75.             /*
  76.              *    -  Se il numero di byte dell'ultimo blocco lascia più di 5 byte
  77.              *       disponibili processo solo quest'ultimo blocco: ( 1 byte 0x80 e 4 byte per la lunghezza del file iniziale)
  78.              *    -  Altrimenti, utilizzo un altro blocco di 64 byte;
  79.              */
  80.  
  81.             int k = 56;
  82.             if (NumberOfByteLasted <= 55)
  83.             {
  84.                 block[NumberOfByteLasted] = 0x80;
  85.                 foreach (byte val in FileLengthArray)
  86.                 {
  87.                     block[k] = val;
  88.                     k++;
  89.                 }
  90.                 md5.ProcessBlock(ByteToWordBlock(block));
  91.             }
  92.             else
  93.             {
  94.                 byte[] newBlock = new byte[64];
  95.                 foreach(byte val in FileLengthArray)
  96.                 {
  97.                     newBlock[k] = val;
  98.                     k++;
  99.                 }
  100.  
  101.                 if (NumberOfByteLasted != 64)
  102.                     block[NumberOfByteLasted] = 0x80;
  103.                 else
  104.                     newBlock[0] = 0x80;
  105.                
  106.                 // Finito il secondo caso di padding, processo i due blocchi;
  107.                 md5.ProcessBlock(ByteToWordBlock(block));
  108.                 md5.ProcessBlock(ByteToWordBlock(newBlock));
  109.             }
  110.  
  111.         }
  112.  
  113.         public String getDigest()
  114.         {
  115.             return md5.returnDigest();
  116.         }
  117.     }
  118. }