GetHash.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Net;
  3. using System.Net.FtpClient;
  4. namespace Examples {
  5. public class GetHashExample {
  6. public static void GetHash() {
  7. using (FtpClient cl = new FtpClient()) {
  8. cl.Credentials = new NetworkCredential("user", "pass");
  9. cl.Host = "some.ftpserver.on.the.internet.com";
  10. // If server supports the HASH command then the
  11. // FtpClient.HashAlgorithms flags will NOT be equal
  12. // to FtpHashAlgorithm.NONE.
  13. if (cl.HashAlgorithms != FtpHashAlgorithm.NONE) {
  14. FtpHash hash;
  15. // Ask the server to compute the hash using whatever
  16. // the default hash algorithm (probably SHA-1) on the
  17. // server is.
  18. hash = cl.GetHash("/path/to/remote/somefile.ext");
  19. // The FtpHash.Verify method computes the hash of the
  20. // specified file or stream based on the hash algorithm
  21. // the server computed its hash with. The classes used
  22. // for computing the local hash are part of the .net
  23. // framework, located in the System.Security.Cryptography
  24. // namespace and are derived from
  25. // System.Security.Cryptography.HashAlgorithm.
  26. if (hash.Verify("/path/to/local/somefile.ext")) {
  27. Console.WriteLine("The computed hashes match!");
  28. }
  29. // Manually specify the hash algorithm to use.
  30. if (cl.HashAlgorithms.HasFlag(FtpHashAlgorithm.MD5)) {
  31. cl.SetHashAlgorithm(FtpHashAlgorithm.MD5);
  32. hash = cl.GetHash("/path/to/remote/somefile.ext");
  33. if (hash.Verify("/path/to/local/somefile.ext")) {
  34. Console.WriteLine("The computed hashes match!");
  35. }
  36. }
  37. }
  38. }
  39. }
  40. }
  41. }