Class OpenSSHKeyPairResourceWriter
- java.lang.Object
-
- org.apache.sshd.common.config.keys.writer.openssh.OpenSSHKeyPairResourceWriter
-
- All Implemented Interfaces:
KeyPairResourceWriter<OpenSSHKeyEncryptionContext>
public class OpenSSHKeyPairResourceWriter extends java.lang.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 java.lang.StringDASHESstatic OpenSSHKeyPairResourceWriterINSTANCEstatic intLINE_LENGTHprivate static java.util.regex.PatternVERTICALSPACE
-
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(java.security.KeyPair key, java.lang.String keyType, int blockSize, java.lang.String comment)static byte[]encodePublicKey(java.security.PublicKey key, java.lang.String keyType)static java.lang.StringfirstLine(java.lang.String text)static voidwrite(java.io.OutputStream out, byte[] bytes, int lineLength)static voidwrite(java.io.OutputStream out, java.lang.String s)voidwritePrivateKey(java.security.KeyPair key, java.lang.String comment, OpenSSHKeyEncryptionContext options, java.io.OutputStream out)Writes a serialization of a private key from a givenKeyPairto a givenOutputStream.voidwritePublicKey(java.security.PublicKey key, java.lang.String comment, java.io.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 java.lang.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
-
VERTICALSPACE
private static final java.util.regex.Pattern VERTICALSPACE
-
-
Method Detail
-
writePrivateKey
public void writePrivateKey(java.security.KeyPair key, java.lang.String comment, OpenSSHKeyEncryptionContext options, java.io.OutputStream out) throws java.io.IOException, java.security.GeneralSecurityExceptionDescription 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:
java.io.IOException- if the key cannot be writtenjava.security.GeneralSecurityException- 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(java.security.KeyPair key, java.lang.String keyType, int blockSize, java.lang.String comment) throws java.io.IOException, java.security.GeneralSecurityException- Throws:
java.io.IOExceptionjava.security.GeneralSecurityException
-
encodePublicKey
public static byte[] encodePublicKey(java.security.PublicKey key, java.lang.String keyType) throws java.io.IOException, java.security.GeneralSecurityException- Throws:
java.io.IOExceptionjava.security.GeneralSecurityException
-
write
public static void write(java.io.OutputStream out, byte[] bytes, int lineLength) throws java.io.IOException- Throws:
java.io.IOException
-
writePublicKey
public void writePublicKey(java.security.PublicKey key, java.lang.String comment, java.io.OutputStream out) throws java.io.IOException, java.security.GeneralSecurityExceptionWrites 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:
java.io.IOException- if the key cannot be writtenjava.security.GeneralSecurityException- if the key is unknown
-
firstLine
public static java.lang.String firstLine(java.lang.String text)
-
write
public static void write(java.io.OutputStream out, java.lang.String s) throws java.io.IOException- Throws:
java.io.IOException
-
-