public class OpenSSHKeyPairResourceWriter extends java.lang.Object implements KeyPairResourceWriter<OpenSSHKeyEncryptionContext>
KeyPairResourceWriter
for writing keys in the modern OpenSSH format, using the OpenBSD bcrypt KDF for
passphrase-protected encrypted private keys.Modifier and Type | Class and Description |
---|---|
static class |
OpenSSHKeyPairResourceWriter.KeyEncryptor
A key encryptor for modern-style OpenSSH private keys using the bcrypt KDF.
|
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DASHES |
static OpenSSHKeyPairResourceWriter |
INSTANCE |
static int |
LINE_LENGTH |
private static java.util.regex.Pattern |
VERTICALSPACE |
Constructor and Description |
---|
OpenSSHKeyPairResourceWriter() |
Modifier and Type | Method and Description |
---|---|
static OpenSSHKeyEncryptionContext |
determineEncryption(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.String |
firstLine(java.lang.String text) |
static void |
write(java.io.OutputStream out,
byte[] bytes,
int lineLength) |
static void |
write(java.io.OutputStream out,
java.lang.String s) |
void |
writePrivateKey(java.security.KeyPair key,
java.lang.String comment,
OpenSSHKeyEncryptionContext options,
java.io.OutputStream out)
Writes a serialization of a private key from a given
KeyPair to a given OutputStream . |
void |
writePublicKey(java.security.PublicKey key,
java.lang.String comment,
java.io.OutputStream out)
Writes a serialization of a
PublicKey to a given OutputStream . |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
writePublicKey
public static final java.lang.String DASHES
public static final int LINE_LENGTH
public static final OpenSSHKeyPairResourceWriter INSTANCE
private static final java.util.regex.Pattern VERTICALSPACE
public void writePrivateKey(java.security.KeyPair key, java.lang.String comment, OpenSSHKeyEncryptionContext options, java.io.OutputStream out) throws java.io.IOException, java.security.GeneralSecurityException
KeyPairResourceWriter
KeyPair
to a given OutputStream
.writePrivateKey
in interface KeyPairResourceWriter<OpenSSHKeyEncryptionContext>
key
- to write the private key ofcomment
- to write with the private keyoptions
- for writing the key; may be null
if no encryption is wanted. The caller
is responsible for clearing the options when no longer needed. If the passphrase
obtained from the context is null
or an empty/blank string (length zero
or containing only whitespace), the key is written unencrypted.out
- The OutputStream
to write to - recommend using a
SecureByteArrayOutputStream
in order to reduce sensitive data exposure
in memoryjava.io.IOException
- if the key cannot be writtenjava.security.GeneralSecurityException
- if the key is inconsistent or unknown, or the encryption specified cannot be
appliedpublic static OpenSSHKeyEncryptionContext determineEncryption(OpenSSHKeyEncryptionContext options)
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
java.io.IOException
java.security.GeneralSecurityException
public static byte[] encodePublicKey(java.security.PublicKey key, java.lang.String keyType) throws java.io.IOException, java.security.GeneralSecurityException
java.io.IOException
java.security.GeneralSecurityException
public static void write(java.io.OutputStream out, byte[] bytes, int lineLength) throws java.io.IOException
java.io.IOException
public void writePublicKey(java.security.PublicKey key, java.lang.String comment, java.io.OutputStream out) throws java.io.IOException, java.security.GeneralSecurityException
PublicKey
to a given OutputStream
.
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.writePublicKey
in interface KeyPairResourceWriter<OpenSSHKeyEncryptionContext>
key
- to writecomment
- to write with the keyout
- The OutputStream
to write to - recommend using a
SecureByteArrayOutputStream
in order to reduce sensitive data exposure
in memoryjava.io.IOException
- if the key cannot be writtenjava.security.GeneralSecurityException
- if the key is unknownpublic static java.lang.String firstLine(java.lang.String text)
public static void write(java.io.OutputStream out, java.lang.String s) throws java.io.IOException
java.io.IOException