Guest User

Untitled

a guest
Nov 22nd, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. using System.IO;
  2. using System.Security.Cryptography;
  3.  
  4. ...
  5.  
  6. public byte[] HashedFileWrite(string filename, Stream input)
  7. {
  8. var hash_algorithm = MD5.Create();
  9.  
  10. using(var file = File.OpenWrite(filename))
  11. {
  12. byte[] buffer = new byte[4096];
  13. int read = 0;
  14.  
  15. while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
  16. {
  17. hash_algorithm.TransformBlock(buffer, 0, read, null, 0);
  18. file.Write(buffer, 0, read);
  19. }
  20.  
  21. hash_algorithm.TransformFinalBlock(buffer, 0, read);
  22. }
  23.  
  24. return hash_algorithm.Hash;
  25. }
  26.  
  27. private static byte[] CopyAndHash(string source, string target, Action<double> progress, Func<bool> isCanceled)
  28. {
  29. using(var sha512 = SHA512.Create())
  30. using (var targetStream = File.OpenWrite(target))
  31. using (var cryptoStream = new CryptoStream(targetStream, sha512, CryptoStreamMode.Write))
  32. using (var sourceStream = File.OpenRead(source))
  33. {
  34. byte[] buffer = new byte[81920];
  35. int read;
  36. while ((read = sourceStream.Read(buffer, 0, buffer.Length)) > 0 && !isCanceled())
  37. {
  38. cryptoStream.Write(buffer, 0, read);
  39.  
  40. progress?.Invoke((double) sourceStream.Length / sourceStream.Position * 100);
  41. }
  42. }
  43.  
  44. File.SetAttributes(target, File.GetAttributes(source));
  45.  
  46. return sha512.Hash;
  47. }
  48. }
  49.  
  50. Stream s = File.Open("infile.dat");
  51. Stream out = File.Create("outfile.dat");
  52. HashWrapStream hasher = new HashWrapStream(out);
  53. byte[] buffer=new byte[1024];
  54. int read = 0;
  55. while ((read=s.Read(buffer)!=0)
  56. {
  57. hasher.Write(buffer);
  58. }
  59. long hash=hasher.GetComputedHash(); // get actual hash
  60. hasher.Dispose();
  61. s.Dispose();
  62.  
  63. //var ticks = **some_array_you_want_to_write_as_csv**
  64.  
  65. using (var memoryStream = new System.IO.MemoryStream())
  66. {
  67. using (var textWriter = new System.IO.StreamWriter(memoryStream))
  68. {
  69. using (var csv = new CsvHelper.CsvWriter(textWriter))
  70. {
  71. csv.Configuration.DetectColumnCountChanges = true; //error checking
  72. csv.Configuration.RegisterClassMap<TickDataClassMap>();
  73. csv.WriteRecords(ticks);
  74.  
  75. textWriter.Flush();
  76.  
  77. //write to disk
  78. using (var fileStream = new System.IO.FileStream(targetFileName, System.IO.FileMode.Create))
  79. {
  80. memoryStream.Position = 0;
  81. memoryStream.CopyTo(fileStream);
  82.  
  83. }
  84.  
  85. //write sha256 hash, ensuring that the file was properly written
  86. using (var sha256 = System.Security.Cryptography.SHA256.Create())
  87. {
  88. memoryStream.Position = 0;
  89. var hash = sha256.ComputeHash(memoryStream);
  90. using (var reader = System.IO.File.OpenRead(targetFileName))
  91. {
  92. System.IO.File.WriteAllText(targetFileName + ".sha256", hash.ConvertByteArrayToHexString());
  93. }
  94. }
  95.  
  96. }
  97.  
  98. }
  99. }
Add Comment
Please, Sign In to add comment