You are viewing a plain text version of this content. The canonical link for it is here.
Posted to juice-svn@xml.apache.org by ra...@apache.org on 2006/01/13 18:05:49 UTC

svn commit: r368785 - /incubator/juice/java/src/org/apache/security/jce/RSA.java

Author: raul
Date: Fri Jan 13 09:05:41 2006
New Revision: 368785

URL: http://svn.apache.org/viewcvs?rev=368785&view=rev
Log:
Optimization: Change from instance to static 
 a native object.
Optimization: Cache the keys, so it is not need 
 to decode again.
FIXME: A memory retention added. Fix it with a weakmap?
 

Modified:
    incubator/juice/java/src/org/apache/security/jce/RSA.java

Modified: incubator/juice/java/src/org/apache/security/jce/RSA.java
URL: http://svn.apache.org/viewcvs/incubator/juice/java/src/org/apache/security/jce/RSA.java?rev=368785&r1=368784&r2=368785&view=diff
==============================================================================
--- incubator/juice/java/src/org/apache/security/jce/RSA.java (original)
+++ incubator/juice/java/src/org/apache/security/jce/RSA.java Fri Jan 13 09:05:41 2006
@@ -26,6 +26,8 @@
 import java.security.interfaces.RSAPrivateKey;
 import java.security.interfaces.RSAPublicKey;
 import java.security.spec.AlgorithmParameterSpec;
+import java.util.HashMap;
+import java.util.Map;
 
 import javax.crypto.Cipher;
 import javax.crypto.CipherSpi;
@@ -38,8 +40,9 @@
  *
  */
 public class RSA extends CipherSpi {
-
+	static Map	keys=new HashMap();
 	private RSAKey key;
+	long ptrKey=0l;
 	private int opmode;
 	private ByteArrayOutputStream bOut = new ByteArrayOutputStream();
 
@@ -109,8 +112,22 @@
 		if (key instanceof RSAPrivateKey && !(key instanceof RSAPrivateCrtKey)) {
 			throw new InvalidKeyException("Supplied key is an RSAPrivateKey but not an RSAPrivateCrtKey (RSAPrivateCrtKey is required).");
 		}
-
 		this.key = (RSAKey) key;
+		Object ob=keys.get(key);		
+		if (ob!=null) {
+			ptrKey=((Long)ob).longValue();			
+			return;
+		}
+		if (key instanceof RSAPublicKey) {
+			ptrKey=generatePublicKey((RSAPublicKey) key);
+			//FIXME: Memory retention problem. Fixme.
+			keys.put(key,new Long(ptrKey));
+		} else if (key instanceof RSAPrivateCrtKey) {
+			ptrKey=generatePrivateKey((RSAPrivateCrtKey) key);
+			//FIXME: Memory retention problem. Fixme.
+			keys.put(key,new Long(ptrKey));
+		} 
+		
 	}
 
 	public void engineSetMode(String mode) {
@@ -144,16 +161,16 @@
 		bOut.reset(); // finished with these bytes
 
 		if (opmode == Cipher.ENCRYPT_MODE && key instanceof RSAPublicKey) {
-			return this.publicEncrypt((RSAPublicKey) key, bytes);
+			return publicEncrypt(ptrKey, bytes);
 
 		} else if (opmode == Cipher.ENCRYPT_MODE && key instanceof RSAPrivateCrtKey) {
-			return this.privateEncrypt((RSAPrivateCrtKey) key, bytes);
+			return privateEncrypt(ptrKey, bytes);
 
 		} else if (opmode == Cipher.DECRYPT_MODE && key instanceof RSAPublicKey) {
-			return this.publicDecrypt((RSAPublicKey) key, bytes);
+			return publicDecrypt(ptrKey, bytes);
 
 		} else if (opmode == Cipher.DECRYPT_MODE && key instanceof RSAPrivateCrtKey) {
-			return this.privateDecrypt((RSAPrivateCrtKey) key, bytes);
+			return privateDecrypt(ptrKey, bytes);
 
 		} else {
 			throw new IllegalStateException("Cipher not initialized.");
@@ -174,9 +191,15 @@
 
 		return out.length;
 	}
-
-	private native byte[] publicEncrypt(RSAPublicKey key, byte[] bytes);
-	private native byte[] publicDecrypt(RSAPublicKey key, byte[] bytes);
-	private native byte[] privateEncrypt(RSAPrivateCrtKey key, byte[] bytes);
-	private native byte[] privateDecrypt(RSAPrivateCrtKey key, byte[] bytes);
+	/*protected void finalize() throws Throwable {
+		freeKey(ptrKey);
+		super.finalize();		
+	}*/
+	static private native long generatePublicKey(RSAPublicKey key);
+	static private native long generatePrivateKey(RSAPrivateCrtKey key);
+	static private native void freeKey(long key);
+	static private native byte[] publicEncrypt(long key, byte[] bytes);
+	static private native byte[] publicDecrypt(long key, byte[] bytes);
+	static private native byte[] privateEncrypt(long key, byte[] bytes);
+	static private native byte[] privateDecrypt(long key, byte[] bytes);
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: juice-svn-unsubscribe@xml.apache.org
For additional commands, e-mail: juice-svn-help@xml.apache.org