Guest User

Untitled

a guest
Jun 25th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. ABCDEFGHIJKLMNOP
  2.  
  3. ABCDEF11GHIJKLMN
  4.  
  5. public static string GenerateHash(string filePathAndName)
  6. {
  7. string hashText = "";
  8. string hexValue = "";
  9.  
  10. byte[] fileData = File.ReadAllBytes(filePathAndName);
  11. byte[] hashData = SHA1.Create().ComputeHash(fileData); // SHA1 or MD5
  12.  
  13. foreach (byte b in hashData)
  14. {
  15. hexValue = b.ToString("X").ToLower(); // Lowercase for compatibility on case-sensitive systems
  16. hashText += (hexValue.Length == 1 ? "0" : "") + hexValue;
  17. }
  18.  
  19. return hashText;
  20. }
  21.  
  22. /// <summary>
  23. /// Methode, die einen Binärvergleich von 2 Dateien macht und
  24. /// das Vergleichsergebnis zurückliefert.
  25. /// </summary>
  26. /// <param name="p_FileA">Voll qualifizierte Pfadangabe zur ersten Datei.</param>
  27. /// <param name="p_FileB">Voll qualifizierte Pfadangabe zur zweiten Datei.</param>
  28. /// <returns>True, wenn die Dateien binär gleich sind, andernfalls False.</returns>
  29. private static bool FileDiffer(string p_FileA, string p_FileB)
  30. {
  31. bool retVal = true;
  32. FileInfo infoA = null;
  33. FileInfo infoB = null;
  34. byte[] bufferA = new byte[128];
  35. byte[] bufferB = new byte[128];
  36. int bufferRead = 0;
  37.  
  38. // Die Dateien überprüfen
  39. if (!File.Exists(p_FileA))
  40. {
  41. throw new ArgumentException(String.Format("Die Datei '{0}' konnte nicht gefunden werden", p_FileA), "p_FileA");
  42. }
  43. if (!File.Exists(p_FileB))
  44. {
  45. throw new ArgumentException(String.Format("Die Datei '{0}' konnte nicht gefunden werden", p_FileB), "p_FileB");
  46. }
  47.  
  48. // Dateiinfo wegen der Dateigröße erzeugen
  49. infoA = new FileInfo(p_FileA);
  50. infoB = new FileInfo(p_FileB);
  51.  
  52. // Wenn die Dateigröße gleich ist, dann einen Vergleich anstossen
  53. if (infoA.Length == infoB.Length)
  54. {
  55. // Binärvergleich
  56. using (BinaryReader readerA = new BinaryReader(File.OpenRead(p_FileA)))
  57. {
  58. using (BinaryReader readerB = new BinaryReader(File.OpenRead(p_FileB)))
  59. {
  60. // Dateistream blockweise über Puffer einlesen
  61. while ((bufferRead = readerA.Read(bufferA, 0, bufferA.Length)) > 0)
  62. {
  63. // Dateigrößen sind gleich, deshalb kann hier
  64. // ungeprüft auch von der 2. Datei eingelesen werden
  65. readerB.Read(bufferB, 0, bufferB.Length);
  66.  
  67. // Bytevergleich innerhalb des Puffers
  68. for (int i = 0; i < Math.Min(bufferA.Length, bufferRead); i++)
  69. {
  70. if (bufferA[i] != bufferB[i])
  71. {
  72. retVal = false;
  73. break;
  74. }
  75. }
  76.  
  77. // Wenn Vergleich bereits fehlgeschlagen, dann hier schon abbruch
  78. if (!retVal)
  79. {
  80. break;
  81. }
  82. }
  83. }
  84. }
  85. }
  86. else
  87. {
  88. // Die Dateigröße ist schon unterschiedlich
  89. retVal = false;
  90. }
  91.  
  92. return retVal;
  93. }
Add Comment
Please, Sign In to add comment