You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@turbine.apache.org by sg...@apache.org on 2008/07/22 11:55:58 UTC

svn commit: r678696 - in /turbine/fulcrum/trunk/yaafi-crypto/src: java/org/apache/fulcrum/jce/crypto/CryptoUtil.java java/org/apache/fulcrum/jce/crypto/StreamUtil.java test/org/apache/fulcrum/jce/crypto/CryptoUtilTest.java

Author: sgoeschl
Date: Tue Jul 22 02:55:57 2008
New Revision: 678696

URL: http://svn.apache.org/viewvc?rev=678696&view=rev
Log:
Moving stream related code from CryptoUtil to StreamUtil

Added:
    turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/StreamUtil.java
      - copied, changed from r678465, turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/CryptoUtil.java
Modified:
    turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/CryptoUtil.java
    turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/CryptoUtilTest.java

Modified: turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/CryptoUtil.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/CryptoUtil.java?rev=678696&r1=678695&r2=678696&view=diff
==============================================================================
--- turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/CryptoUtil.java (original)
+++ turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/CryptoUtil.java Tue Jul 22 02:55:57 2008
@@ -19,11 +19,8 @@
  * under the License.
  */
 
-import java.io.ByteArrayInputStream;
+
 import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -41,9 +38,6 @@
 
 public final class CryptoUtil
 {
-    /** the size of the internal buffer to copy streams */
-    private static final int BUFFER_SIZE = 1024;
-
     /**
      * Copies from a source to a target object using encryption
      *
@@ -80,10 +74,10 @@
         CryptoStreamFactory factory, Object source, Object target, char[] password )
         throws GeneralSecurityException, IOException
     {
-        InputStream is = CryptoUtil.createInputStream( source );
-        OutputStream os = CryptoUtil.createOutputStream( target );
+        InputStream is = StreamUtil.createInputStream( source );
+        OutputStream os = StreamUtil.createOutputStream( target );
         OutputStream eos = factory.getOutputStream( os, password );
-        CryptoUtil.copy( is, eos );
+        StreamUtil.copy( is, eos );
     }
 
     /**
@@ -121,10 +115,10 @@
         CryptoStreamFactory factory, Object source, Object target, char[] password )
         throws GeneralSecurityException, IOException
     {
-        InputStream is = CryptoUtil.createInputStream( source );
-        OutputStream os = CryptoUtil.createOutputStream( target );
+        InputStream is = StreamUtil.createInputStream( source );
+        OutputStream os = StreamUtil.createOutputStream( target );
         InputStream dis = factory.getInputStream( is, password );
-        CryptoUtil.copy( dis, os );
+        StreamUtil.copy( dis, os );
     }
 
     /**
@@ -206,93 +200,6 @@
         return new String( bais.toByteArray(), "utf-8" );
     }
 
-    ///////////////////////////////////////////////////////////////////////////
-    // Private Implementation
-    ///////////////////////////////////////////////////////////////////////////
-
-    /**
-     * Create an input stream supporting the following types
-     *
-     * <ul>
-     *  <li>String</li>
-     *  <li>File</li>
-     *  <li>byte[]</li>
-     *  <li>char[]</li>
-     *  <li>ByteArrayOutputStream</li>
-     *  <li>InputStream</li>
-     * </ul>
-     *
-     * @param source the source object
-     * @return the created input stream
-     * @throws IOException creating the input stream failed
-     */
-    private static InputStream createInputStream( Object source )
-        throws IOException
-    {
-        InputStream is;
-
-        // create an InputStream
-
-        if( source instanceof String )
-        {
-            byte[] content = ((String) source).getBytes("utf-8");
-            is = new ByteArrayInputStream( content );
-        }
-        else if( source instanceof File )
-        {
-            is = new FileInputStream( (File) source );
-        }
-        else if( source instanceof byte[] )
-        {
-            is = new ByteArrayInputStream( (byte[]) source );
-        }
-        else if( source instanceof char[] )
-        {
-            byte[] content = new String((char[])source).getBytes("utf-8");
-            is = new ByteArrayInputStream( content );
-        }
-        else if( source instanceof ByteArrayOutputStream )
-        {
-            byte[] content = ((ByteArrayOutputStream) source).toByteArray();
-            is = new ByteArrayInputStream( content );
-        }
-        else
-        {
-            is = (InputStream) source;
-        }
-
-        return is;
-    }
-
-    /**
-     * Create an output stream supporting the following types
-     *
-     * <ul>
-     *  <li>File</li>
-     *  <li>OutputStream</li>
-     * </ul>
-     *
-     * @param target the target object
-     * @return the output stream
-     * @throws IOException creating the output stream failed
-     */
-    private static OutputStream createOutputStream( Object target )
-        throws IOException
-    {
-        OutputStream os;
-
-        if( target instanceof File )
-        {
-            os = new FileOutputStream( (File) target );
-        }
-        else
-        {
-            os = (OutputStream) target;
-        }
-
-        return os;
-    }
-
     /**
      * Pumps the input stream to the output stream.
      *
@@ -300,26 +207,12 @@
      * @param os the target output stream
      * @return the number of bytes copied
      * @throws IOException the copying failed
+     * @deprecated use StreamUtil instead
      */
     public static long copy( InputStream is, OutputStream os )
         throws IOException
     {
-        byte[] buf = new byte[BUFFER_SIZE];
-        int n = 0;
-        long total = 0;
-
-        while ((n = is.read(buf)) > 0)
-        {
-            os.write(buf, 0, n);
-            total += n;
-        }
-
-        is.close();
-
-        os.flush();
-        os.close();
-
-        return total;
+        return StreamUtil.copy(is, os);
     }
 
     /**

Copied: turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/StreamUtil.java (from r678465, turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/CryptoUtil.java)
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/StreamUtil.java?p2=turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/StreamUtil.java&p1=turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/CryptoUtil.java&r1=678465&r2=678696&rev=678696&view=diff
==============================================================================
--- turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/CryptoUtil.java (original)
+++ turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/StreamUtil.java Tue Jul 22 02:55:57 2008
@@ -27,194 +27,23 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.security.GeneralSecurityException;
 
 /**
- * Helper class to provde generic functions to work with CryptoStreams.
- *
- * The code uses parts from Markus Hahn's Blowfish library found at
- * http://blowfishj.sourceforge.net/
+ * Helper class to provde generic stream functions.
  *
  * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl </a>
- * @author <a href="mailto:maakus@earthlink.net">Markus Hahn</a>
  */
 
-public final class CryptoUtil
+public final class StreamUtil
 {
     /** the size of the internal buffer to copy streams */
     private static final int BUFFER_SIZE = 1024;
 
     /**
-     * Copies from a source to a target object using encryption
-     *
-     * @param source the source object
-     * @param target the target object
-     * @param password the password to use for encryption
-     * @throws GeneralSecurityException accessing JCE failed
-     * @throws IOException accessing the souce failed
-     *
-     */
-    public static void encrypt( Object source, Object target, char[] password )
-        throws GeneralSecurityException, IOException
-    {
-        CryptoUtil.encrypt(
-            CryptoUtil.getCryptoStreamFactory(),
-            source,
-            target,
-            password
-            );
-    }
-
-    /**
-     * Copies from a source to a target object using encryption and a
-     * caller supplied CryptoStreamFactory.
-     *
-     * @param factory the factory to create the crypto streams
-     * @param source the source object
-     * @param target the target object
-     * @param password the password to use for encryption
-     * @throws GeneralSecurityException accessing JCE failed
-     * @throws IOException accessing the souce failed
-     */
-    public static void encrypt(
-        CryptoStreamFactory factory, Object source, Object target, char[] password )
-        throws GeneralSecurityException, IOException
-    {
-        InputStream is = CryptoUtil.createInputStream( source );
-        OutputStream os = CryptoUtil.createOutputStream( target );
-        OutputStream eos = factory.getOutputStream( os, password );
-        CryptoUtil.copy( is, eos );
-    }
-
-    /**
-     * Copies from a source to a target object using decryption.
-     *
-     * @param source the source object
-     * @param target the target object
-     * @param password the password to use for decryption
-     * @throws GeneralSecurityException accessing JCE failed
-     * @throws IOException accessing the souce failed
-     */
-    public static void decrypt( Object source, Object target, char[] password )
-        throws GeneralSecurityException, IOException
-    {
-        CryptoUtil.decrypt(
-            CryptoUtil.getCryptoStreamFactory(),
-            source,
-            target,
-            password
-            );
-    }
-
-    /**
-     * Copies from a source to a target object using decryption and a
-     * caller-suppier CryptoStreamFactory.
-     *
-     * @param factory the factory to create the crypto streams
-     * @param source the source object
-     * @param target the target object
-     * @param password the password to use for decryption
-     * @throws GeneralSecurityException accessing JCE failed
-     * @throws IOException accessing the souce failed
-     */
-    public static void decrypt(
-        CryptoStreamFactory factory, Object source, Object target, char[] password )
-        throws GeneralSecurityException, IOException
-    {
-        InputStream is = CryptoUtil.createInputStream( source );
-        OutputStream os = CryptoUtil.createOutputStream( target );
-        InputStream dis = factory.getInputStream( is, password );
-        CryptoUtil.copy( dis, os );
-    }
-
-    /**
-     * Encrypts a string into a hex string.
-     *
-     * @param plainText the plain text to be encrypted
-     * @param password the password for encryption
-     * @return the encrypted string
-     * @throws GeneralSecurityException accessing JCE failed
-     * @throws IOException accessing the souce failed
-     */
-    public static String encryptString( String plainText, char[] password )
-        throws GeneralSecurityException, IOException
-    {
-        return CryptoUtil.encryptString(
-            CryptoUtil.getCryptoStreamFactory(),
-            plainText,
-            password
-            );
-    }
-
-    /**
-     * Encrypts a string into a hex string.
-     *
-     * @param factory the factory to create the crypto streams
-     * @param plainText the plain text to be encrypted
-     * @param password the password for encryption
-     * @return the encrypted string
-     * @throws GeneralSecurityException accessing JCE failed
-     * @throws IOException accessing the souce failed
-     */
-    public static String encryptString(
-        CryptoStreamFactory factory, String plainText, char[] password )
-        throws GeneralSecurityException, IOException
-    {
-        ByteArrayOutputStream bais = new ByteArrayOutputStream();
-        CryptoUtil.encrypt( factory, plainText, bais, password );
-        return HexConverter.toString( bais.toByteArray() );
-    }
-
-    /**
-     * Decrypts an encrypted string into the plain text. The encrypted
-     * string must be a hex string created by encryptString.
-     *
-     * @param cipherText the encrypted text to be decrypted
-     * @param password the password for decryption
-     * @return the decrypted string
-     * @throws GeneralSecurityException accessing JCE failed
-     * @throws IOException accessing the souce failed
-     */
-    public static String decryptString( String cipherText, char[] password )
-        throws GeneralSecurityException, IOException
-    {
-        return CryptoUtil.decryptString(
-            CryptoUtil.getCryptoStreamFactory(),
-            cipherText,
-            password
-            );
-    }
-
-    /**
-     * Decrypts an encrypted string into the plain text. The encrypted
-     * string must be a hex string created by encryptString.
-     *
-     * @param factory the factory to create the crypto streams
-     * @param cipherText the encrypted text to be decrypted
-     * @param password the password for decryption
-     * @return the decrypted string
-     * @throws GeneralSecurityException accessing JCE failed
-     * @throws IOException accessing the souce failed
-     */
-    public static String decryptString(
-        CryptoStreamFactory factory, String cipherText, char[] password )
-        throws GeneralSecurityException, IOException
-    {
-        byte[] buffer = HexConverter.toBytes( cipherText );
-        ByteArrayOutputStream bais = new ByteArrayOutputStream();
-        CryptoUtil.decrypt( factory, buffer, bais, password );
-        return new String( bais.toByteArray(), "utf-8" );
-    }
-
-    ///////////////////////////////////////////////////////////////////////////
-    // Private Implementation
-    ///////////////////////////////////////////////////////////////////////////
-
-    /**
      * Create an input stream supporting the following types
      *
      * <ul>
-     *  <li>String</li>
+     *  <li>String (using the UTF-8 encoded content)</li>
      *  <li>File</li>
      *  <li>byte[]</li>
      *  <li>char[]</li>
@@ -224,9 +53,9 @@
      *
      * @param source the source object
      * @return the created input stream
-     * @throws IOException creating the input stream failed
+     * @throws java.io.IOException creating the input stream failed
      */
-    private static InputStream createInputStream( Object source )
+    public static InputStream createInputStream( Object source )
         throws IOException
     {
         InputStream is;
@@ -256,10 +85,14 @@
             byte[] content = ((ByteArrayOutputStream) source).toByteArray();
             is = new ByteArrayInputStream( content );
         }
-        else
+        else if( source instanceof InputStream )
         {
             is = (InputStream) source;
         }
+        else
+        {
+            throw new IllegalArgumentException("Don't know hot to handle " + source.getClass().getName());
+        }
 
         return is;
     }
@@ -269,26 +102,39 @@
      *
      * <ul>
      *  <li>File</li>
+     *  <li>String</li>
      *  <li>OutputStream</li>
      * </ul>
      *
      * @param target the target object
      * @return the output stream
-     * @throws IOException creating the output stream failed
+     * @throws java.io.IOException creating the output stream failed
      */
-    private static OutputStream createOutputStream( Object target )
+    public static OutputStream createOutputStream( Object target )
         throws IOException
     {
         OutputStream os;
 
         if( target instanceof File )
         {
-            os = new FileOutputStream( (File) target );
+            File currFile = (File) target;
+            createParentFile(currFile);
+            os = new FileOutputStream(currFile);
         }
-        else
+        else if( target instanceof String )
+        {
+            File currFile = new File((String) target);
+            createParentFile(currFile);
+            os = new FileOutputStream(currFile);
+        }
+        else if( target instanceof OutputStream )
         {
             os = (OutputStream) target;
         }
+        else
+        {
+            throw new IllegalArgumentException("Don't know hot to handle " + target.getClass().getName());
+        }
 
         return os;
     }
@@ -299,7 +145,7 @@
      * @param is the source input stream
      * @param os the target output stream
      * @return the number of bytes copied
-     * @throws IOException the copying failed
+     * @throws java.io.IOException the copying failed
      */
     public static long copy( InputStream is, OutputStream os )
         throws IOException
@@ -323,10 +169,18 @@
     }
 
     /**
-     * @return the CryptoStreamFactory to be used
+     * Ensure that the parent directories exists before writing to
+     * the file.
+     * 
+     * @param currFile the file to write to
      */
-    public static CryptoStreamFactory getCryptoStreamFactory()
+    private static void createParentFile(File currFile)
     {
-        return CryptoStreamFactoryImpl.getInstance();
+        File parentFile = currFile.getParentFile();
+        
+        if((parentFile != null) && !parentFile.exists())
+        {
+            parentFile.mkdirs();
+        }
     }
-}
+}
\ No newline at end of file

Modified: turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/CryptoUtilTest.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/CryptoUtilTest.java?rev=678696&r1=678695&r2=678696&view=diff
==============================================================================
--- turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/CryptoUtilTest.java (original)
+++ turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/CryptoUtilTest.java Tue Jul 22 02:55:57 2008
@@ -112,7 +112,7 @@
         testTextEncryption();
         File sourceFile = new File( this.getTempDataDirectory(), "plain.enc.txt" );
         File targetFile = new File( this.getTempDataDirectory(), "plain.dec.txt" );
-        CryptoUtil.decrypt( sourceFile, targetFile, this.getPassword() );
+        CryptoUtil.decrypt( sourceFile, targetFile.getAbsolutePath(), this.getPassword() );
     }
 
     /** Encrypt an empty text file */