Class OpenSSHKeyPairResourceWriter
- java.lang.Object
-
- org.apache.sshd.common.config.keys.writer.openssh.OpenSSHKeyPairResourceWriter
-
- All Implemented Interfaces:
KeyPairResourceWriter<OpenSSHKeyEncryptionContext>
public class OpenSSHKeyPairResourceWriter extends Object implements KeyPairResourceWriter<OpenSSHKeyEncryptionContext>
AKeyPairResourceWriter
for writing keys in the modern OpenSSH format, using the OpenBSD bcrypt KDF for passphrase-protected encrypted private keys.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
OpenSSHKeyPairResourceWriter.KeyEncryptor
A key encryptor for modern-style OpenSSH private keys using the bcrypt KDF.
-
Field Summary
Fields Modifier and Type Field Description static String
DASHES
static OpenSSHKeyPairResourceWriter
INSTANCE
static int
LINE_LENGTH
-
Constructor Summary
Constructors Constructor Description OpenSSHKeyPairResourceWriter()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static OpenSSHKeyEncryptionContext
determineEncryption(OpenSSHKeyEncryptionContext options)
static byte[]
encodePrivateKey(KeyPair key, String keyType, int blockSize, String comment)
static byte[]
encodePublicKey(PublicKey key, String keyType)
static String
firstLine(String text)
static void
write(OutputStream out, byte[] bytes, int lineLength)
static void
write(OutputStream out, String s)
void
writePrivateKey(KeyPair key, String comment, OpenSSHKeyEncryptionContext options, OutputStream out)
Writes a serialization of a private key from a givenKeyPair
to a givenOutputStream
.void
writePublicKey(PublicKey key, String comment, OutputStream out)
Writes a serialization of aPublicKey
to a givenOutputStream
.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.sshd.common.config.keys.writer.KeyPairResourceWriter
writePublicKey
-
-
-
-
Field Detail
-
DASHES
public static final String DASHES
- See Also:
- Constant Field Values
-
LINE_LENGTH
public static final int LINE_LENGTH
- See Also:
- Constant Field Values
-
INSTANCE
public static final OpenSSHKeyPairResourceWriter INSTANCE
-
-
Method Detail
-
writePrivateKey
public void writePrivateKey(KeyPair key, String comment, OpenSSHKeyEncryptionContext options, OutputStream out) throws IOException, GeneralSecurityException
Description copied from interface:KeyPairResourceWriter
Writes a serialization of a private key from a givenKeyPair
to a givenOutputStream
.- Specified by:
writePrivateKey
in interfaceKeyPairResourceWriter<OpenSSHKeyEncryptionContext>
- Parameters:
key
- to write the private key ofcomment
- to write with the private keyoptions
- for writing the key; may benull
if no encryption is wanted. The caller is responsible for clearing the options when no longer needed. If the passphrase obtained from the context isnull
or an empty/blank string (length zero or containing only whitespace), the key is written unencrypted.out
- TheOutputStream
to write to - recommend using aSecureByteArrayOutputStream
in order to reduce sensitive data exposure in memory- Throws:
IOException
- if the key cannot be writtenGeneralSecurityException
- if the key is inconsistent or unknown, or the encryption specified cannot be applied
-
determineEncryption
public static OpenSSHKeyEncryptionContext determineEncryption(OpenSSHKeyEncryptionContext options)
-
encodePrivateKey
public static byte[] encodePrivateKey(KeyPair key, String keyType, int blockSize, String comment) throws IOException, GeneralSecurityException
- Throws:
IOException
GeneralSecurityException
-
encodePublicKey
public static byte[] encodePublicKey(PublicKey key, String keyType) throws IOException, GeneralSecurityException
- Throws:
IOException
GeneralSecurityException
-
write
public static void write(OutputStream out, byte[] bytes, int lineLength) throws IOException
- Throws:
IOException
-
writePublicKey
public void writePublicKey(PublicKey key, String comment, OutputStream out) throws IOException, GeneralSecurityException
Writes a serialization of aPublicKey
to a givenOutputStream
. Writes the public key in the single-line OpenSSH format "key-type pub-key comment" without terminating line ending. If the comment has multiple lines, only the first line is written.- Specified by:
writePublicKey
in interfaceKeyPairResourceWriter<OpenSSHKeyEncryptionContext>
- Parameters:
key
- to writecomment
- to write with the keyout
- TheOutputStream
to write to - recommend using aSecureByteArrayOutputStream
in order to reduce sensitive data exposure in memory- Throws:
IOException
- if the key cannot be writtenGeneralSecurityException
- if the key is unknown
-
write
public static void write(OutputStream out, String s) throws IOException
- Throws:
IOException
-
-