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 ;
65import java .nio .charset .StandardCharsets ;
76import java .nio .file .Files ;
@@ -32,16 +31,26 @@ private EncryptionUtils() {
3231 /**
3332 * Populate a X509 encryption certificate object with the certificate data at the given file path.
3433 */
35- public static Certificate loadEncryptionCertificate (String certificatePath ) throws CertificateException , FileNotFoundException {
34+ public static Certificate loadEncryptionCertificate (String certificatePath ) throws CertificateException , IOException {
35+ return loadEncryptionCertificate (Files .readAllBytes (Paths .get (certificatePath )));
36+ }
37+
38+ public static Certificate loadEncryptionCertificate (byte [] certificateBytes ) throws CertificateException {
3639 CertificateFactory factory = CertificateFactory .getInstance ("X.509" );
37- return factory .generateCertificate (new FileInputStream ( certificatePath ));
40+ return factory .generateCertificate (new ByteArrayInputStream ( certificateBytes ));
3841 }
3942
4043 /**
4144 * Load a RSA decryption key from a file (PEM or DER).
4245 */
4346 public static PrivateKey loadDecryptionKey (String keyFilePath ) throws GeneralSecurityException , IOException {
44- byte [] keyDataBytes = Files .readAllBytes (Paths .get (keyFilePath ));
47+ return loadDecryptionKey (Files .readAllBytes (Paths .get (keyFilePath )));
48+ }
49+
50+ /**
51+ * Load a RSA decryption key from key data in bytes.
52+ */
53+ public static PrivateKey loadDecryptionKey (byte [] keyDataBytes ) throws GeneralSecurityException {
4554 String keyDataString = new String (keyDataBytes , StandardCharsets .UTF_8 );
4655
4756 if (keyDataString .contains (PKCS_1_PEM_HEADER )) {
@@ -63,17 +72,17 @@ public static PrivateKey loadDecryptionKey(String keyFilePath) throws GeneralSec
6372 }
6473
6574 // We assume it's a PKCS#8 DER encoded binary file
66- return readPkcs8PrivateKey (Files . readAllBytes ( Paths . get ( keyFilePath )) );
75+ return readPkcs8PrivateKey (keyDataBytes );
6776 }
6877
6978 /**
7079 * Load a RSA decryption key out of a PKCS#12 container.
7180 */
7281 public static PrivateKey loadDecryptionKey (String pkcs12KeyFilePath ,
73- String decryptionKeyAlias ,
74- String decryptionKeyPassword ) throws GeneralSecurityException , IOException {
82+ String decryptionKeyAlias ,
83+ String decryptionKeyPassword ) throws GeneralSecurityException , IOException {
7584 KeyStore pkcs12KeyStore = KeyStore .getInstance ("PKCS12" );
76- pkcs12KeyStore .load (new FileInputStream ( pkcs12KeyFilePath ), decryptionKeyPassword .toCharArray ());
85+ pkcs12KeyStore .load (Files . newInputStream ( Paths . get ( pkcs12KeyFilePath ) ), decryptionKeyPassword .toCharArray ());
7786 return (PrivateKey ) pkcs12KeyStore .getKey (decryptionKeyAlias , decryptionKeyPassword .toCharArray ());
7887 }
7988
0 commit comments