Last active
December 13, 2015 22:39
-
-
Save stavrossk/4985643 to your computer and use it in GitHub Desktop.
File hash calculator
This file contains 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
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