[jboss-svn-commits] JBL Code SVN: r30400 - in labs/jbossesb/workspace/performance/perf2/product/rosetta: src/org/jboss/soa/esb/services/security/util and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Nov 30 23:15:13 EST 2009


Author: beve
Date: 2009-11-30 23:15:13 -0500 (Mon, 30 Nov 2009)
New Revision: 30400

Modified:
   labs/jbossesb/workspace/performance/perf2/product/rosetta/src/org/jboss/soa/esb/services/security/PublicCryptoUtil.java
   labs/jbossesb/workspace/performance/perf2/product/rosetta/src/org/jboss/soa/esb/services/security/util/CryptoUtil.java
   labs/jbossesb/workspace/performance/perf2/product/rosetta/tests/src/org/jboss/soa/esb/services/security/PublicCryptoUtilUnitTest.java
Log:
Performance: Improve PublicCryptoUtil so that it does not create a new Cipher object the while look of encrypt.


Modified: labs/jbossesb/workspace/performance/perf2/product/rosetta/src/org/jboss/soa/esb/services/security/PublicCryptoUtil.java
===================================================================
--- labs/jbossesb/workspace/performance/perf2/product/rosetta/src/org/jboss/soa/esb/services/security/PublicCryptoUtil.java	2009-11-30 19:27:43 UTC (rev 30399)
+++ labs/jbossesb/workspace/performance/perf2/product/rosetta/src/org/jboss/soa/esb/services/security/PublicCryptoUtil.java	2009-12-01 04:15:13 UTC (rev 30400)
@@ -31,12 +31,15 @@
 import java.security.KeyStore;
 import java.security.KeyStoreException;
 import java.security.NoSuchAlgorithmException;
-import java.security.PrivateKey;
 import java.security.PublicKey;
 import java.security.UnrecoverableKeyException;
 import java.security.cert.Certificate;
 import java.security.cert.CertificateException;
 
+import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
+import javax.crypto.IllegalBlockSizeException;
+
 import org.apache.log4j.Logger;
 import org.jboss.soa.esb.common.Configuration;
 import org.jboss.soa.esb.common.Environment;
@@ -84,7 +87,8 @@
             return null;
         }
     
-        ByteArrayInputStream plainInStream;
+        final ByteArrayOutputStream encryptedOutStream = new ByteArrayOutputStream();
+        final ByteArrayInputStream plainInStream;
         try
         {
             plainInStream = new ByteArrayInputStream(getBytes(object));
@@ -94,24 +98,31 @@
             throw new SecurityServiceException(e.getMessage(), e);
         }
     
-        ByteArrayOutputStream encryptedOutStream = new ByteArrayOutputStream();
-    
         try
         {
             byte[] buf = new byte[100];
             int bufLength;
+            final Cipher cipher = CryptoUtil.getCipherForEncryption(transformation, publicKey);
             while ( (bufLength = plainInStream.read(buf)) != -1)
             {
-                byte[] tmp = CryptoUtil.encrypt(copyBytes(buf,bufLength), publicKey, transformation);
+                byte[] tmp = cipher.doFinal(copyBytes(buf, bufLength));
                 encryptedOutStream.write(tmp);
-                encryptedOutStream.flush();
             }
+            encryptedOutStream.flush();
             return encryptedOutStream.toByteArray();
         }
         catch (final IOException e)
         {
             throw new SecurityServiceException(e.getMessage(), e);
         }
+        catch (IllegalBlockSizeException e)
+        {
+            throw new SecurityServiceException(e.getMessage(), e);
+        }
+        catch (BadPaddingException e)
+        {
+            throw new SecurityServiceException(e.getMessage(), e);
+        }
     }
 
     public Serializable decrypt(final byte[] bytes) throws SecurityServiceException
@@ -120,27 +131,35 @@
         {
             return null;
         }
-        ByteArrayInputStream encryptedBytesInStream = new ByteArrayInputStream(bytes);
+        final ByteArrayInputStream encryptedBytesInStream = new ByteArrayInputStream(bytes);
+        final ByteArrayOutputStream decryptedBytesOutStream = new ByteArrayOutputStream();
     
-        ByteArrayOutputStream decryptedBytesOutStream = new ByteArrayOutputStream();
-    
         byte[] decryptBytes = null;
         try
         {
             byte[] buf = new byte[128];
-            int bufLenth;
-            while ( (bufLenth = encryptedBytesInStream.read(buf)) != -1)
+            int bufLength;
+            final Cipher cipher = CryptoUtil.getCipherForDecryption(transformation, key);
+            while ( (bufLength = encryptedBytesInStream.read(buf)) != -1)
             {
-                byte[] tmp = CryptoUtil.decrypt( copyBytes(buf,bufLenth),(PrivateKey)key, transformation);
+                byte[] tmp = cipher.doFinal(copyBytes(buf, bufLength));
                 decryptedBytesOutStream.write(tmp);
-                decryptedBytesOutStream.flush();
-                decryptBytes = decryptedBytesOutStream.toByteArray();
             }
+            decryptedBytesOutStream.flush();
+            decryptBytes = decryptedBytesOutStream.toByteArray();
         }
         catch (final IOException e)
         {
             throw new SecurityServiceException(e.getMessage(), e);
         }
+        catch (IllegalBlockSizeException e)
+        {
+            throw new SecurityServiceException(e.getMessage(), e);
+        }
+        catch (BadPaddingException e)
+        {
+            throw new SecurityServiceException(e.getMessage(), e);
+        }
         finally
         {
             try { decryptedBytesOutStream.close(); } catch (IOException ignore) { log.error(ignore.getMessage(),ignore); }
@@ -301,9 +320,8 @@
                 if (encrypted != null) {
                     message.getContext().setContext(SecurityService.AUTH_REQUEST, encrypted);
                     return true;
-                } else {
-                    logger.warn("No public keystore has been configured which means that the authentication request cannot be encrypted. Please configure jbossesb-properties.xml with a publickey store.");
                 }
+                logger.warn("No public keystore has been configured which means that the authentication request cannot be encrypted. Please configure jbossesb-properties.xml with a publickey store.");
             } catch (final SecurityServiceException e) {
                 throw new MessageDeliverException(e.getMessage(), e);
             }

Modified: labs/jbossesb/workspace/performance/perf2/product/rosetta/src/org/jboss/soa/esb/services/security/util/CryptoUtil.java
===================================================================
--- labs/jbossesb/workspace/performance/perf2/product/rosetta/src/org/jboss/soa/esb/services/security/util/CryptoUtil.java	2009-11-30 19:27:43 UTC (rev 30399)
+++ labs/jbossesb/workspace/performance/perf2/product/rosetta/src/org/jboss/soa/esb/services/security/util/CryptoUtil.java	2009-12-01 04:15:13 UTC (rev 30400)
@@ -23,8 +23,6 @@
 import java.security.InvalidKeyException;
 import java.security.Key;
 import java.security.NoSuchAlgorithmException;
-import java.security.PrivateKey;
-import java.security.PublicKey;
 
 import javax.crypto.BadPaddingException;
 import javax.crypto.Cipher;
@@ -41,23 +39,60 @@
 public final class CryptoUtil
 {
     private CryptoUtil() {}
-
-    public static byte[] decrypt(final byte[] bytes, final Key key, final String transformation) throws SecurityServiceException
+    
+    public static Cipher getCipher(final String transformation) throws SecurityServiceException
     {
         try
         {
-            final Cipher cipher = Cipher.getInstance(transformation);
+            return Cipher.getInstance(transformation);
+        }
+        catch (NoSuchAlgorithmException e)
+        {
+            throw new SecurityServiceException(e.getMessage(), e);
+        }
+        catch (NoSuchPaddingException e)
+        {
+            throw new SecurityServiceException(e.getMessage(), e);
+        }
+    }
+    
+    public static Cipher getCipherForDecryption(final String transformation, final Key key) throws SecurityServiceException
+    {
+        final Cipher cipher = getCipher(transformation);
+        try
+        {
             cipher.init(Cipher.DECRYPT_MODE, key);
-            return cipher.doFinal(bytes);
         }
-        catch (final NoSuchAlgorithmException e)
+        catch (InvalidKeyException e)
         {
             throw new SecurityServiceException(e.getMessage(), e);
         }
-        catch (final NoSuchPaddingException e)
+        return cipher;
+    }
+    
+    public static Cipher getCipherForEncryption(final String transformation, final Key key) throws SecurityServiceException
+    {
+        final Cipher cipher = getCipher(transformation);
+        try
         {
+            cipher.init(Cipher.ENCRYPT_MODE, key);
+        }
+        catch (InvalidKeyException e)
+        {
             throw new SecurityServiceException(e.getMessage(), e);
         }
+        return cipher;
+    }
+    
+
+    public static byte[] decrypt(final byte[] bytes, final Key key, final String transformation) throws SecurityServiceException
+    {
+        try
+        {
+            Cipher cipher = getCipher(transformation);
+            cipher.init(Cipher.DECRYPT_MODE, key);
+            return cipher.doFinal(bytes);
+        }
         catch (final InvalidKeyException e)
         {
             throw new SecurityServiceException(e.getMessage(), e);
@@ -76,18 +111,10 @@
     {
         try
         {
-            final Cipher cipher = Cipher.getInstance(transformation);
+            Cipher cipher = getCipher(transformation);
             cipher.init(Cipher.ENCRYPT_MODE, key);
             return cipher.doFinal(text);
         }
-        catch (final NoSuchAlgorithmException e)
-        {
-            throw new SecurityServiceException(e.getMessage(), e);
-        }
-        catch (final NoSuchPaddingException e)
-        {
-            throw new SecurityServiceException(e.getMessage(), e);
-        }
         catch (InvalidKeyException e)
         {
             throw new SecurityServiceException(e.getMessage(), e);

Modified: labs/jbossesb/workspace/performance/perf2/product/rosetta/tests/src/org/jboss/soa/esb/services/security/PublicCryptoUtilUnitTest.java
===================================================================
--- labs/jbossesb/workspace/performance/perf2/product/rosetta/tests/src/org/jboss/soa/esb/services/security/PublicCryptoUtilUnitTest.java	2009-11-30 19:27:43 UTC (rev 30399)
+++ labs/jbossesb/workspace/performance/perf2/product/rosetta/tests/src/org/jboss/soa/esb/services/security/PublicCryptoUtilUnitTest.java	2009-12-01 04:15:13 UTC (rev 30400)
@@ -23,17 +23,14 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 
-import java.io.IOException;
 import java.io.Serializable;
 import java.net.URL;
-import java.security.InvalidKeyException;
-import java.security.NoSuchAlgorithmException;
 import java.security.Provider;
 import java.security.Security;
+import java.util.concurrent.TimeUnit;
 
 import junit.framework.JUnit4TestAdapter;
 
-import org.jboss.soa.esb.ConfigurationException;
 import org.jboss.soa.esb.common.Environment;
 import org.jboss.soa.esb.util.ClassUtil;
 import org.junit.After;
@@ -51,11 +48,13 @@
 	private String jbossEsbProperties;
 
     @Test
-    public void encryptAndDecrypt() throws SecurityServiceException, InvalidKeyException, NoSuchAlgorithmException, IOException, ClassNotFoundException
+    public void encryptAndDecrypt() throws Exception
     {
         final String object = getLongString(5000);
         byte[] encrypted = PublicCryptoUtil.INSTANCE.encrypt(object);
         assertFalse(object.equals(new String(encrypted)));
+        
+        encrypted = PublicCryptoUtil.INSTANCE.encrypt(object);
 
         Serializable unsealedObject = PublicCryptoUtil.INSTANCE.decrypt(encrypted);
         assertEquals(object, unsealedObject);
@@ -82,15 +81,13 @@
     }
 
     @Before
-    public void setup() throws ConfigurationException
+    public void setup() throws Exception
     {
         jbossEsbProperties = System.getProperty(Environment.PROPERTIES_FILE);
         URL resource = ClassUtil.getResource("security-properties.xml", getClass());
         System.setProperty(Environment.PROPERTIES_FILE, "abs://" + resource.getFile());
     }
 
-
-
     @After
     public void tearDown()
     {



More information about the jboss-svn-commits mailing list