Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11.  
  12. namespace HashFiles
  13. {
  14. class Program
  15. {
  16. static string Paths = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  17.  
  18. static void Main(string[] args)
  19. {
  20. CreateFile();
  21. Console.WriteLine("Attempting to Hash your Files!");
  22. string filepath = Paths + "/files/";
  23. Console.WriteLine("Path: "+filepath);
  24. string[] localfilelist = Directory.GetFiles(filepath, "*", SearchOption.AllDirectories);
  25. string[] localfilenames = new string[localfilelist.Length];
  26. string[] md5 = new string[localfilelist.Length];
  27. for (int i = 0;i<localfilelist.Length;i++)
  28. {
  29. md5[i] = CalculateMD5(localfilelist[i]);
  30. Console.WriteLine("Hashing "+localfilelist[i]+" Hash: "+md5[i]);
  31. }
  32. for (int i = 0; i < localfilenames.Length; i++)
  33. {
  34. localfilenames[i] = localfilelist[i].Replace(filepath, "");
  35. }
  36. using (StreamWriter sw = new StreamWriter("manifest.txt", true))
  37. {
  38. for (int i = 0; i < localfilelist.Length; i++)
  39. {
  40. string[] split = Regex.Split(localfilelist[i], "/files/");
  41.  
  42. sw.WriteLine(split[1]+"="+localfilenames[i]+"="+md5[i]);
  43. }
  44. sw.Close();
  45.  
  46. Console.WriteLine("Finished");
  47. Console.ReadLine();
  48. }
  49. }
  50.  
  51. public static void CreateFile()
  52. {
  53. if (!File.Exists("manifest.txt"))
  54. {
  55. File.Create("manifest.txt");
  56. }
  57. }
  58.  
  59. public static string CalculateMD5(string filename) //calculate MD5s the same way HashMyFiles does it
  60. {
  61. using (var md5 = MD5.Create())
  62. {
  63. using (var stream = File.OpenRead(filename))
  64. {
  65. var hash = md5.ComputeHash(stream);
  66. return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
  67. }
  68. }
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement