jellyfin/OpenSubtitlesHandler/MovieHasher.cs

49 lines
1.4 KiB
C#
Raw Normal View History

using System;
2014-05-05 04:36:45 +00:00
using System.IO;
using System.Text;
namespace OpenSubtitlesHandler
{
public class MovieHasher
{
2015-09-13 21:32:02 +00:00
public static byte[] ComputeMovieHash(Stream input)
2014-05-05 04:36:45 +00:00
{
2016-10-27 21:38:10 +00:00
using (input)
2014-05-05 04:36:45 +00:00
{
2016-10-27 21:38:10 +00:00
long lhash, streamsize;
streamsize = input.Length;
lhash = streamsize;
2014-05-05 04:36:45 +00:00
2016-10-27 21:38:10 +00:00
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;
2014-05-05 04:36:45 +00:00
}
}
public static string ToHexadecimal(byte[] bytes)
{
StringBuilder hexBuilder = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
hexBuilder.Append(bytes[i].ToString("x2"));
}
return hexBuilder.ToString();
}
}
}