Verify file hash after FTP upload

FTP provides a way to transfer files which is fast but there are no standard built-in ways to ensure that the file got transferred intact.

Although we use .NET socket to transfer data and the underlying TCP/IP ensures that all data packets are transferred properly, mishandling of buggy firewall or third-party application may happen, leading the FTP client to think that the transfer was successful when the transferred data has been compromised and incomplete. This is why we need a checksum mechanism. This article describes how to check the file checksum after upload to FTP server using the Ultimate FTP. You can use Crc, MD5 and SHA1 algorithms for checksum verification.

Get File Checksum

First, your remote FTP server must support the checksum calculation. Not all FTP servers support this. Even the RFC specification does not define a command to calculate a checksum of a remote file. Many recent FTP servers support determining checksums (CRC-32, MD5 or SHA-1) of remote files. And they use different commands like XSHA1, XSHA256, XSHA512, MD5, XMD5, XCRC and HASH.

We handle these command automatically. You can use Ftp object's GetSupportedChecksumTypes method to determine whether the FTP server support this functionality (and which algorithms). If it does, use GetFileChecksum method to get a checksum of a file at the server. The corresponding checksums of local files can be calculated using CalculateLocalChecksum() method of DiskFileSystem.

This way we can ensure that the files are uploaded correctly by comparing the server checksum with of the local files.

We now take advantage of those methods in the following example:

Add using directive

You will need to add namespaces ComponentPro.IO and ComponentPro.Net.

using System;
using System.Collections.Generic;
using System.Text;
using ComponentPro.IO;
using ComponentPro.Net;

Connect and Authenticate

Then we create a new instance of the Ftp class, connect to the FTP server and then authenticate the user. In this example we use 'myserver.' That should be changed to your server's IP address or hostname. The credentials should also be changed to yours.

// Create a new class instance.
Ftp client = new Ftp();

// Connect to the FTP server.

// Or you can specify the FTP port with 
// client.Connect("myserver", 21); 
// Authenticate.
client.Authenticate("userName", "password");

Check server's capability

Now we want to know if the server supports any checksum calculation methods.

FileChecksumType[] supportedChecksumTypes = client.GetSupportedChecksumTypes();

if (supportedChecksumTypes.Length == 0)
    // Show error and exit. 
    Console.WriteLine("No checksum algorithm supported by the server.");

Upload file

Yea, it supports at least one method. We can now upload a file to the server.

// Upload the file. 
const string remoteFile = "/my remote file.dat";
const string localFile = "my local file";

long transferred = client.UploadFile(localFile, remoteFile);
Console.WriteLine("Bytes transferred: " + transferred);

Get remote file's checksum

After uploading we get the remote file's checksum.

// Get remote checksum 
// Suppose the server support at least one algorithm. 
string remoteChecksum = client.GetFileChecksum(supportedChecksumTypes[0], remoteFile);
Console.WriteLine("Remote checksum: " + remoteChecksum);

Compare checksums

Then we calculate the local file checksum and compare them. The library DiskFileSystem class in ComponentPro.FileSystem.dll already has that method named CalculateLocalChecksum.

// Get local checksum 
string localChecksum = DiskFileSystem.CalculateLocalChecksum(supportedChecksumTypes[0], localFile);
Console.WriteLine("Local checksum: " + localChecksum);

if (localChecksum == remoteChecksum)
    Console.WriteLine("File uploaded successfully. {0} checked.", supportedChecksumTypes[0]);
    Console.WriteLine("File uploaded successfully. {0} not matched.", supportedChecksumTypes[0]);

Close the connection

We close the connection when done.

// Disconnect.

45-Day Money Back Guarantee

We will refund your full money in 45 days
if you are not satisfied with our products

Buy Now

Dont miss out Get update on new articles and other opportunities