यह सभी फ़ाइल (सापेक्ष) पथ और सामग्री को हैश करता है, और सही ढंग से फ़ाइल ऑर्डरिंग को संभालता है।
और यह 4 एमबी निर्देशिका के लिए 30 एमएम की तरह त्वरित है।
using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Linq;
...
public static string CreateMd5ForFolder(string path)
{
// assuming you want to include nested folders
var files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)
.OrderBy(p => p).ToList();
MD5 md5 = MD5.Create();
for(int i = 0; i < files.Count; i++)
{
string file = files[i];
// hash path
string relativePath = file.Substring(path.Length + 1);
byte[] pathBytes = Encoding.UTF8.GetBytes(relativePath.ToLower());
md5.TransformBlock(pathBytes, 0, pathBytes.Length, pathBytes, 0);
// hash contents
byte[] contentBytes = File.ReadAllBytes(file);
if (i == files.Count - 1)
md5.TransformFinalBlock(contentBytes, 0, contentBytes.Length);
else
md5.TransformBlock(contentBytes, 0, contentBytes.Length, contentBytes, 0);
}
return BitConverter.ToString(md5.Hash).Replace("-", "").ToLower();
}
आपकी प्रतिक्रिया के लिए धन्यवाद। स्ट्रिंग वास्तव में बड़ी हो सकती है, इसलिए मुझे इसे टुकड़ों पर विभाजित करने की आवश्यकता होगी, बस सोचें कि इसे सही तरीके से कैसे किया जाए। –
मुझे याद है कि सी # हैशर्स के पास उन्हें टुकड़ों के साथ खिलाने के लिए एक समारोह था, और अंत में आप अंतिम हैश प्राप्त करने के लिए कह सकते हैं, यह सुनिश्चित नहीं कर सकते कि ये कार्य/कक्षाएं क्या थीं। उनके साथ आप मेमोरी में अपनी पसंद के तरीके को सॉर्ट कर सकते हैं, फिर लूप फाइलें और लोड सौ कुछ सौ केबी में लोड कर सकते हैं और इसे हैशर को खिला सकते हैं, इस तरह आपको ज्यादा मेमोरी की आवश्यकता नहीं है लेकिन फिर भी, हैशिंग के लिए कुछ समय लगेगा, जो कुछ है जिसे आप छुटकारा नहीं पा सकते हैं। रचनात्मक सोच के लिए – aularon