2

I am using commons VFS to transfer files over sftp. Once copying is done, i want to compare the checksum of source and destination file.

How can i find the checksum of remote file in java ? For local file,i am using FileUtils.checksum().

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
user608020
  • 313
  • 4
  • 15
  • 4
    I'm not aware of any SFTP command (native or through VFS) that gives you the checksum of a remote file. But http://stackoverflow.com/questions/16216902/getting-md5-checksum-on-the-remote-server-using-jsch seems to imply that SFTP does integrity checks on the fly, so this may be pointless... Still, if this is of importance, since you are using SFTP, you may have SSH access to the remote host. Given a SSH session, you could ask the remote server to compute the checksum (maybe through openssl). – GPI Jul 22 '14 at 08:09

1 Answers1

1

It depends on which check sum implementation you're using (e.g. CRC32, etc.).

Let's take CRC32 as an example. You create an instance of CRC32, and update it with more and more bytes. This is a perfect fit for the task you're trying to do, since you can update whenever you have more bytes available in the input stream.

Here's a half-baked exanple:

public long checksumRemote(InputStream inputStream) {
    CRC32 checksum = new CRC32();
    boolean finished = false;
    byte[] buffer = new byte[4096];

    while (!finished) {
        int bytesRead = inputStream.read(buffer);
        if (bytesRead < 0) {
            finished = true;
        } else if (bytesRead > 0) {
            checksum.update(buffer, 0, bytesRead);
        }
    }

    return checksum.getValue();
}
ethanfar
  • 3,733
  • 24
  • 43
  • Yes but you're computing the bytes as they come out of the SFTP stream. So you end up computing the checksum of the local file, not the remote one. (Although you may argue that since SFTP is checksumming on the fly, and since you checksum the stream, then comparing the checkum computed on the fly with that of the final File is good enough, I think the OP has in mind a direct comparison of the remote checksum with the final one) – GPI Jul 22 '14 at 08:24
  • Someone suggested to download the remote file and checksum that and compare. This would be performance kill. – user608020 Jul 22 '14 at 08:28