MovieHasher.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. using (input)
  11. {
  12. long lhash, streamsize;
  13. streamsize = input.Length;
  14. lhash = streamsize;
  15. long i = 0;
  16. byte[] buffer = new byte[sizeof(long)];
  17. while (i < 65536 / sizeof(long) && (input.Read(buffer, 0, sizeof(long)) > 0))
  18. {
  19. i++;
  20. lhash += BitConverter.ToInt64(buffer, 0);
  21. }
  22. input.Position = Math.Max(0, streamsize - 65536);
  23. i = 0;
  24. while (i < 65536 / sizeof(long) && (input.Read(buffer, 0, sizeof(long)) > 0))
  25. {
  26. i++;
  27. lhash += BitConverter.ToInt64(buffer, 0);
  28. }
  29. byte[] result = BitConverter.GetBytes(lhash);
  30. Array.Reverse(result);
  31. return result;
  32. }
  33. }
  34. public static string ToHexadecimal(byte[] bytes)
  35. {
  36. var hexBuilder = new StringBuilder();
  37. for (int i = 0; i < bytes.Length; i++)
  38. {
  39. hexBuilder.Append(bytes[i].ToString("x2"));
  40. }
  41. return hexBuilder.ToString();
  42. }
  43. }
  44. }