Move test code out of "test" package
[fanfix.git] / src / be / nikiroo / utils / CryptUtils.java
index ea6892babd0a8c355c8182c364655e7493ea3ac0..b82a169ac21fffd4eb6d95f6df86c46a43566bfd 100644 (file)
@@ -1,15 +1,21 @@
 package be.nikiroo.utils;
 
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
 import java.security.InvalidKeyException;
 import java.security.NoSuchAlgorithmException;
 
 import javax.crypto.BadPaddingException;
 import javax.crypto.Cipher;
+import javax.crypto.CipherInputStream;
+import javax.crypto.CipherOutputStream;
 import javax.crypto.IllegalBlockSizeException;
 import javax.crypto.NoSuchPaddingException;
 import javax.crypto.SecretKey;
 import javax.crypto.spec.SecretKeySpec;
+import javax.net.ssl.SSLException;
 
 /**
  * Small utility class to do AES encryption/decryption.
@@ -58,6 +64,54 @@ public class CryptUtils {
                init(bytes32);
        }
 
+       /**
+        * Wrap the given {@link InputStream} so it is transparently encrypted by
+        * the current {@link CryptUtils}.
+        * 
+        * @param in
+        *            the {@link InputStream} to wrap
+        * @return the auto-encode {@link InputStream}
+        */
+       public InputStream encryptInputStream(InputStream in) {
+               return new CipherInputStream(in, ecipher);
+       }
+
+       /**
+        * Wrap the given {@link OutputStream} so it is transparently encrypted by
+        * the current {@link CryptUtils}.
+        * 
+        * @param in
+        *            the {@link OutputStream} to wrap
+        * @return the auto-encode {@link OutputStream}
+        */
+       public OutputStream encryptOutpuStream(OutputStream out) {
+               return new CipherOutputStream(out, ecipher);
+       }
+
+       /**
+        * Wrap the given {@link OutStream} so it is transparently decoded by the
+        * current {@link CryptUtils}.
+        * 
+        * @param in
+        *            the {@link InputStream} to wrap
+        * @return the auto-decode {@link InputStream}
+        */
+       public InputStream decryptInputStream(InputStream in) {
+               return new CipherInputStream(in, dcipher);
+       }
+
+       /**
+        * Wrap the given {@link OutStream} so it is transparently decoded by the
+        * current {@link CryptUtils}.
+        * 
+        * @param out
+        *            the {@link OutputStream} to wrap
+        * @return the auto-decode {@link OutputStream}
+        */
+       public OutputStream decryptOutputStream(OutputStream out) {
+               return new CipherOutputStream(out, dcipher);
+       }
+
        /**
         * This method required an array of 128 bytes.
         * 
@@ -104,17 +158,17 @@ public class CryptUtils {
         * 
         * @return the encrypted data
         * 
-        * @throws IOException
+        * @throws SSLException
         *             in case of I/O error (i.e., the data is not what you assumed
         *             it was)
         */
-       public byte[] encrypt(byte[] data) throws IOException {
+       public byte[] encrypt(byte[] data) throws SSLException {
                try {
                        return ecipher.doFinal(data);
                } catch (IllegalBlockSizeException e) {
-                       throw new IOException(e);
+                       throw new SSLException(e);
                } catch (BadPaddingException e) {
-                       throw new IOException(e);
+                       throw new SSLException(e);
                }
        }
 
@@ -126,12 +180,18 @@ public class CryptUtils {
         * 
         * @return the encrypted data
         * 
-        * @throws IOException
+        * @throws SSLException
         *             in case of I/O error (i.e., the data is not what you assumed
         *             it was)
         */
-       public byte[] encrypt(String data) throws IOException {
-               return encrypt(data.getBytes("UTF8"));
+       public byte[] encrypt(String data) throws SSLException {
+               try {
+                       return encrypt(data.getBytes("UTF8"));
+               } catch (UnsupportedEncodingException e) {
+                       // UTF-8 is required in all confirm JVMs
+                       e.printStackTrace();
+                       return null;
+               }
        }
 
        /**
@@ -146,12 +206,18 @@ public class CryptUtils {
         * 
         * @return the encrypted data, encoded in Base64
         * 
-        * @throws IOException
+        * @throws SSLException
         *             in case of I/O error (i.e., the data is not what you assumed
         *             it was)
         */
-       public String encrypt64(String data, boolean zip) throws IOException {
-               return encrypt64(data.getBytes("UTF8"), zip);
+       public String encrypt64(String data, boolean zip) throws SSLException {
+               try {
+                       return encrypt64(data.getBytes("UTF8"), zip);
+               } catch (UnsupportedEncodingException e) {
+                       // UTF-8 is required in all confirm JVMs
+                       e.printStackTrace();
+                       return null;
+               }
        }
 
        /**
@@ -166,12 +232,18 @@ public class CryptUtils {
         * 
         * @return the encrypted data, encoded in Base64
         * 
-        * @throws IOException
+        * @throws SSLException
         *             in case of I/O error (i.e., the data is not what you assumed
         *             it was)
         */
-       public String encrypt64(byte[] data, boolean zip) throws IOException {
-               return StringUtils.base64(encrypt(data), zip);
+       public String encrypt64(byte[] data, boolean zip) throws SSLException {
+               try {
+                       return StringUtils.base64(encrypt(data), zip);
+               } catch (IOException e) {
+                       // not exactly true, but we consider here that this error is a crypt
+                       // error, not a normal I/O error
+                       throw new SSLException(e);
+               }
        }
 
        /**
@@ -182,16 +254,16 @@ public class CryptUtils {
         * 
         * @return the original, decoded data
         * 
-        * @throws IOException
+        * @throws SSLException
         *             in case of I/O error
         */
-       public byte[] decrypt(byte[] data) throws IOException {
+       public byte[] decrypt(byte[] data) throws SSLException {
                try {
                        return dcipher.doFinal(data);
                } catch (IllegalBlockSizeException e) {
-                       throw new IOException(e);
+                       throw new SSLException(e);
                } catch (BadPaddingException e) {
-                       throw new IOException(e);
+                       throw new SSLException(e);
                }
        }
 
@@ -207,11 +279,17 @@ public class CryptUtils {
         * 
         * @return the original, decoded data
         * 
-        * @throws IOException
+        * @throws SSLException
         *             in case of I/O error
         */
-       public byte[] decrypt64(String data, boolean zip) throws IOException {
-               return decrypt(StringUtils.unbase64(data, zip));
+       public byte[] decrypt64(String data, boolean zip) throws SSLException {
+               try {
+                       return decrypt(StringUtils.unbase64(data, zip));
+               } catch (IOException e) {
+                       // not exactly true, but we consider here that this error is a crypt
+                       // error, not a normal I/O error
+                       throw new SSLException(e);
+               }
        }
 
        /**
@@ -227,11 +305,21 @@ public class CryptUtils {
         * 
         * @return the original, decoded data
         * 
-        * @throws IOException
+        * @throws SSLException
         *             in case of I/O error
         */
-       public String decrypt64s(String data, boolean zip) throws IOException {
-               return new String(decrypt(StringUtils.unbase64(data, zip)), "UTF-8");
+       public String decrypt64s(String data, boolean zip) throws SSLException {
+               try {
+                       return new String(decrypt(StringUtils.unbase64(data, zip)), "UTF-8");
+               } catch (UnsupportedEncodingException e) {
+                       // UTF-8 is required in all confirm JVMs
+                       e.printStackTrace();
+                       return null;
+               } catch (IOException e) {
+                       // not exactly true, but we consider here that this error is a crypt
+                       // error, not a normal I/O error
+                       throw new SSLException(e);
+               }
        }
 
        /**
@@ -243,6 +331,6 @@ public class CryptUtils {
         * @return a 128 bits key computed from the given input
         */
        static private byte[] key2key(String input) {
-               return StringUtils.getMd5Hash(input).getBytes();
+               return StringUtils.getMd5Hash("" + input).getBytes();
        }
 }