22
33import java .io .ByteArrayInputStream ;
44import java .io .IOException ;
5+ import java .io .InputStream ;
56import java .nio .charset .StandardCharsets ;
67import java .nio .file .Files ;
78import java .nio .file .Paths ;
@@ -32,29 +33,31 @@ private EncryptionUtils() {
3233 * Populate a X509 encryption certificate object with the certificate data at the given file path.
3334 */
3435 public static Certificate loadEncryptionCertificate (String certificatePath ) throws CertificateException , IOException {
35- return loadEncryptionCertificate (Files .readAllBytes (Paths .get (certificatePath )));
36+ return loadEncryptionCertificate (Files .newInputStream (Paths .get (certificatePath )));
3637 }
3738
3839 /**
3940 * Populate a X509 encryption certificate object with the certificate data at the given certificate data in bytes.
4041 */
41- public static Certificate loadEncryptionCertificate (byte [] certificateBytes ) throws CertificateException {
42+ public static Certificate loadEncryptionCertificate (InputStream certificateStream ) throws CertificateException {
4243 CertificateFactory factory = CertificateFactory .getInstance ("X.509" );
43- return factory .generateCertificate (new ByteArrayInputStream ( certificateBytes ) );
44+ return factory .generateCertificate (certificateStream );
4445 }
4546
4647 /**
4748 * Load a RSA decryption key from a file (PEM or DER).
4849 */
4950 public static PrivateKey loadDecryptionKey (String keyFilePath ) throws GeneralSecurityException , IOException {
50- return loadDecryptionKey (Files .readAllBytes (Paths .get (keyFilePath )));
51+ InputStream keyStream = new ByteArrayInputStream (Files .readAllBytes (Paths .get (keyFilePath )));
52+ return loadDecryptionKey (keyStream );
5153 }
5254
5355 /**
5456 * Load a RSA decryption key from key data in bytes.
5557 */
56- public static PrivateKey loadDecryptionKey (byte [] keyDataBytes ) throws GeneralSecurityException {
57- String keyDataString = new String (keyDataBytes , StandardCharsets .UTF_8 );
58+ public static PrivateKey loadDecryptionKey (InputStream keyDataStream ) throws GeneralSecurityException , IOException {
59+ byte [] keyBytes = keyDataStream .readAllBytes ();
60+ String keyDataString = new String (keyBytes , StandardCharsets .UTF_8 );
5861
5962 if (keyDataString .contains (PKCS_1_PEM_HEADER )) {
6063 // OpenSSL / PKCS#1 Base64 PEM encoded file
@@ -75,7 +78,7 @@ public static PrivateKey loadDecryptionKey(byte[] keyDataBytes) throws GeneralSe
7578 }
7679
7780 // We assume it's a PKCS#8 DER encoded binary file
78- return readPkcs8PrivateKey (keyDataBytes );
81+ return readPkcs8PrivateKey (keyBytes );
7982 }
8083
8184 /**
0 commit comments