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 classA key encryptor for modern-style OpenSSH private keys using the bcrypt KDF. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final OpenSSHKeyPairResourceWriterstatic final intprivate static final Pattern -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic OpenSSHKeyEncryptionContextstatic byte[]encodePrivateKey(KeyPair key, String keyType, int blockSize, String comment) static byte[]encodePublicKey(PublicKey key, String keyType) static Stringstatic voidwrite(OutputStream out, byte[] bytes, int lineLength) static voidwrite(OutputStream out, String s) voidwritePrivateKey(KeyPair key, String comment, OpenSSHKeyEncryptionContext options, OutputStream out) Writes a serialization of a private key from a givenKeyPairto a givenOutputStream.voidwritePublicKey(PublicKey key, String comment, OutputStream out) Writes a serialization of aPublicKeyto a givenOutputStream.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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:KeyPairResourceWriterWrites a serialization of a private key from a givenKeyPairto a givenOutputStream.- Specified by:
writePrivateKeyin interfaceKeyPairResourceWriter<OpenSSHKeyEncryptionContext>- Parameters:
key- to write the private key ofcomment- to write with the private keyoptions- for writing the key; may benullif no encryption is wanted. The caller is responsible for clearing the options when no longer needed. If the passphrase obtained from the context isnullor an empty/blank string (length zero or containing only whitespace), the key is written unencrypted.out- TheOutputStreamto write to - recommend using aSecureByteArrayOutputStreamin 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:
IOExceptionGeneralSecurityException
-
encodePublicKey
public static byte[] encodePublicKey(PublicKey key, String keyType) throws IOException, GeneralSecurityException - Throws:
IOExceptionGeneralSecurityException
-
write
- Throws:
IOException
-
writePublicKey
public void writePublicKey(PublicKey key, String comment, OutputStream out) throws IOException, GeneralSecurityException Writes a serialization of aPublicKeyto 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:
writePublicKeyin interfaceKeyPairResourceWriter<OpenSSHKeyEncryptionContext>- Parameters:
key- to writecomment- to write with the keyout- TheOutputStreamto write to - recommend using aSecureByteArrayOutputStreamin 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
-