X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-ssh%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fssh%2Fauthentication%2FPEMGenerator.java;fp=opendaylight%2Fnetconf%2Fnetconf-ssh%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fssh%2Fauthentication%2FPEMGenerator.java;h=73886c4f461eb3ecd60710da40cf6fc3ff361e54;hb=58bbc881391e1119198fdebedab57c5cf6b5af1d;hp=0000000000000000000000000000000000000000;hpb=7e7bdaec36dd670a10939b57d96d44432291b77d;p=controller.git diff --git a/opendaylight/netconf/netconf-ssh/src/main/java/org/opendaylight/controller/netconf/ssh/authentication/PEMGenerator.java b/opendaylight/netconf/netconf-ssh/src/main/java/org/opendaylight/controller/netconf/ssh/authentication/PEMGenerator.java new file mode 100644 index 0000000000..73886c4f46 --- /dev/null +++ b/opendaylight/netconf/netconf-ssh/src/main/java/org/opendaylight/controller/netconf/ssh/authentication/PEMGenerator.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.netconf.ssh.authentication; + +import org.apache.commons.io.FileUtils; +import org.bouncycastle.openssl.PEMWriter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.IOException; +import java.io.StringWriter; +import java.security.Key; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.SecureRandom; + +public class PEMGenerator { + private static final Logger logger = LoggerFactory.getLogger(PEMGenerator.class); + private static final int KEY_SIZE = 4096; + + public static String generateTo(File privateFile) throws Exception { + KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); + SecureRandom sr = new SecureRandom(); + keyGen.initialize(KEY_SIZE, sr); + KeyPair keypair = keyGen.generateKeyPair(); + logger.info("Generating private key to {}", privateFile.getAbsolutePath()); + String privatePEM = toString(keypair.getPrivate()); + FileUtils.write(privateFile, privatePEM); + return privatePEM; + } + + private static String toString(Key key) throws IOException { + try (StringWriter writer = new StringWriter()) { + try (PEMWriter pemWriter = new PEMWriter(writer)) { + pemWriter.writeObject(key); + } + return writer.toString(); + } + } +}