use of com.jcraft.jsch.KeyPair in project azure-tools-for-java by Microsoft.
the class AzureDockerCertVaultOps method generateSSHKeys.
public static AzureDockerCertVault generateSSHKeys(String passPhrase, String comment) throws AzureDockerException {
try {
AzureDockerCertVault result = new AzureDockerCertVault();
JSch jsch = new JSch();
KeyPair keyPair = KeyPair.genKeyPair(jsch, KeyPair.RSA);
ByteArrayOutputStream privateKeyBuff = new ByteArrayOutputStream(2048);
ByteArrayOutputStream publicKeyBuff = new ByteArrayOutputStream(2048);
keyPair.writePublicKey(publicKeyBuff, (comment != null) ? comment : "DockerSSHCerts");
if (passPhrase == null || passPhrase.isEmpty()) {
keyPair.writePrivateKey(privateKeyBuff);
} else {
keyPair.writePrivateKey(privateKeyBuff, passPhrase.getBytes());
}
result.sshKey = privateKeyBuff.toString();
result.sshPubKey = publicKeyBuff.toString();
return result;
} catch (Exception e) {
throw new AzureDockerException(e.getMessage());
}
}
use of com.jcraft.jsch.KeyPair in project keepass2android by PhilippC.
the class SftpStorage method createKeyPair.
private String createKeyPair(String key_filename) throws JSchException, IOException {
String public_key_filename = key_filename + ".pub";
File file = new File(key_filename);
if (file.exists())
return public_key_filename;
int type = KeyPair.RSA;
KeyPair kpair = KeyPair.genKeyPair(jsch, type, 4096);
kpair.writePrivateKey(key_filename);
kpair.writePublicKey(public_key_filename, "generated by Keepass2Android");
// ret = "Fingerprint: " + kpair.getFingerPrint();
kpair.dispose();
return public_key_filename;
}
use of com.jcraft.jsch.KeyPair in project MGit by maks.
the class PrivateKeyGenerate method generateKey.
private void generateKey() {
String newFilename = mNewFilename.getText().toString().trim();
if (newFilename.equals("")) {
showToastMessage(R.string.alert_new_filename_required);
mNewFilename.setError(getString(R.string.alert_new_filename_required));
return;
}
if (newFilename.contains("/")) {
showToastMessage(R.string.alert_filename_format);
mNewFilename.setError(getString(R.string.alert_filename_format));
return;
}
int key_size = Integer.parseInt(mKeyLength.getText().toString());
if (key_size < 1024) {
showToastMessage(R.string.alert_too_short_key_size);
mNewFilename.setError(getString(R.string.alert_too_short_key_size));
return;
}
if (key_size > 16384) {
showToastMessage(R.string.alert_too_long_key_size);
mNewFilename.setError(getString(R.string.alert_too_long_key_size));
return;
}
int type = mDSAButton.isChecked() ? KeyPair.DSA : KeyPair.RSA;
File newKey = new File(PrivateKeyUtils.getPrivateKeyFolder(), newFilename);
File newPubKey = new File(PrivateKeyUtils.getPublicKeyFolder(), newFilename);
try {
JSch jsch = new JSch();
KeyPair kpair = KeyPair.genKeyPair(jsch, type, key_size);
kpair.writePrivateKey(new FileOutputStream(newKey));
kpair.writePublicKey(new FileOutputStream(newPubKey), "sgit");
kpair.dispose();
} catch (Exception e) {
// TODO
e.printStackTrace();
}
((PrivateKeyManageActivity) getActivity()).refreshList();
}
use of com.jcraft.jsch.KeyPair in project cdap by caskdata.
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 jcabi-github by jcabi.
the class RtPublicKeysITCase method key.
/**
* Generates a random public key for test.
* @return The encoded SSH public key.
* @throws Exception If a problem occurs.
*/
private String key() throws Exception {
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
final KeyPair kpair = KeyPair.genKeyPair(new JSch(), KeyPair.DSA);
kpair.writePublicKey(stream, "");
kpair.dispose();
} finally {
stream.close();
}
return new String(stream.toByteArray());
}
Aggregations