Skip to content

Instantly share code, notes, and snippets.

@stavrossk
Last active December 13, 2015 22:39
Show Gist options
  • Save stavrossk/4985643 to your computer and use it in GitHub Desktop.
Save stavrossk/4985643 to your computer and use it in GitHub Desktop.
File hash calculator
private static byte[]
ComputeFileHash
(string filename)
{
try
{
Stream input
= File.OpenRead
(filename);
byte[] result
= ComputeFileHash(input);
return result;
}
catch(Exception e)
{
Debugger.LogMessageToFile
("An error occured while" +
" attempting to compute video hash." +
" The error was: " + e );
return null;
}
}
private static byte[] ComputeFileHash(Stream input)
{
long streamsize = input.Length;
long lhash = streamsize;
long i = 0;
var buffer
= new byte[sizeof(long)];
while (i < 65536 / sizeof(long)
&& (input.Read(buffer, 0, sizeof(long)) > 0))
{
i++;
lhash +=
BitConverter.ToInt64
(buffer, 0);
}
input.Position = Math.Max(0, streamsize - 65536);
i = 0;
while (i < 65536 / sizeof(long)
&& (input.Read(buffer, 0, sizeof(long)) > 0))
{
i++;
lhash += BitConverter.ToInt64(buffer, 0);
}
input.Close();
byte[] result
= BitConverter
.GetBytes(lhash);
Array.Reverse(result);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment