mina-sshd icon indicating copy to clipboard operation
mina-sshd copied to clipboard

Question regarding the md5-hash extension

Open avnerw74 opened this issue 2 years ago • 3 comments

Hi, I noticed mina-sshd supports md5-hash extended SFTP request. Do you have an example of how to call it from java code, or are you familiar with open source code that supports it? Thanks, Avner

avnerw74 avatar Jan 04 '24 10:01 avnerw74

You can find an example of calling a client side extension at: https://github.com/apache/mina-sshd/blob/6d93af8e1f852d50849ef158ee30bebf67e46fbc/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/fs/SftpFileSystemProvider.java#L777

So in this case, it would look like:

    SftpClient client = ...;
    MD5FileExtension md5 = client.getExtension(MD5FileExtension.class);
    md5.getHash(...)

gnodet avatar Jan 30 '24 17:01 gnodet

Of course need to check that result of client.getExtension(MD5FileExtension.class) returns non-NULL value, thus indicating that the server supports this extension

lgoldstein avatar Feb 19 '24 16:02 lgoldstein

To utilize the MD5-hash extended SFTP request with mina-sshd in Java, you can use the Apache MINA SSHD library. Below is an example demonstrating how to perform an SFTP operation with MD5 hashing:

First, ensure you have the necessary dependencies in your Maven pom.xml:

<dependency>
    <groupId>org.apache.sshd</groupId>
    <artifactId>sshd-core</artifactId>
    <version>2.9.0</version> <!-- Or the latest version -->
</dependency>

Then, you can use the following Java code:

import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.client.subsystem.sftp.SftpClient;
import org.apache.sshd.common.util.io.IoUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class SftpExample {

    public static void main(String[] args) throws IOException {
        try (SshClient client = SshClient.setUpDefaultClient()) {
            client.start();
            try (ClientSession session = client.connect("username", "hostname", 22).verify().getSession()) {
                session.addPasswordIdentity("password");
                session.auth().verify();
                try (SftpClient sftpClient = session.createSftpClient()) {
                    String content = "Hello, SFTP!";
                    String filename = "example.txt";

                    // Calculate MD5 hash
                    byte[] md5Hash = org.apache.commons.codec.digest.DigestUtils.md5(content.getBytes(StandardCharsets.UTF_8));
                    String md5String = Base64.getEncoder().encodeToString(md5Hash);

                    // Write file with MD5 hash
                    sftpClient.write(filename, new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)), SftpClient.OpenMode.Write, SftpClient.OpenMode.CreateNew)
                            .verify();

                    // Read file back
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    sftpClient.read(filename).writeTo(baos);
                    String fileContent = new String(baos.toByteArray(), StandardCharsets.UTF_8);

                    // Verify MD5 hash
                    byte[] md5HashReceived = org.apache.commons.codec.digest.DigestUtils.md5(fileContent.getBytes(StandardCharsets.UTF_8));
                    String md5StringReceived = Base64.getEncoder().encodeToString(md5HashReceived);

                    if (md5String.equals(md5StringReceived)) {
                        System.out.println("MD5 hash verification successful.");
                    } else {
                        System.err.println("MD5 hash verification failed.");
                    }
                }
            }
        }
    }
}

Make sure to replace "username", "password", and "hostname" with your SSH server's credentials and address respectively.

This example demonstrates writing a file to the SFTP server with its MD5 hash, then reading the file back and verifying the MD5 hash.

Ensure you have the Apache Commons Codec library in your dependencies for MD5 hash calculation:

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.15</version> <!-- Or the latest version -->
</dependency>

This example utilizes the org.apache.commons.codec.digest.DigestUtils class from Apache Commons Codec for MD5 hashing.

Harry5134 avatar Mar 17 '24 06:03 Harry5134