Created
March 14, 2012 17:02
-
-
Save raytiley/2037893 to your computer and use it in GitHub Desktop.
Hashing first 100MB of a file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Grab a SHA1 of the first 100MB file | |
LogToolbox.TraceMessage(string.Format("{0}: Generating SHA1: {1}", m_Parent.CMName, file.Key)); | |
using (HashAlgorithm sha = new SHA1CryptoServiceProvider()) | |
{ | |
FileStream fs = null; | |
try | |
{ | |
int bytesToSHA = 1024 * 1024 * 100; //100MB | |
byte[] first100MB = new byte[bytesToSHA]; | |
fs = new FileStream(file.Key, FileMode.Open, FileAccess.Read); | |
if (info.FileSize < bytesToSHA) | |
{ | |
fs.Read(first100MB, 0, (int)info.FileSize); | |
} | |
else | |
{ | |
fs.Read(first100MB, 0, bytesToSHA); | |
} | |
info.SHA1 = BitConverter.ToString(sha.ComputeHash(first100MB)).Replace("-", ""); | |
} | |
catch (Exception ex) | |
{ | |
LogToolbox.TraceMessage(string.Format("{0} SHA 1 Generation failed for{1} - {2}", m_Parent.CMName, file.Key, ex.Message)); | |
} | |
finally | |
{ | |
if (fs != null) | |
{ | |
fs.Close(); | |
fs.Dispose(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment