- java.lang.Object
-
- com.zerodeplibs.webpush.key.PrivateKeySources
-
public class PrivateKeySources extends Object
Static factory methods used to create instances of
PrivateKeySource.Examples:The following is an example of commands that uses OpenSSL to generate a file that can be handled with
PrivateKeySources.openssl ecparam -genkey -name prime256v1 -noout -out my-private.pem openssl pkcs8 -in my-private.pem -topk8 -nocrypt -out my-private_pkcs8.pem
If you want to generate in DER format, you can also do as follows.
openssl pkcs8 -in my-private.pem -topk8 -nocrypt -outform der -out my-private_pkcs8.der
The examples of using these files to create
PrivateKeySources are as follows.Path pemPath = new File("my-private_pkcs8.pem").toPath(); Path derPath = new File("my-private_pkcs8.der").toPath(); PrivateKeySource pemSource = PrivateKeySources.ofPEMFile(pemPath); PrivateKeySource derSource = PrivateKeySources.ofDERFile(derPath); byte[] pemBytes = Files.readAllBytes(pemPath); String pemText = new String(pemBytes, StandardCharsets.UTF_8); PrivateKeySource pemSource2 = PrivateKeySources.ofPEMText(pemText); byte[] derBytes = Files.readAllBytes(derPath); PrivateKeySource derSource2 = PrivateKeySources.ofPKCS8Bytes(derBytes);Thread Safety:Instances obtained through a factory method of this class are thread-safe.
- Author:
- Tomoki Sato
- See Also:
PrivateKeySource,PublicKeySource,PublicKeySources
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classPrivateKeySources.PEMFileSourceBuilderThe builder class for creating an instance ofPrivateKeySourcefrom a PEM formatted file.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static PrivateKeySources.PEMFileSourceBuildergetPEMFileSourceBuilder(Path path)Gets a newPrivateKeySources.PEMFileSourceBuilder.static PrivateKeySourceofDERFile(Path path)Creates a newPrivateKeySourcewith the DER file specified by the given path.static PrivateKeySourceofECPrivateKey(ECPrivateKey privateKey)Creates a newPrivateKeySourcethat wraps the givenECPrivateKeyobject.static PrivateKeySourceofPEMFile(Path path)Creates a newPrivateKeySourcewith the PEM formatted file specified by the given path.static PrivateKeySourceofPEMFile(Path path, PEMParser parser)Creates a newPrivateKeySourcewith the PEM formatted file specified by the given path.static PrivateKeySourceofPEMText(String pemText)Creates a newPrivateKeySourcewith the given PEM-encoded text.static PrivateKeySourceofPEMText(String pemText, PEMParser parser)Creates a newPrivateKeySourcewith the given PEM-encoded text and the givenPEMParser.static PrivateKeySourceofPKCS8Bytes(byte[] pkcs8Bytes)Creates a newPrivateKeySourcewith the given octet sequence that is assumed to be encoded according to the PKCS#8 standard.
-
-
-
Method Detail
-
ofPKCS8Bytes
public static PrivateKeySource ofPKCS8Bytes(byte[] pkcs8Bytes)
Creates a newPrivateKeySourcewith the given octet sequence that is assumed to be encoded according to the PKCS#8 standard.- Parameters:
pkcs8Bytes- the octet sequence representing a private key.- Returns:
- a new
PrivateKeySource. - See Also:
PKCS8EncodedKeySpec
-
ofPEMText
public static PrivateKeySource ofPEMText(String pemText)
Creates a newPrivateKeySourcewith the given PEM-encoded text. The underlying octet sequence is assumed to be encoded according to the PKCS#8 standard.The PEM-encoded text is assumed to contain a private key data that starts with '-----BEGIN PRIVATE KEY-----' and ends with '-----END PRIVATE KEY-----'.
- Parameters:
pemText- the PEM-encoded text containing a private key data.- Returns:
- a new
PrivateKeySource. - Throws:
MalformedPEMException- if the given text cannot be parsed as a valid PEM format.- See Also:
PKCS8EncodedKeySpec
-
ofPEMText
public static PrivateKeySource ofPEMText(String pemText, PEMParser parser)
Creates a newPrivateKeySourcewith the given PEM-encoded text and the givenPEMParser. The underlying octet sequence is assumed to be encoded according to the PKCS#8 standard.The PEM-encoded text is parsed by the given
PEMParser.- Parameters:
pemText- the PEM-encoded text representing a private key.parser- the parser used to parse the PEM-encoded text.- Returns:
- a new
PrivateKeySource. - Throws:
MalformedPEMException- if the given text cannot be parsed as a valid PEM format.- See Also:
PKCS8EncodedKeySpec
-
ofPEMFile
public static PrivateKeySource ofPEMFile(Path path) throws IOException
Creates a newPrivateKeySourcewith the PEM formatted file specified by the given path. The underlying octet sequence is assumed to be encoded according to the PKCS#8 standard.The PEM formatted file is assumed to contain a private key data that starts with '-----BEGIN PRIVATE KEY-----' and ends with '-----END PRIVATE KEY-----'.
- Parameters:
path- the path to a PEM formatted file.- Returns:
- a new
PrivateKeySource. - Throws:
IOException- if an I/O error occurs.MalformedPEMException- if the content of the given file cannot be parsed as a valid PEM format.
-
ofPEMFile
public static PrivateKeySource ofPEMFile(Path path, PEMParser parser) throws IOException
Creates a newPrivateKeySourcewith the PEM formatted file specified by the given path. The underlying octet sequence is assumed to be encoded according to the PKCS#8 standard.The content of the PEM file is parsed by the given
PEMParser.- Parameters:
path- the path to a PEM formatted file.parser- a parser used to parse the content of the PEM file.- Returns:
- a new
PrivateKeySource. - Throws:
IOException- if an I/O error occurs.MalformedPEMException- if the content of the given file cannot be parsed as a valid PEM format.
-
ofDERFile
public static PrivateKeySource ofDERFile(Path path) throws IOException
Creates a newPrivateKeySourcewith the DER file specified by the given path. Its octet sequence is assumed to be encoded according to the PKCS#8 standard.- Parameters:
path- the path to a DER file.- Returns:
- a new
PrivateKeySource. - Throws:
IOException- if an I/O error occurs.
-
ofECPrivateKey
public static PrivateKeySource ofECPrivateKey(ECPrivateKey privateKey)
Creates a newPrivateKeySourcethat wraps the givenECPrivateKeyobject.- Parameters:
privateKey- anECPrivateKeyobject.- Returns:
- a new
PrivateKeySource.
-
getPEMFileSourceBuilder
public static PrivateKeySources.PEMFileSourceBuilder getPEMFileSourceBuilder(Path path)
Gets a newPrivateKeySources.PEMFileSourceBuilder.- Parameters:
path- the path to a PEM formatted file.- Returns:
- a new
PrivateKeySources.PEMFileSourceBuilder.
-
-