Skip to content

Commit 1e35af6

Browse files
committed
Fix computeEncryptionKeyFingerprintWhenNeeded not taking in the new encryptionKey value issue
1 parent f09a091 commit 1e35af6

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/main/java/com/mastercard/developer/encryption/EncryptionConfigBuilder.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,22 @@ abstract class EncryptionConfigBuilder {
2525

2626
void computeEncryptionKeyFingerprintWhenNeeded() throws EncryptionException {
2727
try {
28-
if (encryptionCertificate == null || !isNullOrEmpty(encryptionKeyFingerprint)) {
29-
// No encryption certificate set or key fingerprint already provided
28+
if ((encryptionCertificate == null && encryptionKey == null) || !isNullOrEmpty(encryptionKeyFingerprint)) {
29+
// No encryption certificate / encryption key set or key fingerprint already provided
3030
return;
3131
}
32-
byte[] keyFingerprintBytes = sha256digestBytes(encryptionCertificate.getPublicKey().getEncoded());
32+
final PublicKey publicKey;
33+
if (encryptionKey != null && encryptionCertificate != null) {
34+
throw new IllegalArgumentException("You can only supply either an encryption key or an encryption certificate");
35+
}
36+
if (encryptionKey != null) {
37+
publicKey = encryptionKey;
38+
} else if (encryptionCertificate != null) {
39+
publicKey = encryptionCertificate.getPublicKey();
40+
} else {
41+
throw new IllegalArgumentException("You need to supply either one of the encryption key or the encryption certificate");
42+
}
43+
byte[] keyFingerprintBytes = sha256digestBytes(publicKey.getEncoded());
3344
encryptionKeyFingerprint = encodeBytes(keyFingerprintBytes, FieldLevelEncryptionConfig.FieldValueEncoding.HEX);
3445
} catch (Exception e) {
3546
throw new EncryptionException("Failed to compute encryption key fingerprint!", e);

0 commit comments

Comments
 (0)