We have a customer complaining that the audio files being transfered to their ftp site are not matching the original MD5 hash. The file transfer is completing without error yet the files do not match exactly. How can we check this?

Our app is built using VB.Net.

Thanks, Richard

asked 24 Feb '10, 17:50

Richard's gravatar image

Richard
261
accept rate: 0%


Many modern FTP servers support determining checksums (CRC-32, MD5 or SHA-1) of remote files. You can use Ftp object's GetSupportedChecksumTypes method to determine whether the FTP server support this functionality (and which algorithms). If it does, use GetRemoteChecksum method to get a checksum of a file at the server or a specified part of it. The corresponding checksums of local files can be calculated using Ftp.CalculateLocalChecksum static method. Not all FTP servers support this, unfortunately.

An alternative is FTP/SSL. SSL has built-in integrity checking (either using MD5 or SHA1) and should never transfer a damaged data packet.

Also, make sure to check that the file lengths of local and remote files are the same before determining any checksums. If they aren't, they will never match.

Here is a sample code:

//create FTP client
Ftp client = new Ftp();
//Connect to the FTP server
client.Connect(hostname, port);
//authorize to the server
client.Login(login, password);

//upload a file
long uploadLength = client.PutFile(localPath, remotePath);

// get the remote file length
long remoteLength = client.GetFileLength(remotePath);

// get the local file length
long localLength = new System.IO.FileInfo(localPath).Length;

//check file lengths
if (uploadLength != localLength || remoteLength != localLength)
    throw new Exception("File lengths differ.");

//get checksums supported by the server
FtpChecksumType[] checksums = client.GetSupportedChecksumTypes();

//check the checksum of local and remote file
if (checksums.Length > 0)
{
    string remoteChecksum = client.GetRemoteChecksum(checksums[0], remotePath);
    string localChecksum = Ftp.CalculateLocalChecksum(checksums[0], localPath);

    if (remoteChecksum != localChecksum)
        throw new Exception("File checksums differ.");
}
else
{
    throw new Exception("Server does not support checksum");
}
link

answered 24 Feb '10, 20:01

Lukas%20Pokorny's gravatar image

Lukas Pokorny ♦♦
2.2k18
accept rate: 32%

edited 10 Jan, 14:59

Pavel%20Matyska's gravatar image

Pavel Matyska ♦♦
252

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×136
×115

Asked: 24 Feb '10, 17:50

Seen: 891 times

Last updated: 10 Jan, 14:59