You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.4 KiB
49 lines
1.4 KiB
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
|
|
namespace OpenSubtitlesHandler
|
|
{
|
|
public class MovieHasher
|
|
{
|
|
public static byte[] ComputeMovieHash(Stream input)
|
|
{
|
|
using (input)
|
|
{
|
|
long lhash, streamsize;
|
|
streamsize = input.Length;
|
|
lhash = streamsize;
|
|
|
|
long i = 0;
|
|
byte[] 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);
|
|
}
|
|
byte[] result = BitConverter.GetBytes(lhash);
|
|
Array.Reverse(result);
|
|
return result;
|
|
}
|
|
}
|
|
|
|
public static string ToHexadecimal(byte[] bytes)
|
|
{
|
|
var hexBuilder = new StringBuilder();
|
|
for (int i = 0; i < bytes.Length; i++)
|
|
{
|
|
hexBuilder.Append(bytes[i].ToString("x2"));
|
|
}
|
|
return hexBuilder.ToString();
|
|
}
|
|
}
|
|
}
|