Skip to content

Commit a5cb49b

Browse files
- Adding private constructors
- Minor cleanup of code
1 parent b556fd1 commit a5cb49b

File tree

18 files changed

+95
-70
lines changed

18 files changed

+95
-70
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,24 @@ public enum Scheme {
2121
/**
2222
* The encryption scheme to be used
2323
*/
24-
protected Scheme scheme = Scheme.LEGACY;
24+
Scheme scheme = Scheme.LEGACY;
2525

2626
/**
2727
* The SHA-256 hex-encoded digest of the key used for encryption (optional, the digest will be
2828
* automatically computed if this field is null or empty).
2929
* Example: "c3f8ef7053c4fb306f7476e7d1956f0aa992ff9dfdd5244b912a1d377ff3a84f"
3030
*/
31-
protected String encryptionKeyFingerprint;
31+
String encryptionKeyFingerprint;
3232

3333
/**
3434
* A certificate object whose public key will be used for encryption.
3535
*/
36-
protected Certificate encryptionCertificate;
36+
Certificate encryptionCertificate;
3737

3838
/**
3939
* A private key object to be used for decryption.
4040
*/
41-
protected PrivateKey decryptionKey;
41+
PrivateKey decryptionKey;
4242

4343
/**
4444
* A list of JSON paths to encrypt in request payloads.
@@ -51,7 +51,7 @@ public enum Scheme {
5151
* }
5252
* </pre>
5353
*/
54-
protected Map<String, String> encryptionPaths = Collections.emptyMap();
54+
Map<String, String> encryptionPaths = Collections.emptyMap();
5555

5656
/**
5757
* A list of JSON paths to decrypt in response payloads.
@@ -64,12 +64,12 @@ public enum Scheme {
6464
* }
6565
* </pre>
6666
*/
67-
protected Map<String, String> decryptionPaths = Collections.emptyMap();
67+
Map<String, String> decryptionPaths = Collections.emptyMap();
6868

6969
/**
7070
* The name of the payload field where to write/read the encrypted data value.
7171
*/
72-
protected String encryptedValueFieldName = null;
72+
String encryptedValueFieldName = null;
7373

7474
public String getEncryptionKeyFingerprint() { return encryptionKeyFingerprint; }
7575

@@ -83,15 +83,15 @@ public PrivateKey getDecryptionKey() {
8383

8484
public Scheme getScheme() { return scheme; }
8585

86-
public Map<String, String> getEncryptionPaths() {
86+
Map<String, String> getEncryptionPaths() {
8787
return encryptionPaths;
8888
}
8989

90-
public Map<String, String> getDecryptionPaths() {
90+
Map<String, String> getDecryptionPaths() {
9191
return decryptionPaths;
9292
}
9393

94-
public String getEncryptedValueFieldName() {
94+
String getEncryptedValueFieldName() {
9595
return encryptedValueFieldName;
9696
}
9797
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
*/
2424
public class FieldLevelEncryption {
2525

26+
private FieldLevelEncryption() {
27+
// Nothing to do here
28+
}
29+
2630
public static String encryptPayload(String payload, FieldLevelEncryptionConfig config) throws EncryptionException {
2731
return encryptPayload(payload, config, null);
2832
}

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,73 +18,73 @@ public enum FieldValueEncoding {
1818
* automatically computed if this field is null or empty).
1919
* Example: "4d9d7540be320429ffc8e6506f054525816e2d0e95a85247d5b58be713f28be0"
2020
*/
21-
protected String encryptionCertificateFingerprint;
21+
String encryptionCertificateFingerprint;
2222

2323
/**
2424
* The digest algorithm to be used for the RSA OAEP padding. Example: "SHA-512".
2525
*/
26-
protected String oaepPaddingDigestAlgorithm = null;
26+
String oaepPaddingDigestAlgorithm = null;
2727

2828
/**
2929
* The name of the payload field where to write/read the digest algorithm used for
3030
* the RSA OAEP padding (optional, the field won't be set if the name is null or empty).
3131
*/
32-
protected String oaepPaddingDigestAlgorithmFieldName = null;
32+
String oaepPaddingDigestAlgorithmFieldName = null;
3333

3434
/**
3535
* The name of the HTTP header where to write/read the digest algorithm used for
3636
* the RSA OAEP padding (optional, the header won't be set if the name is null or empty).
3737
*/
38-
protected String oaepPaddingDigestAlgorithmHeaderName = null;
38+
String oaepPaddingDigestAlgorithmHeaderName = null;
3939

4040
/**
4141
* The name of the payload field where to write/read the initialization vector value.
4242
*/
43-
protected String ivFieldName = null;
43+
String ivFieldName = null;
4444

4545
/**
4646
* The name of the header where to write/read the initialization vector value.
4747
*/
48-
protected String ivHeaderName = null;
48+
String ivHeaderName = null;
4949

5050
/**
5151
* The name of the payload field where to write/read the one-time usage encrypted symmetric key.
5252
*/
53-
protected String encryptedKeyFieldName = null;
53+
String encryptedKeyFieldName = null;
5454

5555
/**
5656
* The name of the header where to write/read the one-time usage encrypted symmetric key.
5757
*/
58-
protected String encryptedKeyHeaderName = null;
58+
String encryptedKeyHeaderName = null;
5959

6060
/**
6161
* The name of the payload field where to write/read the digest of the encryption
6262
* certificate (optional, the field won't be set if the name is null or empty).
6363
*/
64-
protected String encryptionCertificateFingerprintFieldName = null;
64+
String encryptionCertificateFingerprintFieldName = null;
6565

6666
/**
6767
* The name of the header where to write/read the digest of the encryption
6868
* certificate (optional, the header won't be set if the name is null or empty).
6969
*/
70-
protected String encryptionCertificateFingerprintHeaderName = null;
70+
String encryptionCertificateFingerprintHeaderName = null;
7171

7272
/**
7373
* The name of the payload field where to write/read the digest of the encryption
7474
* key (optional, the field won't be set if the name is null or empty).
7575
*/
76-
protected String encryptionKeyFingerprintFieldName = null;
76+
String encryptionKeyFingerprintFieldName = null;
7777

7878
/**
7979
* The name of the header where to write/read the digest of the encryption
8080
* key (optional, the header won't be set if the name is null or empty).
8181
*/
82-
protected String encryptionKeyFingerprintHeaderName = null;
82+
String encryptionKeyFingerprintHeaderName = null;
8383

8484
/**
8585
* How the field/header values have to be encoded.
8686
*/
87-
protected FieldLevelEncryptionConfig.FieldValueEncoding fieldValueEncoding;
87+
FieldLevelEncryptionConfig.FieldValueEncoding fieldValueEncoding;
8888

8989
/**
9090
* If the encryption parameters must be written to/read from HTTP headers.
@@ -96,7 +96,7 @@ public boolean useHttpHeaders() {
9696
/**
9797
* If the encryption parameters must be written to/read from HTTP payloads.
9898
*/
99-
public boolean useHttpPayloads() {
99+
boolean useHttpPayloads() {
100100
return encryptedKeyFieldName != null && ivFieldName != null;
101101
}
102102

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
final class JsonParser {
1414

15+
private JsonParser() {
16+
// Nothing to do here
17+
}
18+
1519
static JsonEngine jsonEngine;
1620
static Configuration jsonPathConfig = withJsonEngine(JsonEngine.getDefault());
1721

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import com.jayway.jsonpath.DocumentContext;
44
import com.jayway.jsonpath.JsonPath;
5-
import com.mastercard.developer.encryption.jwe.JWEHeader;
6-
import com.mastercard.developer.encryption.jwe.JWEObject;
5+
import com.mastercard.developer.encryption.jwe.JweHeader;
6+
import com.mastercard.developer.encryption.jwe.JweObject;
77

88
import java.security.GeneralSecurityException;
99
import java.util.ArrayList;
@@ -15,6 +15,10 @@
1515

1616
public class JweEncryption {
1717

18+
private JweEncryption() {
19+
// Nothing to do here
20+
}
21+
1822
private static final String ALGORITHM = "RSA-OAEP-256";
1923
private static final String ENCRYPTION = "A256GCM";
2024
private static final String CONTENT_TYPE = "application/json";
@@ -65,8 +69,8 @@ private static void encryptPayloadPath(DocumentContext payloadContext, String js
6569
}
6670

6771
String inJsonString = sanitizeJson(jsonEngine.toJsonString(inJsonElement));
68-
JWEHeader myHeader = new JWEHeader(ALGORITHM, ENCRYPTION, config.encryptionKeyFingerprint, CONTENT_TYPE);
69-
String payload = JWEObject.encrypt(config, inJsonString, myHeader);
72+
JweHeader myHeader = new JweHeader(ALGORITHM, ENCRYPTION, config.encryptionKeyFingerprint, CONTENT_TYPE);
73+
String payload = JweObject.encrypt(config, inJsonString, myHeader);
7074

7175
// Delete data in clear
7276
if (!"$".equals(jsonPathIn)) {
@@ -100,7 +104,7 @@ private static void decryptPayloadPath(DocumentContext payloadContext, String js
100104
}
101105

102106
String encryptedValue = jsonEngine.toJsonString(encryptedValueJsonElement).replace("\"", "");
103-
JWEObject jweObject = JWEObject.parse(encryptedValue, jsonEngine);
107+
JweObject jweObject = JweObject.parse(encryptedValue, jsonEngine);
104108
String payload = jweObject.decrypt(config);
105109

106110
// Add decrypted data at the given JSON path

src/main/java/com/mastercard/developer/encryption/aes/AESCBC.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.mastercard.developer.encryption.aes;
22

3-
import com.mastercard.developer.encryption.jwe.JWEObject;
3+
import com.mastercard.developer.encryption.jwe.JweObject;
44
import com.mastercard.developer.utils.EncodingUtils;
55

66
import javax.crypto.Cipher;
@@ -17,7 +17,7 @@ private AESCBC() {
1717

1818
private static final String CYPHER = "AES/CBC/PKCS5Padding";
1919

20-
public static byte[] decrypt(Key secretKey, JWEObject object) throws GeneralSecurityException {
20+
public static byte[] decrypt(Key secretKey, JweObject object) throws GeneralSecurityException {
2121
SecretKeySpec aesKey = new SecretKeySpec(secretKey.getEncoded(), 16, 16, "AES");
2222

2323
byte[] cipherText = EncodingUtils.base64Decode(object.getCipherText());

src/main/java/com/mastercard/developer/encryption/aes/AESEncryption.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
public class AESEncryption {
1212

13+
private AESEncryption() {
14+
// Nothing to do here
15+
}
16+
1317
public static IvParameterSpec generateIv() throws EncryptionException {
1418
try {
1519
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");

src/main/java/com/mastercard/developer/encryption/aes/AESGCM.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.mastercard.developer.encryption.aes;
22

3-
import com.mastercard.developer.encryption.jwe.JWEObject;
3+
import com.mastercard.developer.encryption.jwe.JweObject;
44
import com.mastercard.developer.utils.ByteUtils;
55
import com.mastercard.developer.utils.EncodingUtils;
66

@@ -15,11 +15,12 @@
1515
public class AESGCM {
1616

1717
private AESGCM() {
18+
// Nothing to do here
1819
}
1920

2021
private static final String CYPHER = "AES/GCM/NoPadding";
2122

22-
public static byte[] decrypt(Key cek, JWEObject object) throws GeneralSecurityException {
23+
public static byte[] decrypt(Key cek, JweObject object) throws GeneralSecurityException {
2324
byte[] aad = object.getRawHeader().getBytes(StandardCharsets.US_ASCII);
2425
SecretKey aesKey = new SecretKeySpec(cek.getEncoded(), "AES");
2526
GCMParameterSpec gcmSpec = new GCMParameterSpec(128, EncodingUtils.base64Decode(object.getIv()));

src/main/java/com/mastercard/developer/encryption/jwe/JWEHeader.java renamed to src/main/java/com/mastercard/developer/encryption/jwe/JweHeader.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
import com.mastercard.developer.json.JsonEngine;
55
import com.mastercard.developer.utils.EncodingUtils;
66

7-
public final class JWEHeader {
7+
public final class JweHeader {
88
private final String enc;
99
private final String kid;
1010
private final String alg;
1111
private final String cty;
1212

13-
public JWEHeader(String alg, String enc, String kid, String cty) {
13+
public JweHeader(String alg, String enc, String kid, String cty) {
1414
this.alg = alg;
1515
this.enc = enc;
1616
this.kid = kid;
@@ -36,14 +36,14 @@ String toJson() {
3636
return engine.toJsonString(obj);
3737
}
3838

39-
static JWEHeader parseJweHeader(String encodedHeader, JsonEngine jsonEngine) {
39+
static JweHeader parseJweHeader(String encodedHeader, JsonEngine jsonEngine) {
4040
Object headerObj = jsonEngine.parse(new String(EncodingUtils.base64Decode(encodedHeader)));
4141
JsonProvider jsonProvider = jsonEngine.getJsonProvider();
4242
String alg = jsonProvider.getMapValue(headerObj, "alg").toString();
4343
String enc = jsonProvider.getMapValue(headerObj, "enc").toString();
4444
String kid = jsonProvider.getMapValue(headerObj, "kid").toString();
4545
Object cty = jsonProvider.getMapValue(headerObj, "cty");
46-
return new JWEHeader(alg, enc, kid, cty != null ? cty.toString() : null);
46+
return new JweHeader(alg, enc, kid, cty != null ? cty.toString() : null);
4747
}
4848

4949
String getEnc() { return enc; }

src/main/java/com/mastercard/developer/encryption/jwe/JWEObject.java renamed to src/main/java/com/mastercard/developer/encryption/jwe/JweObject.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import java.security.Key;
2020
import java.util.Base64;
2121

22-
public class JWEObject {
23-
private final JWEHeader header;
22+
public class JweObject {
23+
private final JweHeader header;
2424
private final String rawHeader;
2525
private final String encryptedKey;
2626
private final String iv;
@@ -30,7 +30,7 @@ public class JWEObject {
3030
private static final String A128CBC_HS256 = "A128CBC-HS256";
3131
private static final String A256GCM = "A256GCM";
3232

33-
private JWEObject(JWEHeader header, String rawHeader, String encryptedKey, String iv, String cipherText, String authTag) {
33+
private JweObject(JweHeader header, String rawHeader, String encryptedKey, String iv, String cipherText, String authTag) {
3434
this.header = header;
3535
this.rawHeader = rawHeader;
3636
this.encryptedKey = encryptedKey;
@@ -56,7 +56,7 @@ public String decrypt(JweConfig config) throws EncryptionException, GeneralSecur
5656
return new String(plainText);
5757
}
5858

59-
public static String encrypt(JweConfig config, String payload, JWEHeader header) throws EncryptionException, GeneralSecurityException {
59+
public static String encrypt(JweConfig config, String payload, JweHeader header) throws EncryptionException, GeneralSecurityException {
6060
SecretKeySpec cek = AESEncryption.generateCek(256);
6161
byte[] encryptedSecretKeyBytes = RSA.wrapSecretKey(config.getEncryptionCertificate().getPublicKey(), cek, "SHA-256");
6262
String encryptedKey = base64Encode(encryptedSecretKeyBytes);
@@ -82,34 +82,32 @@ public static String encrypt(JweConfig config, String payload, JWEHeader header)
8282
}
8383

8484
private static String serialize(String header, String encryptedKey, String iv, String cipherText, String authTag) {
85-
StringBuilder sb = new StringBuilder(header);
86-
sb.append('.');
87-
sb.append(encryptedKey);
88-
sb.append('.');
89-
sb.append(iv);
90-
sb.append('.');
91-
sb.append(cipherText);
92-
sb.append('.');
93-
sb.append(authTag);
94-
return sb.toString();
85+
return header + '.' +
86+
encryptedKey +
87+
'.' +
88+
iv +
89+
'.' +
90+
cipherText +
91+
'.' +
92+
authTag;
9593
}
9694

9795
private static String base64Encode(byte[] bytes) {
9896
return EncodingUtils.encodeBytes(bytes, FieldLevelEncryptionConfig.FieldValueEncoding.BASE64);
9997
}
10098

101-
public static JWEObject parse(String encryptedPayload, JsonEngine jsonEngine) {
99+
public static JweObject parse(String encryptedPayload, JsonEngine jsonEngine) {
102100
String t = encryptedPayload.trim();
103101
int dot1 = t.indexOf('.');
104102
int dot2 = t.indexOf('.', dot1 + 1);
105103
int dot3 = t.indexOf('.', dot2 + 1);
106104
int dot4 = t.indexOf('.', dot3 + 1);
107-
JWEHeader header = JWEHeader.parseJweHeader(t.substring(0, dot1), jsonEngine);
105+
JweHeader header = JweHeader.parseJweHeader(t.substring(0, dot1), jsonEngine);
108106

109-
return new JWEObject(header, t.substring(0, dot1), t.substring(dot1 + 1, dot2), t.substring(dot2 + 1, dot3), t.substring(dot3 + 1, dot4), t.substring(dot4 + 1));
107+
return new JweObject(header, t.substring(0, dot1), t.substring(dot1 + 1, dot2), t.substring(dot2 + 1, dot3), t.substring(dot3 + 1, dot4), t.substring(dot4 + 1));
110108
}
111109

112-
public JWEHeader getHeader() {
110+
public JweHeader getHeader() {
113111
return header;
114112
}
115113

0 commit comments

Comments
 (0)