use of com.jcraft.jsch.KeyPair in project che by eclipse.
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;
}
use of com.jcraft.jsch.KeyPair in project KeyBox by skavanagh.
the class AuthKeysAction method generateUserKey.
/**
* generates public private key from passphrase
*
* @param username username to set in public key comment
* @param keyname keyname to set in public key comment
* @return public key
*/
public String generateUserKey(String username, String keyname) {
//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;
}
JSch jsch = new JSch();
String pubKey = null;
try {
KeyPair keyPair = KeyPair.genKeyPair(jsch, type, SSHUtil.KEY_LENGTH);
OutputStream os = new ByteArrayOutputStream();
keyPair.writePrivateKey(os, publicKey.getPassphrase().getBytes());
//set private key
servletRequest.getSession().setAttribute(PVT_KEY, EncryptionUtil.encrypt(os.toString()));
os = new ByteArrayOutputStream();
keyPair.writePublicKey(os, username + "@" + keyname);
pubKey = os.toString();
keyPair.dispose();
} catch (Exception ex) {
log.error(ex.toString(), ex);
}
return pubKey;
}
use of com.jcraft.jsch.KeyPair in project MGit by maks.
the class PrivateKeyUtils method getPublicKeyEnsure.
public static File getPublicKeyEnsure(File privateKey) {
File publicKey = getPublicKey(privateKey);
if (!publicKey.exists()) {
try {
JSch jsch = new JSch();
KeyPair kpair = KeyPair.load(jsch, privateKey.getAbsolutePath());
kpair.writePublicKey(new FileOutputStream(publicKey), "mgit");
kpair.dispose();
} catch (Exception e) {
// TODO
e.printStackTrace();
}
}
return publicKey;
}
use of com.jcraft.jsch.KeyPair in project MGit by maks.
the class SGitSessionFactory method createDefaultJSch.
@Override
protected JSch createDefaultJSch(FS fs) throws JSchException {
JSch jsch = new JSch();
PrivateKeyUtils.migratePrivateKeys();
File sshDir = PrivateKeyUtils.getPrivateKeyFolder();
for (File file : sshDir.listFiles()) {
KeyPair kpair = KeyPair.load(jsch, file.getAbsolutePath());
jsch.addIdentity(file.getAbsolutePath());
}
return jsch;
}
use of com.jcraft.jsch.KeyPair in project airavata by apache.
the class SSHCredentialGenerator method generateCredential.
/**
* @return a SSH Credential generated and encrypted using a randomly generated password
* @throws CredentialStoreException
*/
public SSHCredential generateCredential(String tokenId) throws CredentialStoreException {
JSch jsch = new JSch();
try {
KeyPair kpair = KeyPair.genKeyPair(jsch, KeyPair.RSA);
File file;
file = File.createTempFile("id_rsa", "");
String fileName = file.getAbsolutePath();
String password = generateRandomString();
// We are encrypting the private key with the hash of (tokenId+password).
// Any client which wants to use this private key will also generate a hash and then use it to decrypt the key.
kpair.writePrivateKey(fileName, password.getBytes());
kpair.writePublicKey(fileName + ".pub", "");
kpair.dispose();
byte[] priKey = FileUtils.readFileToByteArray(new File(fileName));
byte[] pubKey = FileUtils.readFileToByteArray(new File(fileName + ".pub"));
SSHCredential sshCredential = new SSHCredential();
sshCredential.setPrivateKey(priKey);
sshCredential.setPublicKey(pubKey);
sshCredential.setPassphrase(password);
return sshCredential;
} catch (IOException e) {
logger.error("IO Exception when creating SSH credential ", e);
throw new CredentialStoreException("Unable to generate SSH Credential", e);
} catch (JSchException e) {
logger.error("JSch SSH credential creation exception ", e);
throw new CredentialStoreException("Unable to generate SSH Credential. JSch exception ", e);
}
}
Aggregations