Skip to content

Commit 1ad448e

Browse files
author
e113028
committed
EncryptionUtils - added support for passing key and certificate bytes along with existing support.
1 parent 50bfc06 commit 1ad448e

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.mastercard.developer</groupId>
88
<artifactId>client-encryption</artifactId>
9-
<version>1.8.2</version>
9+
<version>1.8.3</version>
1010
<packaging>jar</packaging>
1111
<description>Library for Mastercard API compliant payload encryption/decryption</description>
1212
<url>https://github.com/Mastercard/client-encryption-java</url>

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.mastercard.developer.utils;
22

3-
import java.io.FileInputStream;
4-
import java.io.FileNotFoundException;
3+
import java.io.ByteArrayInputStream;
54
import java.io.IOException;
65
import java.nio.charset.StandardCharsets;
76
import 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

Comments
 (0)