SubtitleEdit/libse/MovieHasher.cs

66 lines
1.9 KiB
C#
Raw Normal View History

2018-08-24 14:41:08 +02:00
using System;
using System.IO;
using System.Text;
namespace Nikse.SubtitleEdit.Core
{
/// <summary>
/// Hash from OpenSubtitles: http://trac.opensubtitles.org/projects/opensubtitles/wiki/HashSourceCodes
/// </summary>
public static class MovieHasher
{
2018-08-24 22:09:53 +02:00
public static string GenerateHash(string videoFileName)
2018-08-24 14:41:08 +02:00
{
2018-08-24 22:09:53 +02:00
return ToHexadecimal(ComputeMovieHash(videoFileName));
2018-08-24 14:41:08 +02:00
}
2018-08-24 22:09:53 +02:00
private static byte[] ComputeMovieHash(string videoFileName)
2018-08-24 14:41:08 +02:00
{
byte[] result;
2018-08-24 22:09:53 +02:00
using (Stream input = new FileStream(videoFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
2018-08-24 14:41:08 +02:00
{
result = ComputeMovieHash(input);
}
return result;
}
private static byte[] ComputeMovieHash(Stream input)
{
long streamsize = input.Length;
long lhash = streamsize;
long i = 0;
var buffer = new byte[sizeof(long)];
const int c = 65536;
while (i < c / sizeof(long) && input.Read(buffer, 0, sizeof(long)) > 0)
{
i++;
lhash += BitConverter.ToInt64(buffer, 0);
}
input.Position = Math.Max(0, streamsize - c);
i = 0;
while (i < 65536 / sizeof(long) && input.Read(buffer, 0, sizeof(long)) > 0)
{
i++;
lhash += BitConverter.ToInt64(buffer, 0);
}
input.Close();
var result = BitConverter.GetBytes(lhash);
Array.Reverse(result);
return result;
}
private 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();
}
}
}