Clean up PMD and FindBug reports. Fixed some typos and updated to conform with Turbine coding style guidelines
git-svn-id: https://svn.apache.org/repos/asf/turbine/fulcrum/trunk/yaafi-crypto@1866486 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index f7fc829..07e61db 100644
--- a/pom.xml
+++ b/pom.xml
@@ -72,7 +72,7 @@
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${turbine.log4j2.version}</version>
- <scope>test</scope><!-- chnage to provided ? -->
+ <scope>test</scope><!-- change to provided ? -->
</dependency>
</dependencies>
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 1a70588..1e5046b 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -28,6 +28,9 @@
<action dev="jk" type="update">
Add Java 8 PBEWithHmacSHA256AndAES_128 based encryption
</action>
+ <action dev="painter" type="update">
+ Clean up PMD and FindBug reports. Fixed some typos and updated to conform with Turbine coding style guidelines
+ </action>
</release>
<release version="1.0.7" date="2018-11-08">
<action dev="painter" type="update">
diff --git a/src/java/org/apache/fulcrum/jce/crypto/CryptoParameters.java b/src/java/org/apache/fulcrum/jce/crypto/CryptoParameters.java
index c8d1bec..947e591 100644
--- a/src/java/org/apache/fulcrum/jce/crypto/CryptoParameters.java
+++ b/src/java/org/apache/fulcrum/jce/crypto/CryptoParameters.java
@@ -31,12 +31,17 @@
/** Parameter for PBEParameterSpec */
int COUNT = 20;
- /** The password salt */
- byte[] SALT = {
- (byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
- (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
- };
-
+ /** The password salt: update to a method to prevent malicious code bug */
+ public static byte[] Salt()
+ {
+ return new byte[]
+ {
+ (byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
+ (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
+ };
+ }
+
+
/** The crypto algorithm being used */
String ALGORITHM = "PBEWithMD5AndDES";
diff --git a/src/java/org/apache/fulcrum/jce/crypto/CryptoStreamFactoryImpl.java b/src/java/org/apache/fulcrum/jce/crypto/CryptoStreamFactoryImpl.java
index 21334b5..6c491dc 100644
--- a/src/java/org/apache/fulcrum/jce/crypto/CryptoStreamFactoryImpl.java
+++ b/src/java/org/apache/fulcrum/jce/crypto/CryptoStreamFactoryImpl.java
@@ -103,7 +103,7 @@
*/
public CryptoStreamFactoryImpl()
{
- this.salt = CryptoParameters.SALT;
+ this.salt = CryptoParameters.Salt();
this.count = CryptoParameters.COUNT;
this.providerName = PROVIDERNAME;
this.algorithm = CryptoParameters.ALGORITHM;
diff --git a/src/java/org/apache/fulcrum/jce/crypto/CryptoStreamFactoryTemplate.java b/src/java/org/apache/fulcrum/jce/crypto/CryptoStreamFactoryTemplate.java
index 347e218..17e6f89 100644
--- a/src/java/org/apache/fulcrum/jce/crypto/CryptoStreamFactoryTemplate.java
+++ b/src/java/org/apache/fulcrum/jce/crypto/CryptoStreamFactoryTemplate.java
@@ -54,18 +54,21 @@
/** the default instance */
protected static CryptoStreamFactory instance;
- public static CryptoStreamFactory getInstance() {
+ public static CryptoStreamFactory getInstance()
+ {
return instance;
}
- public static void setInstance(CryptoStreamFactory instance) {
+ public static void setInstance(CryptoStreamFactory instance)
+ {
CryptoStreamFactoryTemplate.instance = instance;
}
/**
* @see org.apache.fulcrum.jce.crypto.CryptoStreamFactory#getInputStream(java.io.InputStream, String)
*/
- public InputStream getInputStream(InputStream is, String decryptionMode) throws GeneralSecurityException, IOException {
+ public InputStream getInputStream(InputStream is, String decryptionMode) throws GeneralSecurityException, IOException
+ {
InputStream result = null;
@@ -87,7 +90,8 @@
/**
* @see org.apache.fulcrum.jce.crypto.CryptoStreamFactory#getInputStream(java.io.InputStream, String, char[])
*/
- public InputStream getInputStream(InputStream is, String decryptionMode, char[] password) throws GeneralSecurityException, IOException {
+ public InputStream getInputStream(InputStream is, String decryptionMode, char[] password) throws GeneralSecurityException, IOException
+ {
InputStream result = null;
diff --git a/src/java/org/apache/fulcrum/jce/crypto/PasswordFactory.java b/src/java/org/apache/fulcrum/jce/crypto/PasswordFactory.java
index ac14eb4..d5d8049 100644
--- a/src/java/org/apache/fulcrum/jce/crypto/PasswordFactory.java
+++ b/src/java/org/apache/fulcrum/jce/crypto/PasswordFactory.java
@@ -20,7 +20,6 @@
*/
import java.io.UnsupportedEncodingException;
-import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@@ -109,8 +108,8 @@
throws NoSuchAlgorithmException, UnsupportedEncodingException
{
return create(
- PasswordParameters.DEFAULTPASSWORD,
- PasswordParameters.SALT,
+ PasswordParameters.DefaultPassword(),
+ PasswordParameters.Salt(),
count
);
}
@@ -143,7 +142,7 @@
{
return create(
seed,
- PasswordFactory.SALT,
+ PasswordParameters.Salt(),
count
);
}
diff --git a/src/java/org/apache/fulcrum/jce/crypto/PasswordParameters.java b/src/java/org/apache/fulcrum/jce/crypto/PasswordParameters.java
index aa114ed..96c66e2 100644
--- a/src/java/org/apache/fulcrum/jce/crypto/PasswordParameters.java
+++ b/src/java/org/apache/fulcrum/jce/crypto/PasswordParameters.java
@@ -31,16 +31,20 @@
int COUNT = 1000;
/** The default password used for creating the internal password */
- char[] DEFAULTPASSWORD = {
+ public static char[] DefaultPassword() {
+ return new char[] {
(char) 'f', (char) 'u', (char) 'l', (char) 'c',
(char) 'r', (char) 'u', (char) 'm', (char) '-',
(char) 'y', (char) 'a', (char) 'a', (char) 'f',
(char) 'i'
};
+ }
/** The password salt */
- byte[] SALT = {
+ public static byte[] Salt() {
+ return new byte[] {
(byte)0xc6, (byte)0x74, (byte)0x81, (byte)0x8a,
(byte)0x7b, (byte)0xe8, (byte)0xfe, (byte)0x99
};
+ }
}
diff --git a/src/java/org/apache/fulcrum/jce/crypto/SmartDecryptingInputStream.java b/src/java/org/apache/fulcrum/jce/crypto/SmartDecryptingInputStream.java
index 1521134..2f188b3 100644
--- a/src/java/org/apache/fulcrum/jce/crypto/SmartDecryptingInputStream.java
+++ b/src/java/org/apache/fulcrum/jce/crypto/SmartDecryptingInputStream.java
@@ -115,7 +115,7 @@
/**
* Determine if the content is encrypted. We are
- * using our knowledge about block lenght, check
+ * using our knowledge about block length, check
* for XML, ZIP and PDF files and at the end of
* the day we are just guessing.
*
@@ -227,8 +227,8 @@
*/
private boolean hasByteOrderMark( byte[] content )
{
- if( ( (content[0] == 0xFF) && (content[1] == 0xFF) ) ||
- ( (content[0] == 0xFF) && (content[1] == 0xFF) ) )
+ if( (content[0] == 0xFF) && (content[1] == 0xFE) ||
+ (content[0] == 0xFE) && (content[1] == 0xFF) )
{
return true;
}
diff --git a/src/java/org/apache/fulcrum/jce/crypto/StreamUtil.java b/src/java/org/apache/fulcrum/jce/crypto/StreamUtil.java
index 2fe609c..2ff5e41 100644
--- a/src/java/org/apache/fulcrum/jce/crypto/StreamUtil.java
+++ b/src/java/org/apache/fulcrum/jce/crypto/StreamUtil.java
@@ -27,7 +27,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.util.stream.IntStream;
/**
* Helper class to provde generic stream functions.
@@ -181,7 +180,11 @@
if((parentFile != null) && !parentFile.exists())
{
- parentFile.mkdirs();
+ boolean success = parentFile.mkdirs();
+ if ( !success )
+ {
+ System.err.println("Error, could not create directory to write parent file");
+ }
}
}
}
\ No newline at end of file
diff --git a/src/java/org/apache/fulcrum/jce/crypto/algo/CryptoStreamGCMImpl.java b/src/java/org/apache/fulcrum/jce/crypto/algo/CryptoStreamGCMImpl.java
index 75ca0c2..dde400c 100644
--- a/src/java/org/apache/fulcrum/jce/crypto/algo/CryptoStreamGCMImpl.java
+++ b/src/java/org/apache/fulcrum/jce/crypto/algo/CryptoStreamGCMImpl.java
@@ -118,7 +118,7 @@
Cipher cipher;
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
- long total = StreamUtil.copy(is, bos);
+ StreamUtil.copy(is, bos);
byte[] input = bos.toByteArray();
byte[] ciphertext = null;
diff --git a/src/java/org/apache/fulcrum/jce/crypto/algo/CryptoStreamPBEImpl.java b/src/java/org/apache/fulcrum/jce/crypto/algo/CryptoStreamPBEImpl.java
index 256a15d..bc2d54a 100644
--- a/src/java/org/apache/fulcrum/jce/crypto/algo/CryptoStreamPBEImpl.java
+++ b/src/java/org/apache/fulcrum/jce/crypto/algo/CryptoStreamPBEImpl.java
@@ -45,11 +45,11 @@
* by SUN (using SunJCE 1.42).
*
* The implementation uses as @see {@link CryptoParametersJ8#ALGORITHM_J8_PBE} for encryption which
- * should be sufficent for most applications.
+ * should be sufficient for most applications.
*
* The implementation also supplies a default password in the case that
* the programmer don't want to have additional hassles. It is easy to
- * reengineer the password being used but much better than a hard-coded
+ * re-engineer the password being used but much better than a hard-coded
* password in the application.
*
* The code uses parts from Markus Hahn's Blowfish library found at
@@ -147,7 +147,7 @@
PBEParameterSpec paramSpec = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
- long total = StreamUtil.copy(is, bos);
+ StreamUtil.copy(is, bos);
byte[] input = bos.toByteArray();
byte[] ciphertext = null;
diff --git a/src/java/org/apache/fulcrum/jce/crypto/cli/CLI.java b/src/java/org/apache/fulcrum/jce/crypto/cli/CLI.java
index 6f493cc..9394b5f 100644
--- a/src/java/org/apache/fulcrum/jce/crypto/cli/CLI.java
+++ b/src/java/org/apache/fulcrum/jce/crypto/cli/CLI.java
@@ -102,9 +102,13 @@
targetFile = new File(args[4]);
File parentFile = targetFile.getParentFile();
- if(parentFile != null)
+ if (parentFile != null)
{
- parentFile.mkdirs();
+ boolean success = parentFile.mkdirs();
+ if ( !success )
+ {
+ System.err.println("Failed to create directory");
+ }
}
}
@@ -112,11 +116,11 @@
}
/**
- * Decrypt/encrypt a single file
+ * Decrypt and encrypt a single file
* @param cipherMode the mode
- * @param password the passwors
+ * @param password the password
* @param sourceFile the file to process
- * @param targetFile the targetf file
+ * @param targetFile the target file
* @throws Exception the operation failed
*/
public static void processFile(String cipherMode, char[] password, File sourceFile, File targetFile)
diff --git a/src/java/org/apache/fulcrum/jce/crypto/cli/CLI2.java b/src/java/org/apache/fulcrum/jce/crypto/cli/CLI2.java
index 81e747c..9f4c3dc 100644
--- a/src/java/org/apache/fulcrum/jce/crypto/cli/CLI2.java
+++ b/src/java/org/apache/fulcrum/jce/crypto/cli/CLI2.java
@@ -1,7 +1,5 @@
package org.apache.fulcrum.jce.crypto.cli;
-import java.io.BufferedReader;
-
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -26,10 +24,8 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
-import java.io.FileReader;
-import java.io.Reader;
-import java.nio.file.Files;
-import java.nio.file.Paths;
+import java.io.OutputStreamWriter;
+import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Optional;
import java.util.regex.Matcher;
@@ -73,7 +69,8 @@
String operationMode = args[0];
String msg = "No operationMode" ;
- if (operationMode == null || operationMode.equals("")) {
+ if (operationMode == null || operationMode.equals(""))
+ {
throw new IllegalArgumentException(msg);
}
@@ -81,7 +78,9 @@
{
printInfo();
return;
- } else if (operationMode.equals("help") ) {
+ }
+ else if (operationMode.equals("help") )
+ {
printHelp();
return;
}
@@ -109,7 +108,8 @@
}
}
- private static void printInfo() {
+ private static void printInfo()
+ {
CryptoUtilJ8 cryptoUtilJ8 = CryptoUtilJ8.getInstance();
System.out.println("\tCrypto factory class: " + cryptoUtilJ8.getCryptoStreamFactory().getClass());
System.out.println("\tDefault Algorithm used: " + cryptoUtilJ8.getCryptoStreamFactory().getAlgorithm());
@@ -152,7 +152,7 @@
File sourceFile = new File(args[3]);
File targetFile = null;
- if( args.length == 4 )
+ if (args.length == 4)
{
targetFile = sourceFile;
}
@@ -161,9 +161,13 @@
targetFile = new File(args[4]);
File parentFile = targetFile.getParentFile();
- if(parentFile != null)
+ if (parentFile != null)
{
- parentFile.mkdirs();
+ boolean success = parentFile.mkdirs();
+ if ( !success )
+ {
+ System.err.println("Error, could not create directory to write parent file");
+ }
}
}
@@ -182,31 +186,36 @@
throws Exception
{
- try (FileInputStream fis = new FileInputStream(sourceFile)) {
+ try (FileInputStream fis = new FileInputStream(sourceFile))
+ {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
CryptoUtilJ8 cryptoUtilJ8 = createCryptoUtil(cipherMode);
if( cipherMode.startsWith("dec") )
{
System.out.println("Decrypting " + sourceFile.getAbsolutePath() );
+
+ //String value = new String(Files.readAllBytes(Paths.get(sourceFile.toURI())));
+ StringBuffer stringBuffer = new StringBuffer();
+ int i;
+ while ((i=fis.read()) != -1)
+ {
+ stringBuffer.append((char) i);
+ }
-
- //String value = new String(Files.readAllBytes(Paths.get(sourceFile.toURI())));
- StringBuffer stringBuffer = new StringBuffer();
- int i;
- while ((i=fis.read()) != -1) {
- stringBuffer.append((char) i);
- }
- String value = stringBuffer.toString();
- if (isHexadecimal(new String(value))) {
- byte[] buffer = HexConverter.toBytes(value);
- cryptoUtilJ8.decrypt( buffer, baos, password );
- } else {
- try ( FileInputStream fis2 = new FileInputStream(sourceFile) ) {
- cryptoUtilJ8.decrypt( fis2, baos, password );
- }
+ String value = stringBuffer.toString();
+ if (isHexadecimal(value))
+ {
+ byte[] buffer = HexConverter.toBytes(value);
+ cryptoUtilJ8.decrypt( buffer, baos, password );
+ }
+ else
+ {
+ try ( FileInputStream fis2 = new FileInputStream(sourceFile) )
+ {
+ cryptoUtilJ8.decrypt( fis2, baos, password );
}
+ }
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
FileOutputStream fos = new FileOutputStream(targetFile);
@@ -234,24 +243,32 @@
}
}
- private static CryptoUtilJ8 createCryptoUtil(String cipherMode) throws Exception {
+ private static CryptoUtilJ8 createCryptoUtil(String cipherMode) throws Exception
+ {
CryptoUtilJ8 cryptoUtilJ8 = null;
- if (cipherMode.endsWith(TYPES.PBE.toString()) || cipherMode.substring("enc".length()).equals("") ) {
+ if (cipherMode.endsWith(TYPES.PBE.toString()) || cipherMode.substring("enc".length()).equals("") )
+ {
cryptoUtilJ8 = CryptoUtilJ8.getInstance();
- } else {
+ }
+ else
+ {
Optional<TYPES> algoShortcut = Arrays.stream(CryptoParametersJ8.TYPES.values()).filter(a-> cipherMode.endsWith(a.toString())).findFirst(); //.collect(Collectors.toList());
- if (algoShortcut.isPresent()) {
+ if (algoShortcut.isPresent())
+ {
cryptoUtilJ8 = CryptoUtilJ8.getInstance(algoShortcut.get());
}
}
- if (cryptoUtilJ8 == null) {
+
+ if (cryptoUtilJ8 == null)
+ {
throw new Exception("Could not find any algorithms. check provided alog shortcuts with CLI2 info!");
}
+
return cryptoUtilJ8;
}
/**
- * Decrypt/encrypt a string.
+ * Decrypt and encrypt a string.
*
* @param args the command line
* @throws Exception the operation failed
@@ -264,17 +281,24 @@
String value = args[3];
File targetFile = null;
- if( args.length == 5 ) {
+ if (args.length == 5)
+ {
targetFile = new File(args[4]);
File parentFile = targetFile.getParentFile();
if(parentFile != null)
{
- parentFile.mkdirs();
+ boolean success = parentFile.mkdirs();
+ if ( !success )
+ {
+ System.err.println("Error, could not create directory to write parent file");
+ }
+
}
}
- if (value != null && !value.equals("")) {
+ if (value != null && !value.equals(""))
+ {
CryptoUtilJ8 cryptoUtilJ8 = createCryptoUtil(cipherMode);
@@ -291,9 +315,11 @@
System.out.println( result );
if (targetFile != null) {
- try ( FileOutputStream outputStream = new FileOutputStream(targetFile)) {
- outputStream.write(result.getBytes());
- }
+
+ try (OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(targetFile), Charset.forName("UTF-8").newEncoder() ) )
+ {
+ osw.write(result);
+ }
}
}
}
diff --git a/src/java/org/apache/fulcrum/jce/crypto/extended/CryptoParametersJ8.java b/src/java/org/apache/fulcrum/jce/crypto/extended/CryptoParametersJ8.java
index b185ea7..f70fa32 100644
--- a/src/java/org/apache/fulcrum/jce/crypto/extended/CryptoParametersJ8.java
+++ b/src/java/org/apache/fulcrum/jce/crypto/extended/CryptoParametersJ8.java
@@ -37,30 +37,40 @@
* Algo/mode/padding for cipher transformation:
* @see https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html
*
- * using PBEWith<digest>And<encryption>:
+ * using PBEWith <digest>And<encryption>:
*
- * <li>PBEWithHmacSHA256AndAES_256/CBC/PKCS5Padding
+ * <ul>
+ * <li>PBEWithHmacSHA256AndAES_256/CBC/PKCS5Padding</li>
+ * </ul>
*
* or
* Cipher Algorithm Names/Cipher Algorithm Modes/Cipher Algorithm Padding
*
- * <li>AES/GCM/NoPadding
+ * <ul>
+ * <li>AES/GCM/NoPadding</li>
+ * </ul>
*/
- public enum TYPES_IMPL {
+ public enum TYPES_IMPL
+ {
ALGORITHM_J8_PBE("PBEWithHmacSHA256AndAES_256"),
ALGORITHM_J8_GCM("AES/GCM/NoPadding");
private final String algorithm;
- private TYPES_IMPL(String algo) {
+ private TYPES_IMPL(String algo)
+ {
algorithm = algo;
}
+
@Override
- public String toString() {
+ public String toString()
+ {
return this.algorithm;
}
- public String getAlgorithm() {
+
+ public String getAlgorithm()
+ {
return algorithm;
}
}
@@ -74,5 +84,5 @@
*
* This should be always 10 bytes
*/
- String CLEAR_CODE_J8 = "J8_AES256;"; //
+ String CLEAR_CODE_J8 = "J8_AES256;";
}
diff --git a/src/java/org/apache/fulcrum/jce/crypto/extended/CryptoStreamFactoryJ8Impl.java b/src/java/org/apache/fulcrum/jce/crypto/extended/CryptoStreamFactoryJ8Impl.java
index 87f1418..3c45bc0 100644
--- a/src/java/org/apache/fulcrum/jce/crypto/extended/CryptoStreamFactoryJ8Impl.java
+++ b/src/java/org/apache/fulcrum/jce/crypto/extended/CryptoStreamFactoryJ8Impl.java
@@ -25,7 +25,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.security.AlgorithmParameters;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
@@ -38,7 +37,6 @@
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
-import org.apache.fulcrum.jce.crypto.CryptoStreamFactory;
import org.apache.fulcrum.jce.crypto.CryptoStreamFactoryImpl;
import org.apache.fulcrum.jce.crypto.PasswordFactory;
import org.apache.fulcrum.jce.crypto.StreamUtil;
@@ -67,15 +65,12 @@
public final class CryptoStreamFactoryJ8Impl extends CryptoStreamFactoryImpl implements CryptoStreamFactoryJ8
{
- private static final int SALT_SIZE = 128;//might increase cipher length
+ private static final int SALT_SIZE = 128; //might increase cipher length
private static final int KEY_SIZE = 256;
/** the default instance */
private static CryptoStreamFactoryJ8 instance;
- private AlgorithmParameters algorithmParameters;// used only for debugging
-
-
/**
* Factory method to get a default instance
* @return an instance of the CryptoStreamFactory
@@ -230,14 +225,14 @@
PBEParameterSpec paramSpec = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
- long total = StreamUtil.copy(is, bos);
+ StreamUtil.copy(is, bos);
+
byte[] input = bos.toByteArray();
-
byte[] ciphertext = null;
-
byte[] salt = null;
byte[] iv = null;
- if (mode == Cipher.DECRYPT_MODE) {
+ if (mode == Cipher.DECRYPT_MODE)
+ {
salt = Arrays.copyOfRange(input, 0, SALT_SIZE / 8);
iv = Arrays.copyOfRange(input, salt.length, salt.length + 128 / 8);
ciphertext = Arrays.copyOfRange(input, salt.length + iv.length, input.length);// cut out salt and iv
diff --git a/src/java/org/apache/fulcrum/jce/crypto/extended/CryptoUtilJ8.java b/src/java/org/apache/fulcrum/jce/crypto/extended/CryptoUtilJ8.java
index 723e340..8e67fe9 100644
--- a/src/java/org/apache/fulcrum/jce/crypto/extended/CryptoUtilJ8.java
+++ b/src/java/org/apache/fulcrum/jce/crypto/extended/CryptoUtilJ8.java
@@ -30,9 +30,6 @@
import org.apache.fulcrum.jce.crypto.CryptoUtil;
import org.apache.fulcrum.jce.crypto.StreamUtil;
import org.apache.fulcrum.jce.crypto.extended.CryptoParametersJ8.TYPES;
-import org.apache.fulcrum.jce.crypto.extended.CryptoParametersJ8.TYPES_IMPL;
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
/**
* Helper class to provde generic functions to work with CryptoStreams.
@@ -43,20 +40,21 @@
* @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl </a>
* @author <a href="mailto:maakus@earthlink.net">Markus Hahn</a>
*/
-
-public final class CryptoUtilJ8 extends CryptoUtil {
-
-
- public TYPES type;// default see instance
-
-
- public TYPES getType() {
- return type;
- }
+public final class CryptoUtilJ8 extends CryptoUtil
+{
/** the typed default instances */
private static Map<TYPES,CryptoUtilJ8> cryptoUtilJ8s = new ConcurrentHashMap<>();
+ // default see instance
+ public TYPES type;
+
+ public TYPES getType()
+ {
+ return type;
+ }
+
+
/**
* Factory method to get a default instance
@@ -65,7 +63,8 @@
*/
public static CryptoUtilJ8 getInstance(TYPES type)
{
- synchronized (CryptoUtilJ8.class) {
+ synchronized (CryptoUtilJ8.class)
+ {
if( !cryptoUtilJ8s.containsKey(type) )
{
cryptoUtilJ8s.put(type, new CryptoUtilJ8(type) );
@@ -82,7 +81,8 @@
*/
public static CryptoUtilJ8 getInstance()
{
- synchronized (CryptoUtilJ8.class) {
+ synchronized (CryptoUtilJ8.class)
+ {
TYPES defaultType = TYPES.PBE;
if( cryptoUtilJ8s.isEmpty() && !cryptoUtilJ8s.containsKey(defaultType) )
{
@@ -92,12 +92,14 @@
}
}
- private CryptoUtilJ8(TYPES type) {
+ private CryptoUtilJ8(TYPES type)
+ {
super();
this.type = type;
}
- private CryptoUtilJ8() {
+ private CryptoUtilJ8()
+ {
super();
}
@@ -114,10 +116,11 @@
*/
@Override
public void encrypt(CryptoStreamFactory factory, Object source, Object target, char[] password)
- throws GeneralSecurityException, IOException {
- InputStream is = StreamUtil.createInputStream(source);
+ throws GeneralSecurityException, IOException
+ {
+ InputStream is = StreamUtil.createInputStream(source);
OutputStream os = StreamUtil.createOutputStream(target);
- OutputStream eos = ((CryptoStreamFactoryJ8)factory).getOutputStream(is, os, password);
+ OutputStream eos = ( (CryptoStreamFactoryJ8) factory).getOutputStream(is, os, password);
// StreamUtil.copy( is, eos );
}
@@ -134,7 +137,8 @@
*/
@Override
protected void decrypt(CryptoStreamFactory factory, Object source, Object target, char[] password)
- throws GeneralSecurityException, IOException {
+ throws GeneralSecurityException, IOException
+ {
InputStream is = StreamUtil.createInputStream(source);
OutputStream os = StreamUtil.createOutputStream(target);
InputStream dis = factory.getInputStream(is, password);
@@ -145,7 +149,8 @@
*
* @return the CryptoStreamFactory to be used
*/
- public CryptoStreamFactory getCryptoStreamFactory() {
+ public CryptoStreamFactory getCryptoStreamFactory()
+ {
return CryptoStreamFactoryJ8Template.getInstance(type);
}
}
diff --git a/src/test/org/apache/fulcrum/jce/crypto/CryptoUtilTest.java b/src/test/org/apache/fulcrum/jce/crypto/CryptoUtilTest.java
index bab0e8a..caf0283 100644
--- a/src/test/org/apache/fulcrum/jce/crypto/CryptoUtilTest.java
+++ b/src/test/org/apache/fulcrum/jce/crypto/CryptoUtilTest.java
@@ -67,7 +67,7 @@
* @throws Exception Generic exception
*/
protected void setUp() throws Exception {
- CryptoStreamFactoryImpl factory = new CryptoStreamFactoryImpl(CryptoParameters.SALT, CryptoParameters.COUNT);
+ CryptoStreamFactoryImpl factory = new CryptoStreamFactoryImpl(CryptoParameters.Salt(), CryptoParameters.COUNT);
CryptoStreamFactoryImpl.setInstance(factory);
}
diff --git a/src/test/org/apache/fulcrum/jce/crypto/SmartDecryptingInputStreamTest.java b/src/test/org/apache/fulcrum/jce/crypto/SmartDecryptingInputStreamTest.java
index 1c0cd0e..e44b99e 100644
--- a/src/test/org/apache/fulcrum/jce/crypto/SmartDecryptingInputStreamTest.java
+++ b/src/test/org/apache/fulcrum/jce/crypto/SmartDecryptingInputStreamTest.java
@@ -63,7 +63,7 @@
protected void setUp() throws Exception
{
CryptoStreamFactoryImpl factory = new CryptoStreamFactoryImpl(
- CryptoParameters.SALT,
+ CryptoParameters.Salt(),
CryptoParameters.COUNT
);