2929import java .security .spec .X509EncodedKeySpec ;
3030import java .util .Base64 ;
3131
32+ /**
33+ * A class responsible for loading RSA keys from PEM formatted text.
34+ * <p>
35+ * This class implements the {@link KeyLoader} interface and provides methods to load both private
36+ * and public RSA keys. The keys are expected to be in the standard PEM format, which includes
37+ * Base64-encoded key content surrounded by header and footer lines. The class handles the decoding
38+ * of Base64 content and the generation of keys using the RSA key factory.
39+ * <p>
40+ * Any exceptions encountered during the loading process are encapsulated in a
41+ * {@link KeyLoadingException}, allowing for flexible error handling.
42+ *
43+ * @author siujamo
44+ * @see KeyLoader
45+ * @see KeyLoadingException
46+ */
3247public class RsaKeyLoader implements KeyLoader {
3348
3449 private final Base64 .Decoder decoder ;
3550 private final KeyFactory keyFactory ;
3651
52+ /**
53+ * Constructs an instance of {@code RsaKeyLoader}.
54+ * <p>
55+ * This constructor initialises the Base64 decoder and the RSA {@link KeyFactory}. It may throw
56+ * a {@link KeyLoadingException} if the RSA algorithm is not available.
57+ */
3758 public RsaKeyLoader () {
3859 try {
3960 this .decoder = Base64 .getDecoder ();
@@ -43,6 +64,17 @@ public RsaKeyLoader() {
4364 }
4465 }
4566
67+ /**
68+ * Loads an RSA private key from a given PEM formatted key text.
69+ * <p>
70+ * This method extracts the raw key content from the provided PEM text, decodes the
71+ * Base64-encoded content, and generates an instance of {@link RSAPrivateKey}. If the key cannot
72+ * be loaded due to invalid specifications or types, a {@link KeyLoadingException} is thrown.
73+ *
74+ * @param pemKeyText the PEM formatted private key text
75+ * @return an instance of {@link RSAPrivateKey}
76+ * @throws KeyLoadingException if the key loading process encounters an error
77+ */
4678 @ Override
4779 public RSAPrivateKey loadPrivateKey (String pemKeyText ) {
4880 // Extract the raw key content
@@ -67,6 +99,17 @@ public RSAPrivateKey loadPrivateKey(String pemKeyText) {
6799 }
68100 }
69101
102+ /**
103+ * Loads an RSA public key from a given PEM formatted key text.
104+ * <p>
105+ * This method extracts the raw key content from the provided PEM text, decodes the
106+ * Base64-encoded content, and generates an instance of {@link RSAPublicKey}. If the key cannot
107+ * be loaded due to invalid specifications or types, a {@link KeyLoadingException} is thrown.
108+ *
109+ * @param pemKeyText the PEM formatted public key text
110+ * @return an instance of {@link RSAPublicKey}
111+ * @throws KeyLoadingException if the key loading process encounters an error
112+ */
70113 @ Override
71114 public RSAPublicKey loadPublicKey (String pemKeyText ) {
72115 // Extract the raw key content
0 commit comments