Author: gk
Date: Tue Sep 22 13:31:17 2020
New Revision: 1881927
URL: http://svn.apache.org/viewvc?rev=1881927&view=rev
Log:
- reverted release
- fixes for java > 8 and more consistent evaluation of supported providers
- fix for default evaluation
- add main class tests
Modified:
turbine/fulcrum/trunk/yaafi-crypto/pom.xml
turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/cli/CLI2.java
turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/extended/CryptoParametersJ8.java
turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/extended/CryptoUtilJ8.java
turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/Main8Test.java
Modified: turbine/fulcrum/trunk/yaafi-crypto/pom.xml
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/pom.xml?rev=1881927&r1=1881926&r2=1881927&view=diff
==============================================================================
--- turbine/fulcrum/trunk/yaafi-crypto/pom.xml (original)
+++ turbine/fulcrum/trunk/yaafi-crypto/pom.xml Tue Sep 22 13:31:17 2020
@@ -28,7 +28,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>fulcrum-yaafi-crypto</artifactId>
<groupId>org.apache.fulcrum</groupId>
- <version>2.0.2-SNAPSHOT</version>
+ <version>2.0.1-SNAPSHOT</version>
<name>Fulcrum YAAFI Crypto</name>
<inceptionYear>2008</inceptionYear>
<description>Fulcrum YAAFI Crypto Library</description>
Modified:
turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/cli/CLI2.java
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/cli/CLI2.java?rev=1881927&r1=1881926&r2=1881927&view=diff
==============================================================================
---
turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/cli/CLI2.java
(original)
+++
turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/cli/CLI2.java
Tue Sep 22 13:31:17 2020
@@ -27,8 +27,10 @@ import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.Arrays;
+import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
+import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@@ -36,8 +38,8 @@ import java.util.stream.Collectors;
import org.apache.fulcrum.jce.crypto.HexConverter;
import org.apache.fulcrum.jce.crypto.StreamUtil;
import org.apache.fulcrum.jce.crypto.extended.CryptoParametersJ8;
-import org.apache.fulcrum.jce.crypto.extended.CryptoStreamFactoryJ8Template;
import org.apache.fulcrum.jce.crypto.extended.CryptoParametersJ8.TYPES;
+import org.apache.fulcrum.jce.crypto.extended.CryptoStreamFactoryJ8Template;
import org.apache.fulcrum.jce.crypto.extended.CryptoUtilJ8;
/**
@@ -107,7 +109,7 @@ public class CLI2 {
processString(args);
}
} catch (Exception e) {
- System.out.println("Error : " + e.getMessage());
+ System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
@@ -117,29 +119,24 @@ public class CLI2 {
System.out.println("");
System.out.println("\t| Default Crypto factory class: \t" +
cryptoUtilJ8.getCryptoStreamFactory().getClass());
System.out.println("\t|_Default Algorithm used: \t" +
cryptoUtilJ8.getCryptoStreamFactory().getAlgorithm());
+
+ for (int i = 0; i < CryptoParametersJ8.LISTS.length; i++) {
+ List<String> list = CryptoParametersJ8.LISTS[i];
+ String type = CryptoParametersJ8.PROVIDER_TYPES[i];
+ System.out.println("\t|Algorithms (unchecked)
supported: \t" + list);
+ List<String> result =
CryptoParametersJ8.getSupportedAlgos(list, type, true);
+ Set<String> resultSet = new
LinkedHashSet<String>(result);
+ resultSet.addAll(
CryptoParametersJ8.getSupportedAlgos(list, type, false) );
+ System.out.println(
+ String.format("\t|_Matched supported
%2$s:\t%1$s",
+ (resultSet), type));
+ }
- List<String> algoShortList =
Arrays.stream(CryptoParametersJ8.TYPES.values()).map(t -> t.toString())
- .collect(Collectors.toList());
- System.out.println("\t|Algorithms (shortcut) available: \t" +
algoShortList);
- String type = "AlgorithmParameters";
- List result =
CryptoParametersJ8.getSupportedAlgos(algoShortList, type, true);
+ List<String> supportedAlgos = CryptoParametersJ8.init();
System.out.println(
- String.format("\t|_Matched supported
%2$s:\t%1$s",
- ((result.size() > 0) ?
- result:
-
CryptoParametersJ8.getSupportedAlgos(algoShortList, type, false)), type));
-
- List<String> algoList =
Arrays.stream(CryptoParametersJ8.TYPES_IMPL.values()).map(t -> t.toString())
- .collect(Collectors.toList());
- System.out.println("\t|Algorithms available: \t" + algoList);
- type = "Cipher";
- result = CryptoParametersJ8.getSupportedAlgos(algoList, type,
true);
- System.out.println(
- String.format("\t|_Matched Supported
%2$ss:\t%1$s",
- ((result.size() > 0) ?
- result:
-
CryptoParametersJ8.getSupportedAlgos(algoList, type, false)), type));
- System.out.println("");
+ String.format("\t|Effectively supported from
%2$ss:\t*** %1$s ***",
+ supportedAlgos,
Arrays.toString( CryptoParametersJ8.PROVIDER_TYPES) ));
+
if (debug) {
Arrays.stream(CryptoParametersJ8.TYPES.values()).forEach(t -> {
CryptoUtilJ8 testcu =
CryptoUtilJ8.getInstance(t);
@@ -166,7 +163,7 @@ public class CLI2 {
"\tjava -jar
target/fulcrum-yaafi-crypto-1.0.8-SNAPSHOT.jar <operation mode> <coding mode>
<password> <path|string> [target]\r\n");
System.out.println("\t-------------------");
System.out.println("\toperation mode: file|string|info");
- System.out.println("\tcoding mode: enc|dec|enc:GCM. Default
algorithm is " + TYPES.PBE);
+ System.out.println("\tcoding mode: enc|dec|enc:GCM. Default
algorithm is " + CryptoParametersJ8.DEFAULT_TYPE);
System.out.println("\t<password: string or empty:''");
System.out.println("\tcode|coderef: path|string");
System.out.println("\ttarget: optional\r\n");
@@ -221,6 +218,11 @@ public class CLI2 {
try (FileInputStream fis = new FileInputStream(sourceFile)) {
ByteArrayOutputStream baos = new
ByteArrayOutputStream();
CryptoUtilJ8 cryptoUtilJ8 =
createCryptoUtil(cipherMode);
+
+ if (cryptoUtilJ8 == null) {
+ System.out.println("Canceling ");
+ return;
+ }
if (cipherMode.startsWith("dec")) {
System.out.println("Decrypting " +
sourceFile.getAbsolutePath());
@@ -266,11 +268,16 @@ public class CLI2 {
private static CryptoUtilJ8 createCryptoUtil(String cipherMode) throws
Exception {
CryptoUtilJ8 cryptoUtilJ8 = null;
- // now extension like enc:GCM
- if (cipherMode.endsWith(TYPES.PBE.toString()) ||
cipherMode.substring("enc".length()).equals("")) {
- cryptoUtilJ8 = CryptoUtilJ8.getInstance();
+ List<String> supportedTypes = CryptoParametersJ8.init();
+ // no extension like enc:GCM
+ if (cipherMode.substring("enc".length()).equals("")) {
+ if (supportedTypes.stream().anyMatch(x->
x.equals(CryptoParametersJ8.DEFAULT_TYPE.toString()) )) {
+ System.err.println("using default type:"+
CryptoParametersJ8.DEFAULT_TYPE);
+ cryptoUtilJ8 = CryptoUtilJ8.getInstance();
+ } else {
+ System.err.println("Could not use default
type:"+ TYPES.PBE + ".You have to set explicit type, e.g.
enc:"+supportedTypes.get(0) );
+ }
} else {
- List<String> supportedTypes = CryptoParametersJ8.init();
System.err.println("checking supported types:"+
supportedTypes);
List<String> matchedType =
supportedTypes.stream().filter(x-> cipherMode.endsWith(x)
).collect(Collectors.toList());
System.err.println("matched type:"+ matchedType);
@@ -283,7 +290,7 @@ public class CLI2 {
}
if (cryptoUtilJ8 == null) {
- throw new Exception("Could not find any algorithms.
check provided algo shortcuts with CLI2 info!");
+ throw new Exception("Could not use algorithm. Check
debug output and JDK provided algo shortcuts with CLI2 info!");
}
if (debug) {
@@ -324,7 +331,6 @@ public class CLI2 {
if (!success) {
System.err.println("Error, could not
create directory to write parent file");
}
-
}
}
Modified:
turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/extended/CryptoParametersJ8.java
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/extended/CryptoParametersJ8.java?rev=1881927&r1=1881926&r2=1881927&view=diff
==============================================================================
---
turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/extended/CryptoParametersJ8.java
(original)
+++
turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/extended/CryptoParametersJ8.java
Tue Sep 22 13:31:17 2020
@@ -5,7 +5,9 @@ import java.security.Security;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
+import java.util.LinkedHashSet;
import java.util.List;
+import java.util.Set;
import java.util.stream.Collectors;
/*
@@ -140,16 +142,15 @@ public interface CryptoParametersJ8 {
* @param algoList the types to be checked
* @param type the type is ignored if not exact, instead uses the two
types: "AlgorithmParameters", "Cipher".
* @param exact if exact does a exact match
- * @return the matched results as a list
+ * @return the matched results as a list or emtpy list
*/
public static List<String> getSupportedAlgos(List<String> algoList,
String type, boolean exact) {
List<String> result = new ArrayList<String>();
Provider p[] = Security.getProviders();
List<Provider> providerList = Arrays.asList(p);
- String[] PROVIDER_TYPES = { "AlgorithmParameters", "Cipher" };
for (Provider provider : providerList) {
- // System.out.println(provider);
+ //System.out.println(provider);
result.addAll(Collections.list(provider.keys()).stream().map(t -> t.toString())
.filter(x->
(exact)?
@@ -167,26 +168,40 @@ public interface CryptoParametersJ8 {
}
return result;
}
+
+ public static List[] LISTS = {
Arrays.stream(CryptoParametersJ8.TYPES.values()).map(t -> t.toString())
+ .collect(Collectors.toList()),
+
Arrays.stream(CryptoParametersJ8.TYPES_IMPL.values()).map(t -> t.toString())
+ .collect(Collectors.toList()) };
+
+ public static String[] PROVIDER_TYPES = { "AlgorithmParameters",
"Cipher" };
/**
* initializes supported parameters by filtering {@link TYPES} against
<i>AlgorithmParameters</i> in system supported cipher suites:
* first by an exact match with type <i>AlgorithmParameters</i>, then
by inexact matching.
*
* {@link #getSupportedAlgos(List, String, boolean)}
- * @return list of supported algo short codes, if nothing is found the
{@link #DEFAULT_TYPE} is set.
+ * @return list of supported algo short codes, if nothing is found, the
list is empty.
*/
static List<String> init() {
List<String> result = new ArrayList<String>();
- List<String> defaultSupportedTypes = Arrays.asList(
TYPES.values() ).stream().map(x-> x.toString()).collect(Collectors.toList());
- String providerType = "AlgorithmParameters";
+ List<String> defaultSupportedTypes = LISTS[0];
+ String providerType = PROVIDER_TYPES[0];
result = getSupportedAlgos(defaultSupportedTypes, providerType,
true);
- if (result.isEmpty()) {
- // minimal default, try it
- result = getSupportedAlgos(defaultSupportedTypes,
providerType, false);
- result = !result.isEmpty()?
- result: Arrays.asList(
DEFAULT_TYPE).stream().map(x-> x.toString()).collect(Collectors.toList());
- }
- return result;
+ // no duplicates
+ Set<String> resultSet = new LinkedHashSet<String>(result);
+ resultSet.addAll( getSupportedAlgos(defaultSupportedTypes,
providerType, false));
+
+ List<String> algoList = LISTS[1];
+ String type = PROVIDER_TYPES[1];
+ List<String> result3 =
CryptoParametersJ8.getSupportedAlgos(algoList, type, true);
+ defaultSupportedTypes.stream().forEach(c-> {
+ if (result3.stream().anyMatch(x -> x.contains(c))) {
+ //System.out.println("adding " + c);
+ resultSet.add(c);
+ }
+ });
+ return new ArrayList<>(resultSet);
}
}
Modified:
turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/extended/CryptoUtilJ8.java
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/extended/CryptoUtilJ8.java?rev=1881927&r1=1881926&r2=1881927&view=diff
==============================================================================
---
turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/extended/CryptoUtilJ8.java
(original)
+++
turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/extended/CryptoUtilJ8.java
Tue Sep 22 13:31:17 2020
@@ -55,7 +55,7 @@ public final class CryptoUtilJ8 extends
public static CryptoUtilJ8 getInstance() {
synchronized (CryptoUtilJ8.class) {
TYPES defaultType = CryptoParametersJ8.DEFAULT_TYPE;
- if (instances.isEmpty() &&
!instances.containsKey(defaultType)) {
+ if (instances.isEmpty() ||
!instances.containsKey(defaultType)) {
instances.put(defaultType, new CryptoUtilJ8());
}
return instances.get(defaultType);
Modified:
turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/Main8Test.java
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/Main8Test.java?rev=1881927&r1=1881926&r2=1881927&view=diff
==============================================================================
---
turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/Main8Test.java
(original)
+++
turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/Main8Test.java
Tue Sep 22 13:31:17 2020
@@ -7,6 +7,8 @@ import static org.junit.jupiter.api.Asse
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import java.security.GeneralSecurityException;
import java.util.HashMap;
import java.util.Map;
@@ -16,11 +18,15 @@ import org.apache.fulcrum.jce.crypto.cli
import org.apache.fulcrum.jce.crypto.extended.CryptoParametersJ8.TYPES;
import org.apache.fulcrum.jce.junit5.extension.SupportedTypeArguments;
import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
import
org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory;
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ArgumentsSource;
/*
* Licensed to the Apache Software Foundation (ASF) under one
@@ -50,15 +56,24 @@ import org.junit.jupiter.api.Test;
public class Main8Test {
/** the password to be used */
private String password;
+
+ /** the test data directory */
+ private File testDataDirectory;
+
+ /** the temp data director */
+ private File main8DataDirectory;
+
+ private static Logger log = LogManager.getLogger(Main8Test.class);
/**
* Constructor
*/
public Main8Test() {
-
this.password = "foobar";
ConfigurationBuilder<BuiltConfiguration> builder =
ConfigurationBuilderFactory.newConfigurationBuilder();
builder.setStatusLevel(Level.DEBUG);
+ this.testDataDirectory = new File("./src/test/data");
+ this.main8DataDirectory = new File("./target/main8");
}
@BeforeAll
@@ -92,12 +107,13 @@ public class Main8Test {
CLI2.main(decryptionArgs);
}
- @Test
+ @ParameterizedTest
+ @ArgumentsSource(SupportedTypeArguments.class)
/** Encrypt a text file on the command line */
- public void testFileEncryption1() {
- String[] encryptionArgs = { "file", "enc", this.password,
"./src/test/data/plain.txt",
+ public void testFileEncryption1(TYPES type) {
+ String[] encryptionArgs = { "file", "enc:"+ type,
this.password, "./src/test/data/plain.txt",
"./target/main8/plain.enc.txt" };
- String[] decryptionArgs = { "file", "dec", this.password,
"./target/main8/plain.enc.txt",
+ String[] decryptionArgs = { "file", "dec:"+ type,
this.password, "./target/main8/plain.enc.txt",
"./target/main8/plain.dec.txt" };
CLI2.main(encryptionArgs);
CLI2.main(decryptionArgs);
@@ -108,6 +124,29 @@ public class Main8Test {
fail();
}
}
+
+
+ @ParameterizedTest
+ @ArgumentsSource(SupportedTypeArguments.class)
+ /** Encrypt a text file in-place on the command line */
+ public void testFileEncryption2Explicit(TYPES type) {
+ //System.out.println("checking type: "+ type);
+ File sourceFile = new File(this.testDataDirectory, "plain.txt");
+ File targetFile = new File(this.main8DataDirectory,
"plain.txt");
+ String[] encryptionArgs = { "file", "enc:"+ type,
this.password, "./src/test/data/plain.txt",
+ "./target/main8/plain.txt" };
+ // caution decrypting into source file!
+ String[] decryptionArgs = { "file", "dec:"+ type,
this.password, "./target/main8/plain.txt" };
+ CLI2.main(encryptionArgs);
+ CLI2.main(decryptionArgs);
+ try {
+ assertEquals(new
String(Files.readAllBytes(Paths.get(sourceFile.toURI()))),
+ new
String(Files.readAllBytes(Paths.get(targetFile.toURI()))));
+ } catch (IOException e) {
+ e.printStackTrace();
+ fail();
+ }
+ }
@Test
/** Encrypt a text file in-place on the command line */
@@ -229,6 +268,30 @@ public class Main8Test {
String cipherText;
String plainText;
try {
+ cipherText = cuj8.encryptString(source, this.getPassword());
+ plainText = cuj8.decryptString(cipherText, this.getPassword());
+ assertEquals(source, plainText, source +" is not equal with " +
plainText);
+ } catch (GeneralSecurityException | IOException e) {
+ e.printStackTrace();
+ fail();
+ }
+ }
+
+ @ParameterizedTest
+ @ArgumentsSource(SupportedTypeArguments.class)
+ public void testAvailableStringEncryption(TYPES type) {
+ char[] testVector = new char[513];
+
+ for (int i = 0; i < testVector.length; i++) {
+ testVector[i] = (char) i;
+ }
+ String source = new String(testVector);
+
+ CryptoUtilJ8.getInstances().clear();
+ CryptoUtilJ8 cuj8 = CryptoUtilJ8.getInstance(type);
+ String cipherText;
+ String plainText;
+ try {
cipherText = cuj8.encryptString(source, this.getPassword());
plainText = cuj8.decryptString(cipherText, this.getPassword());
assertEquals(source, plainText, source +" is not equal with " +
plainText);