final JSch jsch=new JSch();
final KeyPair kpair;
try {
kpair = KeyPair.genKeyPair(jsch, this.keyType, this.keySize);
} catch (JSchException e) {
throw new KeyGenException(e.getMessage(), e);
}
final String generatedFingerprint = kpair.getFingerPrint();
if (generatedFingerprint == null) {
throw new KeyGenException("fingerprint is missing");
}
final String[] parts = generatedFingerprint.split(" ");
if (parts.length != 2) {
throw new KeyGenException("fingerprint not in expected " +
"format: '" + generatedFingerprint + "'");
}
final String fingerprint = parts[1];
if (fingerprint == null || fingerprint.trim().length() == 0) {
throw new KeyGenException("fingerprint not in expected " +
"format: '" + generatedFingerprint + "'");
}
final StringOutputStream pubsos = new StringOutputStream();
final StringOutputStream privsos = new StringOutputStream();
kpair.writePublicKey(pubsos, "clouduser-" + ownerID);
kpair.writePrivateKey(privsos);
final String pubKeyString = pubsos.toString();
if (pubKeyString == null || pubKeyString.trim().length() == 0) {
throw new KeyGenException("generated pubkey is missing");
}
final String privKeyString = privsos.toString();
if (privKeyString == null || privKeyString.trim().length() == 0) {
throw new KeyGenException("generated privkey is missing");
}
// register it before returning
this.sshKeys.newKey(ownerID, keyName, pubKeyString, fingerprint);