The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HashScanner.cs 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net.Http;
  5. using System.Net.Http.Headers;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Teknik.Configuration;
  10. using Teknik.Utilities.Cryptography;
  11. namespace Teknik.ContentScanningService
  12. {
  13. public class HashScanner : ContentScanner
  14. {
  15. private static readonly HttpClient _client = new HttpClient();
  16. public HashScanner(Config config) : base(config)
  17. { }
  18. public async override Task<ScanResult> ScanFile(Stream stream)
  19. {
  20. var result = new ScanResult();
  21. if (stream != null)
  22. {
  23. // Set the start of the stream
  24. stream.Seek(0, SeekOrigin.Begin);
  25. if (_config.UploadConfig.HashScanConfig.Authenticate)
  26. {
  27. var byteArray = Encoding.UTF8.GetBytes($"{_config.UploadConfig.HashScanConfig.Username}:{_config.UploadConfig.HashScanConfig.Password}");
  28. _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
  29. }
  30. // compute the hash of the stream
  31. var hash = Utilities.Cryptography.SHA1.Hash(stream);
  32. HttpResponseMessage response = await _client.PostAsync(_config.UploadConfig.HashScanConfig.Endpoint, new StringContent(hash));
  33. HttpContent content = response.Content;
  34. string resultStr = await content.ReadAsStringAsync();
  35. if (resultStr == "true")
  36. {
  37. // The hash matched a CP entry, let's return the result as such
  38. result.ResultType = ScanResultType.ChildPornography;
  39. }
  40. else
  41. {
  42. result.RawResult = resultStr + " | " + hash + _client.DefaultRequestHeaders.Authorization.ToString();
  43. }
  44. }
  45. return result;
  46. }
  47. }
  48. }