MovieHasher.cs 1.3 KB

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