Harry5134 commented on issue #450:
URL: https://github.com/apache/mina-sshd/issues/450#issuecomment-2002324211

   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`:
       <version>2.9.0</version> <!-- Or the latest version -->
   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()) {
               try (ClientSession session = client.connect("username", 
"hostname", 22).verify().getSession()) {
                   try (SftpClient sftpClient = session.createSftpClient()) {
                       String content = "Hello, SFTP!";
                       String filename = "example.txt";
                       // Calculate MD5 hash
                       byte[] md5Hash = 
                       String md5String = 
                       // Write file with MD5 hash
                       sftpClient.write(filename, new 
SftpClient.OpenMode.Write, SftpClient.OpenMode.CreateNew)
                       // Read file back
                       ByteArrayOutputStream baos = new ByteArrayOutputStream();
                       String fileContent = new String(baos.toByteArray(), 
                       // Verify MD5 hash
                       byte[] md5HashReceived = 
                       String md5StringReceived = 
                       if (md5String.equals(md5StringReceived)) {
                           System.out.println("MD5 hash verification 
                       } 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:
       <version>1.15</version> <!-- Or the latest version -->
   This example utilizes the `org.apache.commons.codec.digest.DigestUtils` 
class from Apache Commons Codec for MD5 hashing.

This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@mina.apache.org

For queries about this service, please contact Infrastructure at:

To unsubscribe, e-mail: dev-unsubscr...@mina.apache.org
For additional commands, e-mail: dev-h...@mina.apache.org

Reply via email to