Skip to content

Commit 3d5080b

Browse files
author
e113028
committed
Updated loadDecryptionKey and loadEncryptionCertificate to support InputStream
1 parent dd0b0cb commit 3d5080b

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/main/java/com/mastercard/developer/utils/EncryptionUtils.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.ByteArrayInputStream;
44
import java.io.IOException;
5+
import java.io.InputStream;
56
import java.nio.charset.StandardCharsets;
67
import java.nio.file.Files;
78
import 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
/**

src/test/java/com/mastercard/developer/utils/EncryptionUtilsTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.junit.jupiter.params.provider.CsvSource;
77
import org.junit.rules.ExpectedException;
88

9+
import java.io.InputStream;
910
import java.nio.file.Files;
1011
import java.nio.file.NoSuchFileException;
1112
import java.nio.file.Paths;
@@ -79,10 +80,10 @@ void testLoadDecryptionKey_ShouldSupportPkcs8Der(String keyLocation, String expe
7980
void testLoadDecryptionKey_ShouldSupportPkcs8DerBytes(String keyLocation, String expectedEncoding) throws Exception {
8081

8182
// GIVEN
82-
byte[] keyBytes = Files.readAllBytes(Paths.get(String.format("./src/test/resources/keys/%s", keyLocation)));
83+
InputStream keyStream = Files.newInputStream(Paths.get(String.format("./src/test/resources/keys/%s", keyLocation)));
8384

8485
// WHEN
85-
PrivateKey privateKey = EncryptionUtils.loadDecryptionKey(keyBytes);
86+
PrivateKey privateKey = EncryptionUtils.loadDecryptionKey(keyStream);
8687

8788
// THEN
8889
assertNotNull(privateKey);
@@ -118,10 +119,10 @@ void testLoadDecryptionKey_ShouldSupportPkcs1Base64Pem_512bits(String fileName)
118119
void testLoadDecryptionKey_ShouldSupportPkcs1Base64PemBytes_512bits(String fileName) throws Exception {
119120

120121
// GIVEN
121-
byte[] keyBytes = Files.readAllBytes(Paths.get(String.format("./src/test/resources/keys/pkcs1/%s", fileName)));
122+
InputStream keyStream = Files.newInputStream(Paths.get(String.format("./src/test/resources/keys/pkcs1/%s", fileName)));
122123

123124
// WHEN
124-
PrivateKey privateKey = EncryptionUtils.loadDecryptionKey(keyBytes);
125+
PrivateKey privateKey = EncryptionUtils.loadDecryptionKey(keyStream);
125126

126127
// THEN
127128
assertNotNull(privateKey);

0 commit comments

Comments
 (0)