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>
AKeyPairResourceWriterfor 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 classOpenSSHKeyPairResourceWriter.KeyEncryptorA key encryptor for modern-style OpenSSH private keys using the bcrypt KDF.
-
Field Summary
Fields Modifier and Type Field Description static StringDASHESstatic OpenSSHKeyPairResourceWriterINSTANCEstatic intLINE_LENGTH
-
Constructor Summary
Constructors Constructor Description OpenSSHKeyPairResourceWriter()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static OpenSSHKeyEncryptionContextdetermineEncryption(OpenSSHKeyEncryptionContext options)static byte[]encodePrivateKey(KeyPair key, String keyType, int blockSize, String comment)static byte[]encodePublicKey(PublicKey key, String keyType)static StringfirstLine(String text)static 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, 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: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
public static OpenSSHKeyEncryptionContext determineEncryption(OpenSSHKeyEncryptionContext options)
-
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
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 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
-
write
public static void write(OutputStream out, String s) throws IOException
- Throws:
IOException
-
-