11package com .mastercard .developer .utils ;
22
3- import java .io .FileInputStream ;
4- import java .io .FileNotFoundException ;
3+ import java .io .ByteArrayInputStream ;
54import java .io .IOException ;
5+ import java .io .InputStream ;
66import java .nio .charset .StandardCharsets ;
77import java .nio .file .Files ;
88import java .nio .file .Paths ;
@@ -32,17 +32,32 @@ private EncryptionUtils() {
3232 /**
3333 * Populate a X509 encryption certificate object with the certificate data at the given file path.
3434 */
35- public static Certificate loadEncryptionCertificate (String certificatePath ) throws CertificateException , FileNotFoundException {
35+ public static Certificate loadEncryptionCertificate (String certificatePath ) throws CertificateException , IOException {
36+ return loadEncryptionCertificate (Files .newInputStream (Paths .get (certificatePath )));
37+ }
38+
39+ /**
40+ * Populate a X509 encryption certificate object with the certificate data at the given certificate data in bytes.
41+ */
42+ public static Certificate loadEncryptionCertificate (InputStream certificateStream ) throws CertificateException {
3643 CertificateFactory factory = CertificateFactory .getInstance ("X.509" );
37- return factory .generateCertificate (new FileInputStream ( certificatePath ) );
44+ return factory .generateCertificate (certificateStream );
3845 }
3946
4047 /**
4148 * Load a RSA decryption key from a file (PEM or DER).
4249 */
4350 public static PrivateKey loadDecryptionKey (String keyFilePath ) throws GeneralSecurityException , IOException {
44- byte [] keyDataBytes = Files .readAllBytes (Paths .get (keyFilePath ));
45- String keyDataString = new String (keyDataBytes , StandardCharsets .UTF_8 );
51+ InputStream keyStream = new ByteArrayInputStream (Files .readAllBytes (Paths .get (keyFilePath )));
52+ return loadDecryptionKey (keyStream );
53+ }
54+
55+ /**
56+ * Load a RSA decryption key from key data in bytes.
57+ */
58+ public static PrivateKey loadDecryptionKey (InputStream keyDataStream ) throws GeneralSecurityException , IOException {
59+ byte [] keyBytes = keyDataStream .readAllBytes ();
60+ String keyDataString = new String (keyBytes , StandardCharsets .UTF_8 );
4661
4762 if (keyDataString .contains (PKCS_1_PEM_HEADER )) {
4863 // OpenSSL / PKCS#1 Base64 PEM encoded file
@@ -63,17 +78,17 @@ public static PrivateKey loadDecryptionKey(String keyFilePath) throws GeneralSec
6378 }
6479
6580 // We assume it's a PKCS#8 DER encoded binary file
66- return readPkcs8PrivateKey (Files . readAllBytes ( Paths . get ( keyFilePath )) );
81+ return readPkcs8PrivateKey (keyBytes );
6782 }
6883
6984 /**
7085 * Load a RSA decryption key out of a PKCS#12 container.
7186 */
7287 public static PrivateKey loadDecryptionKey (String pkcs12KeyFilePath ,
73- String decryptionKeyAlias ,
74- String decryptionKeyPassword ) throws GeneralSecurityException , IOException {
88+ String decryptionKeyAlias ,
89+ String decryptionKeyPassword ) throws GeneralSecurityException , IOException {
7590 KeyStore pkcs12KeyStore = KeyStore .getInstance ("PKCS12" );
76- pkcs12KeyStore .load (new FileInputStream ( pkcs12KeyFilePath ), decryptionKeyPassword .toCharArray ());
91+ pkcs12KeyStore .load (Files . newInputStream ( Paths . get ( pkcs12KeyFilePath ) ), decryptionKeyPassword .toCharArray ());
7792 return (PrivateKey ) pkcs12KeyStore .getKey (decryptionKeyAlias , decryptionKeyPassword .toCharArray ());
7893 }
7994
0 commit comments