Skip to content

Commit 36890ae

Browse files
committed
chore: adjust api
1 parent 12985f9 commit 36890ae

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

dotenvx-spring-boot/src/main/java/org/mvnsearch/dotenvx/spring/configuration/DotenvxEncryptorBuilder.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.mvnsearch.dotenvx.spring.encryptor.DotenvxEncryptorImpl;
55
import org.springframework.lang.Nullable;
66

7+
import java.util.HashMap;
8+
79
/**
810
* <p>DotenvxEncryptorBuilder class.</p>
911
*
@@ -12,8 +14,11 @@
1214
*/
1315
public class DotenvxEncryptorBuilder {
1416

17+
@Nullable
1518
private String publicKeyHex;
19+
@Nullable
1620
private String privateKeyHex;
21+
private HashMap<String, String> profileKeyPairs = new HashMap<>();
1722

1823
/**
1924
* <p>Constructor for DotenvxEncryptorBuilder.</p>
@@ -26,14 +31,18 @@ public DotenvxEncryptorBuilder(@Nullable String publicKeyHex, @Nullable String p
2631
this.privateKeyHex = privateKeyHex;
2732
}
2833

34+
public DotenvxEncryptorBuilder() {
35+
}
36+
2937
/**
3038
* set public key hex
3139
*
3240
* @param publicKeyHex public key hex
3341
* @return this builder
3442
*/
35-
public DotenvxEncryptorBuilder setPublicKeyHex(@Nullable String publicKeyHex) {
43+
public DotenvxEncryptorBuilder withPrimaryKeyPair(@Nullable String publicKeyHex, @Nullable String privateKeyHex) {
3644
this.publicKeyHex = publicKeyHex;
45+
this.privateKeyHex = privateKeyHex;
3746
return this;
3847
}
3948

@@ -43,8 +52,10 @@ public DotenvxEncryptorBuilder setPublicKeyHex(@Nullable String publicKeyHex) {
4352
* @param privateKeyHex private key hex
4453
* @return this builder
4554
*/
46-
public DotenvxEncryptorBuilder setPrivateKeyHex(@Nullable String privateKeyHex) {
47-
this.privateKeyHex = privateKeyHex;
55+
public DotenvxEncryptorBuilder withProfileKeyPair(@Nullable String publicKeyHex, @Nullable String privateKeyHex) {
56+
if (publicKeyHex != null && privateKeyHex != null) {
57+
this.profileKeyPairs.put(publicKeyHex, privateKeyHex);
58+
}
4859
return this;
4960
}
5061

@@ -54,6 +65,6 @@ public DotenvxEncryptorBuilder setPrivateKeyHex(@Nullable String privateKeyHex)
5465
* @return a {@link DotenvxEncryptor} object
5566
*/
5667
public DotenvxEncryptor build() {
57-
return new DotenvxEncryptorImpl(publicKeyHex, privateKeyHex);
68+
return new DotenvxEncryptorImpl(publicKeyHex, privateKeyHex, profileKeyPairs);
5869
}
5970
}

dotenvx-spring-boot/src/main/java/org/mvnsearch/dotenvx/spring/encryptor/DotenvxEncryptorImpl.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import org.mvnsearch.dotenvx.spring.exception.EncryptionException;
66
import org.springframework.lang.Nullable;
77

8+
import java.util.HashMap;
9+
import java.util.Map;
10+
811
/**
912
* dotenvx encryptor implementation
1013
*
@@ -13,10 +16,12 @@
1316
public class DotenvxEncryptorImpl implements DotenvxEncryptor {
1417
private final String publicKeyHex;
1518
private final String privateKeyHex;
19+
private HashMap<String, String> profileKeyPairs = new HashMap<>();
1620

17-
public DotenvxEncryptorImpl(@Nullable String publicKeyHex, @Nullable String privateKeyHex) {
21+
public DotenvxEncryptorImpl(@Nullable String publicKeyHex, @Nullable String privateKeyHex, HashMap<String, String> profileKeyPairs) {
1822
this.publicKeyHex = publicKeyHex;
1923
this.privateKeyHex = privateKeyHex;
24+
this.profileKeyPairs = profileKeyPairs;
2025
}
2126

2227
/**
@@ -49,8 +54,18 @@ public String decrypt(String base64EncodedText) throws DecryptionException {
4954
base64EncodedText = base64EncodedText.substring("encrypted:".length());
5055
}
5156
return Ecies.decrypt(privateKeyHex, base64EncodedText);
52-
} catch (Exception e) {
53-
throw new DecryptionException("Failed to decrypt text: " + base64EncodedText, e);
57+
} catch (Exception ignore) {
58+
}
59+
for (Map.Entry<String, String> keyPair : profileKeyPairs.entrySet()) {
60+
try {
61+
String privateKey = keyPair.getValue();
62+
if (base64EncodedText.startsWith("encrypted:")) {
63+
base64EncodedText = base64EncodedText.substring("encrypted:".length());
64+
}
65+
return Ecies.decrypt(privateKey, base64EncodedText);
66+
} catch (Exception ignore) {
67+
}
5468
}
69+
throw new DecryptionException("Failed to decrypt text: " + base64EncodedText);
5570
}
5671
}

0 commit comments

Comments
 (0)