HashHelper.cs 940 B

1234567891011121314151617181920212223242526272829303132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Web;
  7. namespace GreenTree.Nachtragsmanagement.Web.Extensions
  8. {
  9. public static class HashHelper
  10. {
  11. /// <summary>
  12. /// Calculates a MD5-Hash value for a specific file
  13. /// </summary>
  14. /// <param name="filePath">Full path of the file.</param>
  15. public static string GetFileMd5(string filePath)
  16. {
  17. if (!File.Exists(filePath))
  18. throw new FileNotFoundException("The specified file does not exist.");
  19. using (var md5 = MD5.Create())
  20. {
  21. using (var stream = File.OpenRead(filePath))
  22. {
  23. var hash = md5.ComputeHash(stream);
  24. return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
  25. }
  26. }
  27. }
  28. }
  29. }