Search in sources :

Example 36 with KeyPair

use of com.jcraft.jsch.KeyPair in project devspaces-images by redhat-developer.

the class SshManager method generatePair.

/**
 * Generates and stores ssh pair for specified user.
 *
 * @param owner the id of the user who will be the owner of the ssh pair
 * @param service service name pf ssh pair
 * @param name name of pair
 * @return instance of generated ssh pair
 * @throws ConflictException when given ssh pair cannot be generated or created
 * @throws ServerException when any other error occurs during ssh pair generating or creating
 */
public SshPairImpl generatePair(String owner, String service, String name) throws ServerException, ConflictException {
    KeyPair keyPair;
    try {
        keyPair = KeyPair.genKeyPair(genJSch, 2, 2048);
    } catch (JSchException e) {
        throw new ServerException("Failed to generate ssh pair.", e);
    }
    ByteArrayOutputStream privateBuff = new ByteArrayOutputStream();
    keyPair.writePrivateKey(privateBuff);
    ByteArrayOutputStream publicBuff = new ByteArrayOutputStream();
    keyPair.writePublicKey(publicBuff, null);
    final SshPairImpl generatedSshPair = new SshPairImpl(owner, service, name, publicBuff.toString(), privateBuff.toString());
    sshDao.create(generatedSshPair);
    return generatedSshPair;
}
Also used : JSchException(com.jcraft.jsch.JSchException) KeyPair(com.jcraft.jsch.KeyPair) ServerException(org.eclipse.che.api.core.ServerException) SshPairImpl(org.eclipse.che.api.ssh.server.model.impl.SshPairImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 37 with KeyPair

use of com.jcraft.jsch.KeyPair in project confij by keykey7.

the class GitResourceSshTest method createKeyPair.

private static Path createKeyPair(Path privateKeyFile) throws Exception {
    JSch jsch = new JSch();
    KeyPair pair = KeyPair.genKeyPair(jsch, KeyPair.RSA, 2048);
    try (OutputStream out = new FileOutputStream(privateKeyFile.toFile())) {
        pair.writePrivateKey(out);
    }
    Path publicKeyFile = privateKeyFile.getParent().resolve(privateKeyFile.getFileName() + ".pub");
    try (OutputStream out = new FileOutputStream(publicKeyFile.toFile())) {
        pair.writePublicKey(out, TEST_USER);
    }
    return publicKeyFile;
}
Also used : Path(java.nio.file.Path) KeyPair(com.jcraft.jsch.KeyPair) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) JSch(com.jcraft.jsch.JSch)

Example 38 with KeyPair

use of com.jcraft.jsch.KeyPair in project confij by keykey7.

the class GitResourceSshTest method createHostKey.

private static byte[] createHostKey(OutputStream publicKey) throws Exception {
    JSch jsch = new JSch();
    KeyPair pair = KeyPair.genKeyPair(jsch, KeyPair.RSA, 2048);
    pair.writePublicKey(publicKey, "");
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        pair.writePrivateKey(out);
        out.flush();
        return out.toByteArray();
    }
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JSch(com.jcraft.jsch.JSch)

Aggregations

KeyPair (com.jcraft.jsch.KeyPair)38 JSch (com.jcraft.jsch.JSch)32 ByteArrayOutputStream (java.io.ByteArrayOutputStream)20 JSchException (com.jcraft.jsch.JSchException)17 File (java.io.File)11 IOException (java.io.IOException)10 OutputStream (java.io.OutputStream)5 FileOutputStream (java.io.FileOutputStream)3 Path (java.nio.file.Path)3 ServerException (org.eclipse.che.api.core.ServerException)3 SshPairImpl (org.eclipse.che.api.ssh.server.model.impl.SshPairImpl)3 BasicSSHUserPrivateKey (com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey)2 SSHKeyPair (io.cdap.cdap.runtime.spi.ssh.SSHKeyPair)2 SSHPublicKey (io.cdap.cdap.runtime.spi.ssh.SSHPublicKey)2 KeyException (java.security.KeyException)2 ArrayList (java.util.ArrayList)2 SuppressLint (android.annotation.SuppressLint)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1