private static List<String> getPublicKeys() throws Exception { return new JschConfigSessionFactory() { @Override protected void configure(OpenSshConfig.Host hc, Session session) { } List<String> getPublicKeys() throws Exception { JSch jSch = createDefaultJSch(FS.DETECTED); List<String> keys = new ArrayList<>(); for (Object o : jSch.getIdentityRepository().getIdentities()) { Identity i = (Identity) o; KeyPair keyPair = KeyPair.load(jSch, i.getName(), null); StringBuilder sb = new StringBuilder(); try (StringBuilderWriter sbw = new StringBuilderWriter(sb); OutputStream os = new WriterOutputStream(sbw, "UTF-8")) { keyPair.writePublicKey(os, keyPair.getPublicKeyComment()); } finally { keyPair.dispose(); } keys.add(sb.toString().trim()); } return keys; } }.getPublicKeys(); }
private Identity getId(final JSch jsch, final String name) { final Iterator<?> it = jsch.getIdentityRepository().getIdentities().iterator(); while (it.hasNext()) { final Identity id = (Identity) it.next(); if (id.getName().contains(name)) { return id; } } throw new AssertionFailedError(format("No identity found with name %s", name)); }
/** * */ @Test public void createDefaultJSch() throws Exception { final SshAgentSessionFactory factory = new SshAgentSessionFactory(log, null, TESTID, null); factory.setIdentityFile(getFile(TESTID)); final JSch jsch = factory.createDefaultJSch(fs); final Identity id = getId(jsch, TESTID); assertFalse(id.isEncrypted()); }
/** * */ @Test public void createDefaultJSch_WithPassword() throws Exception { final SshAgentSessionFactory factory = new SshAgentSessionFactory(log, null, TESTID_WITH_PASSWORD, null); factory.setIdentityFile(getFile(TESTID_WITH_PASSWORD)); JSch jsch = factory.createDefaultJSch(fs); Identity id = getId(jsch, TESTID_WITH_PASSWORD); assertTrue(id.isEncrypted()); factory.setPassphrase("password"); jsch = factory.createDefaultJSch(fs); id = getId(jsch, TESTID_WITH_PASSWORD); assertFalse(id.isEncrypted()); }
public static Keychain getIdentities(JSch jSch) { ArrayList<JschKey> identities = new ArrayList<JschKey>(); Vector _identities = jSch.getIdentityRepository().getIdentities(); if (_identities != null) { for (Object obj : _identities) { identities.add(new JschKey((Identity) obj)); } } return new DefaultKeychain(Collections.unmodifiableList(identities)); }
@Override public Vector<Identity> getIdentities() { final Vector<Identity> identities = new Vector<>(); identities.add(identity); return identities; }
public JschKey(Identity identity) { this.fingerprint = Magic.getFingerprint(identity.getPublicKeyBlob()); this.identity = identity; this.algorithm = Algorithm.forName(identity.getAlgName()); }