Calculate file checksum in FTP server using Apache

2019-04-10 06:05发布

问题:

I am using FtpClient of Apache Commons Net to upload videos to FTP server. To check if the file has really been successfully transferred, I want to calculate the checksum of remote file, but unfortunately I found there is no related API I could use.

My question is: Whether there is a need to calculate file checksum in ftp server?
If the answer is yes, how to get checksum in FtpClient?
If the answer is no, how do FtpClient know if the file has really been successfully and completely transferred?

回答1:

With FTP, I'd recommend to verify the upload, if possible.

The problem is that there's no widespread standard API for calculating checksum with FTP.

There are many proposals for checksum calculation command for FTP. None were accepted yet.

The latest proposal is:
https://tools.ietf.org/html/draft-bryan-ftpext-hash-02

As a consequence, different FTP servers support different checksum commands, with a different syntax. HASH, XSHA1, XSHA256, XSHA512, XMD5, MD5, XCRC, to name some. You need to check what, if any, your FTP server supports.

You can test that with WinSCP. The WinSCP supports all the previously mentioned commands. Test its checksum calculation function or checksum scripting command. If they work, enable logging and check what command and what syntax WinSCP uses against your server.

> 2015-04-28 09:19:16.558 XSHA1 /test/file.dat
< 2015-04-28 09:19:22.778 213 a98faefdb2c36ca352a2d9b01668aec6b641cf4b 

Then execute the command using Apache Commons Net sendCommand method:

if (FTPReply.isPositiveCompletion(ftpClient.sendCommand("XSHA1", "filename"))
{
    String[] reply = ftpClient.getReplyStrings();
}

(I'm the author of WinSCP)


If your server does not support any of the checksum commands, you do not have many options:

  • Download the file back and check it locally.
  • When using encryption (TLS/SSL), chances of the file being corrupted during transfer are significantly lower. The receiving party (server in this case) would otherwise fail to decrypt the data. So if you are sure that the file transfer completed (no decryption errors and the size of the uploaded file is the same as size of the original local file), you can be pretty sure that the uploaded file is correct.


回答2:

Just a addition of how I implemented this. When dealing with standard ftp servers without any additionak modules loaded for checksum checking, all i did was creating a list of MD5 CRC hashes about each file into an SFV file. Say its called uploads.sfv (just in the same format as sfv generator would do). This allows you to do further checksum checks.

Examples about the server side support checksum checking support:

  • PZS-ng for cuftpd, glftpd
  • mod_digest for ProFTPD

Of course as @MartinPrikryl highlighted, none of these are standardized.



回答3:

That's a long shot, but if the server supports php, you can exploit that.

Save the following as a php file (say, check.php), in the same folder as your name_of_file.txt file:

<? php
echo md5_file('name_of_file.txt');
php>

Then, visit the page check.php, and you should get the md5 hash of your file.

Related questions:

  • FTP: copy, check integrity and delete
  • How to perform checksums during a SFTP file transfer for data integrity?
  • https://serverfault.com/q/98597/401691