MovieHasher.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace OpenSubtitlesHandler
  8. {
  9. public class MovieHasher
  10. {
  11. public static byte[] ComputeMovieHash(Stream input)
  12. {
  13. long lhash, streamsize;
  14. streamsize = input.Length;
  15. lhash = streamsize;
  16. long i = 0;
  17. byte[] buffer = new byte[sizeof(long)];
  18. while (i < 65536 / sizeof(long) && (input.Read(buffer, 0, sizeof(long)) > 0))
  19. {
  20. i++;
  21. lhash += BitConverter.ToInt64(buffer, 0);
  22. }
  23. input.Position = Math.Max(0, streamsize - 65536);
  24. i = 0;
  25. while (i < 65536 / sizeof(long) && (input.Read(buffer, 0, sizeof(long)) > 0))
  26. {
  27. i++;
  28. lhash += BitConverter.ToInt64(buffer, 0);
  29. }
  30. input.Close();
  31. byte[] result = BitConverter.GetBytes(lhash);
  32. Array.Reverse(result);
  33. return result;
  34. }
  35. public static string ToHexadecimal(byte[] bytes)
  36. {
  37. StringBuilder hexBuilder = new StringBuilder();
  38. for (int i = 0; i < bytes.Length; i++)
  39. {
  40. hexBuilder.Append(bytes[i].ToString("x2"));
  41. }
  42. return hexBuilder.ToString();
  43. }
  44. }
  45. }