use of com.jcraft.jsch.KeyPair in project KeyBox by skavanagh.
the class SSHUtil method keyGen.
/**
* generates system's public/private key par and returns passphrase
*
* @return passphrase for system generated key
*/
public static String keyGen(String passphrase) throws IOException, JSchException {
FileUtils.forceMkdir(new File(KEY_PATH));
deleteGenSSHKeys();
if (StringUtils.isEmpty(AppConfig.getProperty(PRIVATE_KEY)) || StringUtils.isEmpty(AppConfig.getProperty(PUBLIC_KEY))) {
// set key type
int type = KeyPair.RSA;
if ("dsa".equals(SSHUtil.KEY_TYPE)) {
type = KeyPair.DSA;
} else if ("ecdsa".equals(SSHUtil.KEY_TYPE)) {
type = KeyPair.ECDSA;
} else if ("ed448".equals(SSHUtil.KEY_TYPE)) {
type = KeyPair.ED448;
} else if ("ed25519".equals(SSHUtil.KEY_TYPE)) {
type = KeyPair.ED25519;
}
String comment = "bastillion@global_key";
JSch jsch = new JSch();
KeyPair keyPair = KeyPair.genKeyPair(jsch, type, KEY_LENGTH);
keyPair.writePrivateKey(PVT_KEY, passphrase.getBytes());
keyPair.writePublicKey(PUB_KEY, comment);
System.out.println("Finger print: " + keyPair.getFingerPrint());
keyPair.dispose();
}
return passphrase;
}
use of com.jcraft.jsch.KeyPair in project cdap by cdapio.
the class DefaultSSHContext method generate.
@Override
public SSHKeyPair generate(String user, int bits) throws KeyException {
JSch jsch = new JSch();
try {
KeyPair keyPair = KeyPair.genKeyPair(jsch, KeyPair.RSA, bits);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
keyPair.writePublicKey(bos, user);
SSHPublicKey publicKey = new SSHPublicKey(user, new String(bos.toByteArray(), StandardCharsets.UTF_8));
bos.reset();
keyPair.writePrivateKey(bos);
byte[] privateKey = bos.toByteArray();
return new SSHKeyPair(publicKey, () -> privateKey);
} catch (JSchException e) {
throw new KeyException("Failed to generate ssh key pair", e);
}
}
use of com.jcraft.jsch.KeyPair in project wildfly-core by wildfly.
the class RemoteSshGitRepositoryTestCase method prepareTest.
@Before
public void prepareTest() throws Exception {
remoteRoot = new File("target", "remote").toPath();
Path repoConfigDir = remoteRoot.resolve("configuration");
Files.createDirectories(repoConfigDir);
File baseDir = remoteRoot.toAbsolutePath().toFile();
Path jbossConfigDir = new File(System.getProperty("jboss.home", System.getenv("JBOSS_HOME"))).toPath().resolve("standalone").resolve("configuration");
PathUtil.copyRecursively(jbossConfigDir, repoConfigDir, true);
Path properties = repoConfigDir.resolve("logging.properties");
if (Files.exists(properties)) {
Files.delete(properties);
}
Path jbossAuthDir = new File(System.getProperty("jboss.home", System.getenv("JBOSS_HOME"))).toPath().resolve("standalone").resolve("tmp").resolve("auth");
Files.createDirectories(jbossAuthDir);
File gitDir = new File(baseDir, Constants.DOT_GIT);
if (!gitDir.exists()) {
try (Git git = Git.init().setDirectory(baseDir).setInitialBranch(Constants.MASTER).call()) {
git.add().addFilepattern("configuration").call();
git.commit().setSign(false).setMessage("Repository initialized").call();
}
}
remoteRepository = new FileRepositoryBuilder().setWorkTree(baseDir).setGitDir(gitDir).setup().build();
// Generate new key pair for the server
ByteArrayOutputStream publicHostKey = new ByteArrayOutputStream();
JSch jsch = new JSch();
KeyPair hostKeyPair = KeyPair.genKeyPair(jsch, 2, 2048);
ByteArrayOutputStream hostPrivateKey = new ByteArrayOutputStream();
hostKeyPair.writePrivateKey(hostPrivateKey);
hostPrivateKey.flush();
hostKeyPair.writePublicKey(publicHostKey, "");
sshServer = new SSHServer(EC_USER, SSH_DIR.resolve(EC_PUBKEY), remoteRepository, // create key pair gen
hostPrivateKey.toByteArray());
port = sshServer.start();
// Add new server to known_hosts
KNOWN_HOSTS = SSH_DIR.resolve("known_hosts").toFile();
FileWriter fileWritter = new FileWriter(KNOWN_HOSTS, true);
String knownHostTemplate = "[%s]:" + port + ' ' + publicHostKey.toString(US_ASCII.name()) + "\n";
try (BufferedWriter bw = new BufferedWriter(fileWritter)) {
bw.write(String.format(knownHostTemplate, "127.0.0.1"));
bw.write(String.format(knownHostTemplate, "localhost"));
bw.write(String.format(knownHostTemplate, InetAddress.getLocalHost().getHostName()));
if (System.getenv().containsKey("COMPUTERNAME")) {
bw.write(String.format(knownHostTemplate, System.getenv().get("COMPUTERNAME")));
}
}
}
use of com.jcraft.jsch.KeyPair in project heroku.jar by heroku.
the class KeysRequestIntegrationTest method getPublicKey.
public String getPublicKey(String comment) throws JSchException, IOException {
JSch jsch = new JSch();
KeyPair keyPair = KeyPair.genKeyPair(jsch, KeyPair.RSA);
ByteArrayOutputStream publicKeyOutputStream = new ByteArrayOutputStream();
keyPair.writePublicKey(publicKeyOutputStream, comment);
publicKeyOutputStream.close();
return new String(publicKeyOutputStream.toByteArray());
}
use of com.jcraft.jsch.KeyPair in project che-server by eclipse-che.
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;
}
Aggregations