Example usage for org.apache.commons.ssl PEMUtil encode

List of usage examples for org.apache.commons.ssl PEMUtil encode

Introduction

In this page you can find the example usage for org.apache.commons.ssl PEMUtil encode.

Prototype

public static byte[] encode(Collection items) throws IOException 

Source Link

Usage

From source file:org.apache.karaf.shell.ssh.keygenerator.PemWriter.java

public void writeKeyPair(String resource, KeyPair kp) throws IOException, FileNotFoundException {
    Collection<Object> items = new ArrayList<>();
    items.add(new PEMItem(kp.getPrivate().getEncoded(), "PRIVATE KEY"));
    byte[] bytes = PEMUtil.encode(items);
    try (FileOutputStream os = new FileOutputStream(keyFile)) {
        os.write(bytes);/*from ww  w  . ja  va2 s.  com*/
    }
}

From source file:org.apache.karaf.shell.ssh.OpenSSHGeneratorFileKeyProvider.java

@Override
protected void doWriteKeyPair(String resourceKey, KeyPair kp, OutputStream os)
        throws IOException, GeneralSecurityException {
    Collection<Object> items = new ArrayList<>();
    items.add(kp.getPrivate());//  ww  w .j  av a  2 s . c  o  m
    items.add(kp.getPublic());
    byte[] bytes = PEMUtil.encode(items);
    os.write(bytes);
}

From source file:org.apache.sshd.common.config.keys.loader.pem.PKCS8PEMResourceKeyPairParserTest.java

@Test // see SSHD-760
public void testPkcs8() throws IOException, GeneralSecurityException {
    KeyPairGenerator generator = SecurityUtils.getKeyPairGenerator(algorithm);
    if (keySize > 0) {
        generator.initialize(keySize);/*from  ww  w . ja  v  a 2 s.c  o  m*/
    }
    KeyPair kp = generator.generateKeyPair();

    try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
        Collection<Object> items = new ArrayList<>();
        PrivateKey prv1 = kp.getPrivate();
        items.add(new PEMItem(prv1.getEncoded(), "PRIVATE KEY"));
        byte[] bytes = PEMUtil.encode(items);
        os.write(bytes);
        os.close();

        try (ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) {
            KeyPair kp2 = SecurityUtils.loadKeyPairIdentity(getCurrentTestName(), bais, null);

            assertEquals("Mismatched public key", kp.getPublic(), kp2.getPublic());
            assertEquals("Mismatched private key", prv1, kp2.getPrivate());
        }
    }
}