Repository: wicket
Updated Branches:
  refs/heads/WICKET-5768-improve-crypt [created] 1ffc984ca


WICKET-5768 Improve class hierarchy of package
org.apache.wicket.util.crypt


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/1ffc984c
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/1ffc984c
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/1ffc984c

Branch: refs/heads/WICKET-5768-improve-crypt
Commit: 1ffc984ca1a8c897a91afc05a83f0793dadb14d4
Parents: 1cb35a0
Author: Andrea Del Bene <[email protected]>
Authored: Tue Nov 18 22:23:09 2014 +0100
Committer: Andrea Del Bene <[email protected]>
Committed: Wed Nov 19 21:05:40 2014 +0100

----------------------------------------------------------------------
 .../AbstractKeyInSessionJceCryptFactory.java    |  88 ++++++++++++
 .../crypt/KeyInSessionSunJceCryptFactory.java   |  44 ++----
 .../markup/html/form/encryption/CryptTest.java  |   3 +-
 .../CryptedUrlWebRequestCodingStrategyTest.java |   8 +-
 .../apache/wicket/util/crypt/AbstractCrypt.java | 136 +++++++++----------
 .../util/crypt/CachingSunJceCryptFactory.java   |   2 +-
 .../wicket/util/crypt/ClassCryptFactory.java    |  46 +++++--
 .../org/apache/wicket/util/crypt/ICrypt.java    |   9 --
 .../org/apache/wicket/util/crypt/NoCrypt.java   |  16 +--
 .../apache/wicket/util/crypt/SunJceCrypt.java   |  35 ++++-
 .../apache/wicket/util/crypt/TrivialCrypt.java  |  38 ------
 ...UnlimitedStrengthJurisdictionPolicyTest.java |   4 +-
 12 files changed, 238 insertions(+), 191 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/1ffc984c/wicket-core/src/main/java/org/apache/wicket/core/util/crypt/AbstractKeyInSessionJceCryptFactory.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/core/util/crypt/AbstractKeyInSessionJceCryptFactory.java
 
b/wicket-core/src/main/java/org/apache/wicket/core/util/crypt/AbstractKeyInSessionJceCryptFactory.java
new file mode 100644
index 0000000..77ffa27
--- /dev/null
+++ 
b/wicket-core/src/main/java/org/apache/wicket/core/util/crypt/AbstractKeyInSessionJceCryptFactory.java
@@ -0,0 +1,88 @@
+/*
+ * 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.wicket.core.util.crypt;
+
+
+import java.io.Serializable;
+
+import org.apache.wicket.MetaDataKey;
+import org.apache.wicket.Session;
+import org.apache.wicket.util.crypt.ICrypt;
+
+/**
+ * Base class to implement crypt factories that store crypt into user session.
+ * Note that the use of this crypt factory will result in an immediate 
creation of a http session.
+ * 
+ * @author andrea del bene
+ *
+ * @param <T>
+ *                             the type for the secret key.
+ */
+public abstract class AbstractKeyInSessionJceCryptFactory<T extends 
Serializable>
+{
+       /** metadata-key used to store crypto-key in session metadata */
+       static final MetaDataKey<Serializable> KEY = new 
MetaDataKey<Serializable>()
+       {
+               private static final long serialVersionUID = 1L;
+       };
+       
+       public AbstractKeyInSessionJceCryptFactory()
+       {
+               super();
+       }
+       
+       /**
+        * Creates a new crypt for the current user session. If no user session 
is 
+        * available, a new one is created.
+        * 
+        * @return
+        */
+       @SuppressWarnings("unchecked")
+       public ICrypt newCrypt()
+       {
+               Session session = Session.get();
+               session.bind();
+       
+               // retrieve or generate encryption key from session
+               Serializable key = session.getMetaData(KEY);
+               if (key == null)
+               {
+                       // generate new key
+                       key = generateKey(session);
+                       session.setMetaData(KEY, key);
+               }
+       
+               // build the crypt based on session key
+               ICrypt crypt = createCrypt((T)key);
+               return crypt;
+       }
+
+       /**
+        * Generates the secret key for a new crypt.
+        * 
+        * @param session
+        *                              the current user session where crypt 
will be stored
+        * @return
+        *                              the secret key for a new crypt
+        */
+       protected abstract T generateKey(Session session);      
+
+       /**
+        * @return the {@link org.apache.wicket.util.crypt.ICrypt} to use
+        */
+       protected abstract ICrypt createCrypt(T key);   
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/wicket/blob/1ffc984c/wicket-core/src/main/java/org/apache/wicket/core/util/crypt/KeyInSessionSunJceCryptFactory.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/core/util/crypt/KeyInSessionSunJceCryptFactory.java
 
b/wicket-core/src/main/java/org/apache/wicket/core/util/crypt/KeyInSessionSunJceCryptFactory.java
index 5b3bae6..9158edd 100644
--- 
a/wicket-core/src/main/java/org/apache/wicket/core/util/crypt/KeyInSessionSunJceCryptFactory.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/core/util/crypt/KeyInSessionSunJceCryptFactory.java
@@ -18,7 +18,6 @@ package org.apache.wicket.core.util.crypt;
 
 import java.util.UUID;
 
-import org.apache.wicket.MetaDataKey;
 import org.apache.wicket.Session;
 import org.apache.wicket.util.crypt.ICrypt;
 import org.apache.wicket.util.crypt.ICryptFactory;
@@ -34,15 +33,9 @@ import org.apache.wicket.util.lang.Args;
  *
  * @author igor.vaynberg
  */
-public class KeyInSessionSunJceCryptFactory implements ICryptFactory
-{
-       /** metadata-key used to store crypto-key in session metadata */
-       private static final MetaDataKey<String> KEY = new MetaDataKey<String>()
-       {
-               private static final long serialVersionUID = 1L;
-       };
-
-       private final String cryptMethod;
+public class KeyInSessionSunJceCryptFactory extends 
AbstractKeyInSessionJceCryptFactory<String> implements ICryptFactory
+{      
+       final String cryptMethod;
 
        /**
         * Constructor using {@link javax.crypto.Cipher} {@value 
org.apache.wicket.util.crypt.SunJceCrypt#DEFAULT_CRYPT_METHOD}
@@ -62,33 +55,16 @@ public class KeyInSessionSunJceCryptFactory implements 
ICryptFactory
        {
                this.cryptMethod = Args.notNull(cryptMethod, "Crypt method");
        }
-
+       
        @Override
-       public ICrypt newCrypt()
+       protected String generateKey(Session session)
        {
-               Session session = Session.get();
-               session.bind();
-
-               // retrieve or generate encryption key from session
-               String key = session.getMetaData(KEY);
-               if (key == null)
-               {
-                       // generate new key
-                       key = session.getId() + "." + 
UUID.randomUUID().toString();
-                       session.setMetaData(KEY, key);
-               }
-
-               // build the crypt based on session key
-               ICrypt crypt = createCrypt();
-               crypt.setKey(key);
-               return crypt;
+               return session.getId() + "." + UUID.randomUUID().toString();
        }
-
-       /**
-        * @return the {@link org.apache.wicket.util.crypt.ICrypt} to use
-        */
-       protected ICrypt createCrypt()
+       
+       @Override
+       protected ICrypt createCrypt(String key)
        {
-               return new SunJceCrypt(cryptMethod);
+               return new SunJceCrypt(cryptMethod, key);
        }
 }

http://git-wip-us.apache.org/repos/asf/wicket/blob/1ffc984c/wicket-core/src/test/java/org/apache/wicket/markup/html/form/encryption/CryptTest.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/encryption/CryptTest.java
 
b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/encryption/CryptTest.java
index 72024c6..052f6ef 100644
--- 
a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/encryption/CryptTest.java
+++ 
b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/encryption/CryptTest.java
@@ -31,8 +31,7 @@ public class CryptTest extends WicketTestCase
        @Test
        public void crypt()
        {
-               final ICrypt crypt = new SunJceCrypt();
-               crypt.setKey("someStableKey");
+               final ICrypt crypt = new 
SunJceCrypt(SunJceCrypt.DEFAULT_CRYPT_METHOD, "someStableKey");
 
                try
                {

http://git-wip-us.apache.org/repos/asf/wicket/blob/1ffc984c/wicket-core/src/test/java/org/apache/wicket/protocol/http/request/CryptedUrlWebRequestCodingStrategyTest.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/protocol/http/request/CryptedUrlWebRequestCodingStrategyTest.java
 
b/wicket-core/src/test/java/org/apache/wicket/protocol/http/request/CryptedUrlWebRequestCodingStrategyTest.java
index eab676d..2ff14be 100644
--- 
a/wicket-core/src/test/java/org/apache/wicket/protocol/http/request/CryptedUrlWebRequestCodingStrategyTest.java
+++ 
b/wicket-core/src/test/java/org/apache/wicket/protocol/http/request/CryptedUrlWebRequestCodingStrategyTest.java
@@ -17,12 +17,12 @@
 package org.apache.wicket.protocol.http.request;
 
 import org.apache.wicket.WicketTestCase;
+import org.apache.wicket.core.request.mapper.CryptoMapper;
 import org.apache.wicket.markup.html.WebPage;
 import org.apache.wicket.markup.html.basic.SimplePage;
 import org.apache.wicket.mock.MockApplication;
 import org.apache.wicket.protocol.http.WebApplication;
 import org.apache.wicket.request.mapper.CompoundRequestMapper;
-import org.apache.wicket.core.request.mapper.CryptoMapper;
 import org.apache.wicket.util.crypt.Base64;
 import org.apache.wicket.util.crypt.ICrypt;
 import org.apache.wicket.util.crypt.ICryptFactory;
@@ -118,12 +118,6 @@ public class CryptedUrlWebRequestCodingStrategyTest 
extends WicketTestCase
                                {
                                        return new String(new 
Base64(true).encode(plainText.getBytes()));
                                }
-
-                               @Override
-                               public void setKey(String key)
-                               {
-                               }
-
                        };
                }
        }

http://git-wip-us.apache.org/repos/asf/wicket/blob/1ffc984c/wicket-util/src/main/java/org/apache/wicket/util/crypt/AbstractCrypt.java
----------------------------------------------------------------------
diff --git 
a/wicket-util/src/main/java/org/apache/wicket/util/crypt/AbstractCrypt.java 
b/wicket-util/src/main/java/org/apache/wicket/util/crypt/AbstractCrypt.java
index 9daa2dd..5f68192 100644
--- a/wicket-util/src/main/java/org/apache/wicket/util/crypt/AbstractCrypt.java
+++ b/wicket-util/src/main/java/org/apache/wicket/util/crypt/AbstractCrypt.java
@@ -16,12 +16,13 @@
  */
 package org.apache.wicket.util.crypt;
 
+import java.io.Serializable;
 import java.io.UnsupportedEncodingException;
 import java.security.GeneralSecurityException;
-import java.util.UUID;
 
 import javax.crypto.Cipher;
 
+import org.apache.wicket.util.lang.Args;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -31,7 +32,7 @@ import org.slf4j.LoggerFactory;
  * 
  * @author Juergen Donnerstag
  */
-public abstract class AbstractCrypt implements ICrypt
+public abstract class AbstractCrypt<T extends Serializable> implements ICrypt
 {
        /** Encoding used to convert java String from and to byte[] */
        private static final String CHARACTER_ENCODING = "UTF-8";
@@ -40,90 +41,30 @@ public abstract class AbstractCrypt implements ICrypt
        private static final Logger log = 
LoggerFactory.getLogger(AbstractCrypt.class);
 
        /** Key used to de-/encrypt the data */
-       private String encryptionKey;
-
+       private final T encryptionKey;
+       
        /**
         * Constructor
-        */
-       public AbstractCrypt()
-       {
-               this.encryptionKey = UUID.randomUUID().toString();
-       }
-
-       /**
-        * Decrypts a string into a string.
         * 
-        * @param text
-        *            text to decrypt
-        * @return the decrypted text
+        * @param encryptionKey
+        *                              the encryption key to use
         */
-       @Override
-       public final String decryptUrlSafe(final String text)
+       public AbstractCrypt(final T encryptionKey)
        {
-               try
-               {
-                       byte[] decoded = new Base64(true).decode(text);
-                       return new String(decryptByteArray(decoded), 
CHARACTER_ENCODING);
-               }
-               catch (Exception ex)
-               {
-                       log.debug("Error decoding text: " + text, ex);
-                       return null;
-               }
+               this.encryptionKey = Args.notNull(encryptionKey, 
"encryptionKey");
        }
-
-       /**
-        * Encrypt a string into a string using URL safe Base64 encoding.
-        * 
-        * @param plainText
-        *            text to encrypt
-        * @return encrypted string
-        */
-       @Override
-       public final String encryptUrlSafe(final String plainText)
-       {
-               try
-               {
-                       byte[] encrypted = encryptStringToByteArray(plainText);
-                       Base64 base64 = new Base64(-1, null, true);
-                       byte[] encoded = base64.encode(encrypted);
-                       return new String(encoded, CHARACTER_ENCODING);
-               }
-               catch (GeneralSecurityException e)
-               {
-                       log.error("Unable to encrypt text '" + plainText + "'", 
e);
-                       return null;
-               }
-               catch (UnsupportedEncodingException e)
-               {
-                       log.error("Unable to encrypt text '" + plainText + "'", 
e);
-                       return null;
-               }
-       }
-
+       
        /**
         * Get encryption private key
         * 
         * @return encryption private key
         */
-       public String getKey()
+       public T getKey()
        {
                return encryptionKey;
        }
 
        /**
-        * Set encryption private key
-        * 
-        * @param key
-        *            private key to make de-/encryption unique
-        */
-       @Override
-       public void setKey(final String key)
-       {
-               encryptionKey = key;
-       }
-
-       /**
         * Crypts the given byte array
         * 
         * @param input
@@ -143,7 +84,7 @@ public abstract class AbstractCrypt implements ICrypt
         *            byte array to decrypt
         * @return the decrypted text
         */
-       private byte[] decryptByteArray(final byte[] encrypted)
+       protected final byte[] decryptByteArray(final byte[] encrypted)
        {
                try
                {
@@ -164,7 +105,7 @@ public abstract class AbstractCrypt implements ICrypt
         * @return the string encrypted
         * @throws GeneralSecurityException
         */
-       private byte[] encryptStringToByteArray(final String plainText)
+       protected final byte[] encryptStringToByteArray(final String plainText)
                throws GeneralSecurityException
        {
                try
@@ -176,4 +117,55 @@ public abstract class AbstractCrypt implements ICrypt
                        throw new RuntimeException(ex.getMessage());
                }
        }
+       
+       /**
+        * Decrypts a string into a string.
+        * 
+        * @param text
+        *            text to decrypt
+        * @return the decrypted text
+        */
+       @Override
+       public String decryptUrlSafe(final String text)
+       {
+               try
+               {
+                       byte[] decoded = new Base64(true).decode(text);
+                       return new String(decryptByteArray(decoded), 
CHARACTER_ENCODING);
+               }
+               catch (Exception ex)
+               {
+                       log.debug("Error decoding text: " + text, ex);
+                       return null;
+               }
+       }
+
+       /**
+        * Encrypt a string into a string using URL safe Base64 encoding.
+        * 
+        * @param plainText
+        *            text to encrypt
+        * @return encrypted string
+        */
+       @Override
+       public String encryptUrlSafe(final String plainText)
+       {
+               try
+               {
+                       byte[] encrypted = encryptStringToByteArray(plainText);
+                       Base64 base64 = new Base64(-1, null, true);
+                       byte[] encoded = base64.encode(encrypted);
+                       return new String(encoded, CHARACTER_ENCODING);
+               }
+               catch (GeneralSecurityException e)
+               {
+                       log.error("Unable to encrypt text '" + plainText + "'", 
e);
+                       return null;
+               }
+               catch (UnsupportedEncodingException e)
+               {
+                       log.error("Unable to encrypt text '" + plainText + "'", 
e);
+                       return null;
+               }
+       }
 }

http://git-wip-us.apache.org/repos/asf/wicket/blob/1ffc984c/wicket-util/src/main/java/org/apache/wicket/util/crypt/CachingSunJceCryptFactory.java
----------------------------------------------------------------------
diff --git 
a/wicket-util/src/main/java/org/apache/wicket/util/crypt/CachingSunJceCryptFactory.java
 
b/wicket-util/src/main/java/org/apache/wicket/util/crypt/CachingSunJceCryptFactory.java
index 65db9e4..b521403 100644
--- 
a/wicket-util/src/main/java/org/apache/wicket/util/crypt/CachingSunJceCryptFactory.java
+++ 
b/wicket-util/src/main/java/org/apache/wicket/util/crypt/CachingSunJceCryptFactory.java
@@ -32,6 +32,6 @@ public class CachingSunJceCryptFactory extends 
CryptFactoryCachingDecorator
         */
        public CachingSunJceCryptFactory(final String encryptionKey)
        {
-               super(new ClassCryptFactory(SunJceCrypt.class, encryptionKey));
+               super(new ClassCryptFactory(SunJceCrypt.class, 
SunJceCrypt.DEFAULT_CRYPT_METHOD, encryptionKey));
        }
 }

http://git-wip-us.apache.org/repos/asf/wicket/blob/1ffc984c/wicket-util/src/main/java/org/apache/wicket/util/crypt/ClassCryptFactory.java
----------------------------------------------------------------------
diff --git 
a/wicket-util/src/main/java/org/apache/wicket/util/crypt/ClassCryptFactory.java 
b/wicket-util/src/main/java/org/apache/wicket/util/crypt/ClassCryptFactory.java
index b2e260c..d1aaca8 100644
--- 
a/wicket-util/src/main/java/org/apache/wicket/util/crypt/ClassCryptFactory.java
+++ 
b/wicket-util/src/main/java/org/apache/wicket/util/crypt/ClassCryptFactory.java
@@ -17,6 +17,7 @@
 package org.apache.wicket.util.crypt;
 
 import java.lang.ref.WeakReference;
+import java.lang.reflect.Constructor;
 
 import org.apache.wicket.util.lang.Args;
 import org.slf4j.Logger;
@@ -33,38 +34,40 @@ public class ClassCryptFactory implements ICryptFactory
 {
        private static final Logger log = 
LoggerFactory.getLogger(ClassCryptFactory.class);
 
-       private final WeakReference<Class<?>> cryptClass;
-       private final String encryptionKey;
+       private final WeakReference<Class<? extends ICrypt>> cryptClass;
+       private final Object[] constructorArgs;
 
        /**
         * Construct.
         * 
         * @param cryptClass
         *            class that will be instantiated to represent the ICrypt 
object
-        * @param encryptionKey
-        *            encryption key
+        * @param constructorArgs
+        *            the arguments to instantiate the crypt class.
         */
-       public ClassCryptFactory(final Class<?> cryptClass, final String 
encryptionKey)
+       public ClassCryptFactory(final Class<? extends ICrypt> cryptClass, 
final Object... constructorArgs)
        {
                Args.notNull(cryptClass, "cryptClass");
+               Args.notNull(constructorArgs, "constructorArgs");
 
                if (!ICrypt.class.isAssignableFrom(cryptClass))
                {
                        throw new IllegalArgumentException("cryptClass must 
implement ICrypt interface");
                }
 
-               this.cryptClass = new WeakReference<Class<?>>(cryptClass);
-               this.encryptionKey = encryptionKey;
+               this.cryptClass = new WeakReference<Class<?extends 
ICrypt>>(cryptClass);
+               this.constructorArgs = constructorArgs;
        }
-
+       
        @Override
        public ICrypt newCrypt()
        {
                try
                {
-                       ICrypt crypt = (ICrypt)(cryptClass.get()).newInstance();
-                       log.info("using encryption/decryption object {}", 
crypt);
-                       crypt.setKey(encryptionKey);
+                       Constructor<? extends ICrypt> constructor =
+                               findConstructor(cryptClass.get(), 
constructorArgs);
+                       ICrypt crypt = constructor.newInstance(constructorArgs);
+                       log.info("using encryption/decryption object {}", 
crypt);                       
                        return crypt;
                }
                catch (Exception e)
@@ -93,4 +96,25 @@ public class ClassCryptFactory implements ICryptFactory
                        return new NoCrypt();
                }
        }
+       
+       private Constructor<? extends ICrypt> findConstructor(final Class<? 
extends ICrypt> clazz, final Object... constructorArgs) 
+               throws NoSuchMethodException, SecurityException
+       {
+               Class<?>[] argsType = getArrayElementsType(constructorArgs);
+               
+               return clazz.getConstructor(argsType);
+       }
+       
+       private Class<?>[] getArrayElementsType(Object[] array)
+       {
+               Class<?>[] elementsClass = new Class[array.length];
+               
+               for (int i = 0; i< array.length; i++)
+               {
+                       Object element = array[i];
+                       elementsClass[i] = element.getClass();
+               }
+               
+               return elementsClass;
+       }
 }

http://git-wip-us.apache.org/repos/asf/wicket/blob/1ffc984c/wicket-util/src/main/java/org/apache/wicket/util/crypt/ICrypt.java
----------------------------------------------------------------------
diff --git a/wicket-util/src/main/java/org/apache/wicket/util/crypt/ICrypt.java 
b/wicket-util/src/main/java/org/apache/wicket/util/crypt/ICrypt.java
index 7f28ad3..079d257 100644
--- a/wicket-util/src/main/java/org/apache/wicket/util/crypt/ICrypt.java
+++ b/wicket-util/src/main/java/org/apache/wicket/util/crypt/ICrypt.java
@@ -54,13 +54,4 @@ public interface ICrypt
         * @since 1.2
         */
        String encryptUrlSafe(final String plainText);
-
-       /**
-        * Sets private encryption key. It depends on the implementation if a 
default key is applied or
-        * an exception is thrown, if no private key has been provided.
-        * 
-        * @param key
-        *            private key
-        */
-       void setKey(final String key);
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/wicket/blob/1ffc984c/wicket-util/src/main/java/org/apache/wicket/util/crypt/NoCrypt.java
----------------------------------------------------------------------
diff --git 
a/wicket-util/src/main/java/org/apache/wicket/util/crypt/NoCrypt.java 
b/wicket-util/src/main/java/org/apache/wicket/util/crypt/NoCrypt.java
index be16986..7c14aa0 100644
--- a/wicket-util/src/main/java/org/apache/wicket/util/crypt/NoCrypt.java
+++ b/wicket-util/src/main/java/org/apache/wicket/util/crypt/NoCrypt.java
@@ -16,6 +16,7 @@
  */
 package org.apache.wicket.util.crypt;
 
+import java.security.GeneralSecurityException;
 
 /**
  * Due to legal reasons in some countries the JRE is shipped without a 
security provider. As a
@@ -25,13 +26,11 @@ package org.apache.wicket.util.crypt;
  * 
  * @author Juergen Donnerstag
  */
-public class NoCrypt implements ICrypt
+public class NoCrypt extends AbstractCrypt<String>
 {
-       /**
-        * Constructor
-        */
        public NoCrypt()
        {
+               super("");
        }
 
        /**
@@ -60,14 +59,9 @@ public class NoCrypt implements ICrypt
                return plainText;
        }
 
-       /**
-        * Set encryption private key
-        * 
-        * @param key
-        *            private key to make de-/encryption unique
-        */
        @Override
-       public void setKey(final String key)
+       protected byte[] crypt(byte[] input, int mode) throws 
GeneralSecurityException
        {
+               return input;
        }
 }

http://git-wip-us.apache.org/repos/asf/wicket/blob/1ffc984c/wicket-util/src/main/java/org/apache/wicket/util/crypt/SunJceCrypt.java
----------------------------------------------------------------------
diff --git 
a/wicket-util/src/main/java/org/apache/wicket/util/crypt/SunJceCrypt.java 
b/wicket-util/src/main/java/org/apache/wicket/util/crypt/SunJceCrypt.java
index 934f586..941ffe9 100644
--- a/wicket-util/src/main/java/org/apache/wicket/util/crypt/SunJceCrypt.java
+++ b/wicket-util/src/main/java/org/apache/wicket/util/crypt/SunJceCrypt.java
@@ -23,6 +23,7 @@ import java.security.Security;
 import java.security.spec.AlgorithmParameterSpec;
 import java.security.spec.InvalidKeySpecException;
 import java.security.spec.KeySpec;
+import java.util.UUID;
 
 import javax.crypto.Cipher;
 import javax.crypto.SecretKey;
@@ -41,7 +42,7 @@ import org.apache.wicket.util.lang.Args;
  * 
  * @author Juergen Donnerstag
  */
-public class SunJceCrypt extends AbstractCrypt
+public class SunJceCrypt extends AbstractCrypt<String>
 {
        /**
         * Iteration count used in combination with the salt to create the 
encryption key.
@@ -76,8 +77,25 @@ public class SunJceCrypt extends AbstractCrypt
         */
        public SunJceCrypt(String cryptMethod)
        {
-               this.cryptMethod = Args.notNull(cryptMethod, "Crypt method");
+               this(cryptMethod, generateRandomKey());
+       }
 
+       public SunJceCrypt(String cryptMethod, String key)
+       {
+               super(key);
+               this.cryptMethod = Args.notNull(cryptMethod, "Crypt method");
+               
+               checkChiperIsSupported(cryptMethod);
+       }
+       
+       /**
+        * Check if the current cipher is supported by the underlying JVM.
+        * 
+        * @param cryptMethod
+        *                              the name of encryption method (the 
cipher) 
+        */
+       private void checkChiperIsSupported(String cryptMethod)
+       {
                if (Security.getProviders("Cipher." + cryptMethod).length > 0)
                {
                        return; // we are good to go!
@@ -94,7 +112,7 @@ public class SunJceCrypt extends AbstractCrypt
                        throw new RuntimeException("Unable to load SunJCE 
service provider", ex);
                }
        }
-
+       
        /**
         * Crypts the given byte array
         * 
@@ -105,7 +123,6 @@ public class SunJceCrypt extends AbstractCrypt
         * @return the input crypted. Null in case of an error
         * @throws GeneralSecurityException
         */
-       @Override
        protected byte[] crypt(final byte[] input, final int mode)
                throws GeneralSecurityException
        {
@@ -169,4 +186,14 @@ public class SunJceCrypt extends AbstractCrypt
        {
                return new PBEKeySpec(getKey().toCharArray());
        }
+
+       /**
+        * Generate a random string key using {@code 
java.util.UUID.generateRandomKey()}.
+        * 
+        * @return the random string key
+        */
+       protected static String generateRandomKey()
+       {
+               return UUID.randomUUID().toString();
+       }
 }

http://git-wip-us.apache.org/repos/asf/wicket/blob/1ffc984c/wicket-util/src/main/java/org/apache/wicket/util/crypt/TrivialCrypt.java
----------------------------------------------------------------------
diff --git 
a/wicket-util/src/main/java/org/apache/wicket/util/crypt/TrivialCrypt.java 
b/wicket-util/src/main/java/org/apache/wicket/util/crypt/TrivialCrypt.java
deleted file mode 100644
index 5aab8e7..0000000
--- a/wicket-util/src/main/java/org/apache/wicket/util/crypt/TrivialCrypt.java
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * 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.wicket.util.crypt;
-
-import java.security.GeneralSecurityException;
-
-/**
- * THIS CLASS IS FOR TESTING PURPOSES ONLY. DO NOT USE IT IN PRODUCTION CODE!
- * 
- * @author Jonathan Locke
- */
-public class TrivialCrypt extends AbstractCrypt
-{
-       @Override
-       protected byte[] crypt(final byte[] input, final int mode) throws 
GeneralSecurityException
-       {
-               final byte[] result = new byte[input.length];
-               for (int i = 0; i < input.length; i++)
-               {
-                       result[i] = (byte)(input[i] ^ 0xff);
-               }
-               return result;
-       }
-}

http://git-wip-us.apache.org/repos/asf/wicket/blob/1ffc984c/wicket-util/src/test/java/org/apache/wicket/util/crypt/UnlimitedStrengthJurisdictionPolicyTest.java
----------------------------------------------------------------------
diff --git 
a/wicket-util/src/test/java/org/apache/wicket/util/crypt/UnlimitedStrengthJurisdictionPolicyTest.java
 
b/wicket-util/src/test/java/org/apache/wicket/util/crypt/UnlimitedStrengthJurisdictionPolicyTest.java
index 19515e0..6e11b44 100644
--- 
a/wicket-util/src/test/java/org/apache/wicket/util/crypt/UnlimitedStrengthJurisdictionPolicyTest.java
+++ 
b/wicket-util/src/test/java/org/apache/wicket/util/crypt/UnlimitedStrengthJurisdictionPolicyTest.java
@@ -46,7 +46,7 @@ public class UnlimitedStrengthJurisdictionPolicyTest extends 
Assert
                boolean unlimitedStrengthJurisdictionPolicyInstalled = 
SunJceCryptTest.isUnlimitedStrengthJurisdictionPolicyInstalled();
                Assume.assumeThat(unlimitedStrengthJurisdictionPolicyInstalled, 
is(true));
 
-               AbstractCrypt crypt = new 
UnlimitedStrenghtJurisdictionPolicyCrypt();
+               AbstractCrypt<String> crypt = new 
UnlimitedStrenghtJurisdictionPolicyCrypt();
 
                String input1 = "input1";
                byte[] encrypted = crypt.crypt(input1.getBytes(), 
Cipher.ENCRYPT_MODE);
@@ -64,7 +64,7 @@ public class UnlimitedStrengthJurisdictionPolicyTest extends 
Assert
        /**
         * Based on http://stackoverflow.com/a/992413
         */
-       private static class UnlimitedStrenghtJurisdictionPolicyCrypt extends 
AbstractCrypt
+       private static class UnlimitedStrenghtJurisdictionPolicyCrypt extends 
SunJceCrypt
        {
                private final Cipher crypter;
                private final Cipher decrypter;

Reply via email to