Class KeyUtils
java.lang.Object
se.swedenconnect.security.credential.utils.KeyUtils
Utility methods for handling public and private keys.
- Author:
- Martin Lindström (martin@idsec.se), Stefan Santesson (stefan@idsec.se)
-
Method Summary
Modifier and TypeMethodDescriptionstatic PrivateKeydecodePrivateKey(byte[] bytes) Decodes a private key in DER, PEM, and unencrypted PKCS#8 formats.static PrivateKeydecodePrivateKey(byte[] bytes, char[] password) Decodes an encrypted private key.static PrivateKeydecodePrivateKey(InputStream stream) Decodes a private key in DER, PEM, and unencrypted PKCS#8 formats.static PrivateKeydecodePrivateKey(InputStream stream, char[] password) Decodes an encrypted private key.static PublicKeydecodePublicKey(byte[] bytes) Decodes a public key in DER or PEM format.static PublicKeydecodePublicKey(InputStream stream) Decodes a public key in DER or PEM format.static booleanisInlinedPem(String location) When configuring the use of credentials and when a key is configured, normally, the location of the key file is given.
-
Method Details
-
isInlinedPem
When configuring the use of credentials and when a key is configured, normally, the location of the key file is given. But we also allow to give the key "inline", i.e., to enter its PEM-encoding. This method can be used to find out whether a location string holds an inlined PEM-encoded key.- Parameters:
location- location configuration setting- Returns:
trueif the given string holds a PEM-encoding andfalseotherwise
-
decodePublicKey
Decodes a public key in DER or PEM format.- Parameters:
bytes- the key bytes- Returns:
- the decoded public key
- Throws:
KeyException- for decoding errors
-
decodePublicKey
Decodes a public key in DER or PEM format.The method does not close the input stream.
- Parameters:
stream- the input stream- Returns:
- the decoded public key
- Throws:
KeyException- for decoding errors
-
decodePrivateKey
Decodes a private key in DER, PEM, and unencrypted PKCS#8 formats.- Parameters:
bytes- the key bytes- Returns:
- the decoded private key
- Throws:
KeyException- for decoding errors
-
decodePrivateKey
@Nonnull public static PrivateKey decodePrivateKey(@Nonnull byte[] bytes, @Nullable char[] password) throws KeyException Decodes an encrypted private key. DER or PEM-encoded PKCS#8 and "OpenSSL" PEM formats are supported.- Parameters:
bytes- the key bytespassword- the password, ifnull, thedecodePrivateKey(byte[])is called- Returns:
- the decoded and decrypted private key
- Throws:
KeyException- for decoding and decryption errors
-
decodePrivateKey
Decodes a private key in DER, PEM, and unencrypted PKCS#8 formats.The method does not close the input stream.
- Parameters:
stream- the input stream- Returns:
- the decoded private key
- Throws:
KeyException- for decoding errors
-
decodePrivateKey
@Nonnull public static PrivateKey decodePrivateKey(@Nonnull InputStream stream, @Nullable char[] password) throws KeyException Decodes an encrypted private key. DER or PEM-encoded PKCS#8 and "OpenSSL" PEM formats are supported.The method does not close the input stream.
- Parameters:
stream- the input streampassword- the password, ifnull, thedecodePrivateKey(InputStream)is called.- Returns:
- the decoded and decrypted private key
- Throws:
KeyException- for decoding and decryption errors
-