Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question regarding the md5-hash extension #450

Open
avnerw74 opened this issue Jan 4, 2024 · 3 comments
Open

Question regarding the md5-hash extension #450

avnerw74 opened this issue Jan 4, 2024 · 3 comments

Comments

@avnerw74
Copy link

avnerw74 commented Jan 4, 2024

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

@gnodet
Copy link
Contributor

gnodet commented Jan 30, 2024

You can find an example of calling a client side extension at:

CopyFileExtension copyFile = client.getExtension(CopyFileExtension.class);

So in this case, it would look like:

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

@lgoldstein
Copy link
Contributor

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

@Harry5134
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants