This is an automated email from the ASF dual-hosted git repository.

fmariani pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 805c3829630a5630d6f82a137fd08a8ebb2cc456
Author: Croway <[email protected]>
AuthorDate: Wed Dec 13 12:07:07 2023 +0100

    CAMEL-20231: make generators configurable
---
 .../component/jasypt/JasyptPropertiesParser.java   | 21 ++++++++++++
 .../org/apache/camel/component/jasypt/Main.java    | 28 +++++++++++++++
 .../jasypt/JasyptPropertiesParserTest.java         | 21 ++++++------
 .../jasypt/JasytPropertiesParserCustomAlgTest.java | 40 ++++++++++++++++++++++
 4 files changed, 100 insertions(+), 10 deletions(-)

diff --git 
a/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/JasyptPropertiesParser.java
 
b/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/JasyptPropertiesParser.java
index 87152432528..0dc7ddbec66 100644
--- 
a/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/JasyptPropertiesParser.java
+++ 
b/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/JasyptPropertiesParser.java
@@ -25,6 +25,8 @@ import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.StringHelper;
 import org.jasypt.encryption.StringEncryptor;
 import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
+import org.jasypt.iv.RandomIvGenerator;
+import org.jasypt.salt.RandomSaltGenerator;
 
 /**
  * A {@link org.apache.camel.component.properties.PropertiesParser} which is 
using
@@ -44,6 +46,8 @@ public class JasyptPropertiesParser extends 
DefaultPropertiesParser {
     private StringEncryptor encryptor;
     private String password;
     private String algorithm;
+    private String randomSaltGeneratorAlgorithm;
+    private String randomIvGeneratorAlgorithm;
 
     public JasyptPropertiesParser() {
     }
@@ -69,6 +73,7 @@ public class JasyptPropertiesParser extends 
DefaultPropertiesParser {
         if (encryptor == null) {
             StringHelper.notEmpty("password", password);
             StandardPBEStringEncryptor pbeStringEncryptor = new 
StandardPBEStringEncryptor();
+
             pbeStringEncryptor.setPassword(password);
             if (algorithm != null) {
                 pbeStringEncryptor.setAlgorithm(algorithm);
@@ -76,6 +81,14 @@ public class JasyptPropertiesParser extends 
DefaultPropertiesParser {
             } else {
                 log.debug("Initialized encryptor using default algorithm and 
provided password");
             }
+
+            if (randomSaltGeneratorAlgorithm != null) {
+                pbeStringEncryptor.setSaltGenerator(new 
RandomSaltGenerator(randomSaltGeneratorAlgorithm));
+            }
+            if (randomIvGeneratorAlgorithm != null) {
+                pbeStringEncryptor.setIvGenerator(new 
RandomIvGenerator(randomIvGeneratorAlgorithm));
+            }
+
             encryptor = pbeStringEncryptor;
         }
     }
@@ -88,6 +101,14 @@ public class JasyptPropertiesParser extends 
DefaultPropertiesParser {
         this.algorithm = algorithm;
     }
 
+    public void setRandomSaltGeneratorAlgorithm(String 
randomSaltGeneratorAlgorithm) {
+        this.randomSaltGeneratorAlgorithm = randomSaltGeneratorAlgorithm;
+    }
+
+    public void setRandomIvGeneratorAlgorithm(String 
randomIvGeneratorAlgorithm) {
+        this.randomIvGeneratorAlgorithm = randomIvGeneratorAlgorithm;
+    }
+
     public void setPassword(String password) {
         // lookup password as either environment or JVM system property
         if (password.startsWith("sysenv:")) {
diff --git 
a/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/Main.java
 
b/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/Main.java
index e165e4d5dc1..15a89d414ac 100644
--- 
a/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/Main.java
+++ 
b/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/Main.java
@@ -22,6 +22,8 @@ import java.util.LinkedList;
 import java.util.List;
 
 import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
+import org.jasypt.iv.RandomIvGenerator;
+import org.jasypt.salt.RandomSaltGenerator;
 
 public class Main {
 
@@ -31,6 +33,8 @@ public class Main {
     private String password;
     private String input;
     private String algorithm;
+    private String randomSaltGeneratorAlgorithm;
+    private String randomIvGeneratorAlgorithm;
 
     private abstract class Option {
         private String abbreviation;
@@ -134,6 +138,24 @@ public class Main {
                 algorithm = parameter;
             }
         });
+
+        addOption(new ParameterOption("rsga", "salt", "Optional random salt 
generator algorithm to use", "salt") {
+            protected void doProcess(String arg, String parameter, 
LinkedList<String> remainingArgs) {
+                randomSaltGeneratorAlgorithm = parameter;
+            }
+        });
+
+        addOption(new ParameterOption("riga", "iv", "Optional random iv 
generator algorithm to use", "iv") {
+            protected void doProcess(String arg, String parameter, 
LinkedList<String> remainingArgs) {
+                randomIvGeneratorAlgorithm = parameter;
+            }
+        });
+
+        addOption(new ParameterOption("a", "algorithm", "Optional algorithm to 
use", "algorithm") {
+            protected void doProcess(String arg, String parameter, 
LinkedList<String> remainingArgs) {
+                algorithm = parameter;
+            }
+        });
     }
 
     private void addOption(Option option) {
@@ -204,6 +226,12 @@ public class Main {
         if (algorithm != null) {
             encryptor.setAlgorithm(algorithm);
         }
+        if (randomSaltGeneratorAlgorithm != null) {
+            encryptor.setSaltGenerator(new 
RandomSaltGenerator(randomSaltGeneratorAlgorithm));
+        }
+        if (randomIvGeneratorAlgorithm != null) {
+            encryptor.setIvGenerator(new 
RandomIvGenerator(randomIvGeneratorAlgorithm));
+        }
         if ("encrypt".equals(command)) {
             System.out.println("Encrypted text: " + encryptor.encrypt(input));
         } else {
diff --git 
a/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasyptPropertiesParserTest.java
 
b/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasyptPropertiesParserTest.java
index 1f2fe6a7060..c212eba7b00 100644
--- 
a/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasyptPropertiesParserTest.java
+++ 
b/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasyptPropertiesParserTest.java
@@ -31,16 +31,17 @@ public class JasyptPropertiesParserTest {
 
     private static final String KEY = "somekey";
 
-    private static final String KNOWN_PASSWORD = "secret";
-    private static final String KNOWN_ENCRYPTED = 
"ENC(bsW9uV37gQ0QHFu7KO03Ww==)";
-    private static final String KNOW_DECRYPTED = "tiger";
+    protected String knownPassword = "secret";
+    protected String knownEncrypted = "ENC(bsW9uV37gQ0QHFu7KO03Ww==)";
+    protected String knowDecrypted = "tiger";
 
-    private JasyptPropertiesParser jasyptPropertiesParser = new 
JasyptPropertiesParser();
-    private StandardPBEStringEncryptor encryptor = new 
StandardPBEStringEncryptor();
+    protected JasyptPropertiesParser jasyptPropertiesParser = new 
JasyptPropertiesParser();
+    protected StandardPBEStringEncryptor encryptor = new 
StandardPBEStringEncryptor();
 
     @BeforeEach
     public void before() {
-        encryptor.setPassword(KNOWN_PASSWORD);
+        encryptor.setPassword(knownPassword);
+
         jasyptPropertiesParser.setEncryptor(encryptor);
     }
 
@@ -95,19 +96,19 @@ public class JasyptPropertiesParserTest {
     @Test
     public void testUsesProvidedPasswordIfEncryptorIsNotSet() {
         jasyptPropertiesParser.setEncryptor(null);
-        jasyptPropertiesParser.setPassword(KNOWN_PASSWORD);
+        jasyptPropertiesParser.setPassword(knownPassword);
 
-        assertEquals(KNOW_DECRYPTED, jasyptPropertiesParser.parseProperty(KEY, 
KNOWN_ENCRYPTED, null));
+        assertEquals(knowDecrypted, jasyptPropertiesParser.parseProperty(KEY, 
knownEncrypted, null));
     }
 
     @Test
     public void 
testUsesProvidedPasswordFromSystemPropertyIfEncryptorIsNotSet() {
-        System.setProperty("myfoo", KNOWN_PASSWORD);
+        System.setProperty("myfoo", knownPassword);
 
         jasyptPropertiesParser.setEncryptor(null);
         jasyptPropertiesParser.setPassword("sys:myfoo");
 
-        assertEquals(KNOW_DECRYPTED, jasyptPropertiesParser.parseProperty(KEY, 
KNOWN_ENCRYPTED, null));
+        assertEquals(knowDecrypted, jasyptPropertiesParser.parseProperty(KEY, 
knownEncrypted, null));
 
         System.clearProperty("myfoo");
     }
diff --git 
a/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasytPropertiesParserCustomAlgTest.java
 
b/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasytPropertiesParserCustomAlgTest.java
new file mode 100644
index 00000000000..aa050eb0f54
--- /dev/null
+++ 
b/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasytPropertiesParserCustomAlgTest.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.jasypt;
+
+import org.jasypt.iv.RandomIvGenerator;
+import org.jasypt.salt.RandomSaltGenerator;
+import org.junit.jupiter.api.BeforeEach;
+
+public class JasytPropertiesParserCustomAlgTest extends 
JasyptPropertiesParserTest {
+
+       @BeforeEach
+       public void before() {
+               knowDecrypted = "tigertigertiger";
+               knownEncrypted = 
"ENC(LuCBTHaY1G6XHRwp63teshi/LbFRzpPtq5j8SNpJgv1yn9D25py+xHNGjXEMnf/J)";
+
+        encryptor.setAlgorithm("PBEWithHmacSHA256AndAES_256");
+               encryptor.setSaltGenerator(new RandomSaltGenerator("SHA1PRNG"));
+        encryptor.setIvGenerator(new RandomIvGenerator("SHA1PRNG"));
+               encryptor.setPassword(knownPassword);
+
+        jasyptPropertiesParser.setAlgorithm("PBEWithHmacSHA256AndAES_256");
+               
jasyptPropertiesParser.setRandomSaltGeneratorAlgorithm("SHA1PRNG");
+        jasyptPropertiesParser.setRandomIvGeneratorAlgorithm("SHA1PRNG");
+               jasyptPropertiesParser.setEncryptor(encryptor);
+       }
+}

Reply via email to