-
Notifications
You must be signed in to change notification settings - Fork 345
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
Comments
You can find an example of calling a client side extension at: mina-sshd/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/fs/SftpFileSystemProvider.java Line 777 in 6d93af8
So in this case, it would look like:
|
Of course need to check that result of |
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 <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 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 |
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
The text was updated successfully, but these errors were encountered: