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>
A
KeyPairResourceWriter
for writing keys in the modern OpenSSH format, using the OpenBSD bcrypt KDF for
passphrase-protected encrypted private keys.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
A key encryptor for modern-style OpenSSH private keys using the bcrypt KDF. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
static final OpenSSHKeyPairResourceWriter
static final int
private static final Pattern
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic OpenSSHKeyEncryptionContext
static byte[]
encodePrivateKey
(KeyPair key, String keyType, int blockSize, String comment) static byte[]
encodePublicKey
(PublicKey key, String keyType) static String
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 Details
-
DASHES
- See Also:
-
LINE_LENGTH
public static final int LINE_LENGTH- See Also:
-
INSTANCE
-
VERTICALSPACE
-
-
Constructor Details
-
OpenSSHKeyPairResourceWriter
public OpenSSHKeyPairResourceWriter()
-
-
Method Details
-
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
-
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
- 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
-
firstLine
-
write
- Throws:
IOException
-