FtpClient.GetHash Method

System.Net.FtpClient

Collapse image Expand Image Copy image CopyHover image
Gets the hash of an object on the server using the currently selected hash algorithm. Supported algorithms, if any, are available in the HashAlgorithms property. You should confirm that it's not equal to FtpHashAlgorithm.NONE before calling this method otherwise the server trigger a FtpCommandException() due to a lack of support for the HASH command. You can set the algorithm using the SetHashAlgorithm() method and you can query the server for the current hash algorithm using the GetHashAlgorithm() method. This feature is experimental and based on the following draft: http://tools.ietf.org/html/draft-bryan-ftpext-hash-02

Namespace: System.Net.FtpClient
Assembly: System.Net.FtpClient (in System.Net.FtpClient.dll) Version: 1.0.5064.17461

Syntax

C#
public FtpHash GetHash(
	string path
)
Visual Basic
Public Function GetHash ( 
	path As String
) As FtpHash
Visual C++
public:
virtual FtpHash^ GetHash(
	String^ path
) sealed

Parameters

path
Type: System..::..String
Full or relative path of the object to compute the hash for.

Return Value

Type: FtpHash
The hash of the file.

Implements

IFtpClient..::..GetHash(String)

Examples

C#  Copy imageCopy
using System;
using System.Net;
using System.Net.FtpClient;

namespace Examples {
    public class GetHashExample {
        public static void GetHash() {
            using (FtpClient cl = new FtpClient()) {
                cl.Credentials = new NetworkCredential("user", "pass");
                cl.Host = "some.ftpserver.on.the.internet.com";

                // If server supports the HASH command then the
                // FtpClient.HashAlgorithms flags will NOT be equal
                // to FtpHashAlgorithm.NONE. 
                if (cl.HashAlgorithms != FtpHashAlgorithm.NONE) {
                    FtpHash hash;

                    // Ask the server to compute the hash using whatever 
                    // the default hash algorithm (probably SHA-1) on the 
                    // server is.
                    hash = cl.GetHash("/path/to/remote/somefile.ext");

                    // The FtpHash.Verify method computes the hash of the
                    // specified file or stream based on the hash algorithm
                    // the server computed its hash with. The classes used
                    // for computing the local hash are  part of the .net
                    // framework, located in the System.Security.Cryptography
                    // namespace and are derived from 
                    // System.Security.Cryptography.HashAlgorithm.
                    if (hash.Verify("/path/to/local/somefile.ext")) {
                        Console.WriteLine("The computed hashes match!");
                    }

                    // Manually specify the hash algorithm to use.
                    if (cl.HashAlgorithms.HasFlag(FtpHashAlgorithm.MD5)) {
                        cl.SetHashAlgorithm(FtpHashAlgorithm.MD5);
                        hash = cl.GetHash("/path/to/remote/somefile.ext");
                        if (hash.Verify("/path/to/local/somefile.ext")) {
                            Console.WriteLine("The computed hashes match!");
                        }
                    }
                }
            }
        }
    }
}

See Also