I need to create a checksum for an entire USB Drive. I want to be able to get a checksum for the entire USB Drive when I put files on it and then be able to get another checksum later to check that nothing has changed (viruses, updates to files, etc).
Right now, I'm finding checksums for all of the individual files, placing them into a StringBuilder
, and then getting a checksum for that StringBuilder
once all of the checksums have been placed.
private string ChecksumFolder(string path)
{
string[] files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
StringBuilder allChecksum = new StringBuilder();
for (int count = 0; count < files.Length; count++)
{
allChecksum.Append(CreateChecksumFromFile(files[count]));
}
return CreateChecksumFromString(allChecksum.ToString());
}
I'm running into issues with the "System Volume Information" folder, which is causing an exception at the Directory.GetFiles()
line. The checksum for the files and the StringBuilder
works just fine when used on other folders.
Do you know of either another way to create a checksum for an entire USB or a way to programmatically get into that System Volume Information folder?
Thanks in advance!
EDIT: Adding CreateChecksumFromFile (the String version is essentially the same, just using a different kind of stream for the checksum)
private string CreateChecksumFromFile(string file)
{
string mChecksum;
using (FileStream stream = File.OpenRead(file))
{
SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
byte[] checksum = sha.ComputeHash(stream);
mChecksum = BitConverter.ToString(checksum).Replace("-", String.Empty);
sha.Clear();
}
return mChecksum;
}