You are viewing a plain text version of this content. The canonical link for it is here.
Posted to juice-dev@xml.apache.org by Raul Benito <ra...@r-bg.com> on 2004/12/23 00:28:30 UTC

Patch: make the digest method to update instead of using a ByteArray

Hi All,
 I've created a little Patch for juice so when digesting it just use 
update instead of using a ByteArray to copy the thing to diges and do a 
whole digesting. In the 1.2 version of xml-sec this behaviour is very 
slow so I've create this patch in order to solve this thing.
 It uses the common little hack to matain the memory in C in sync with 
java(using a Long to store memory address return by malloc), if anyone 
knows a better approach...
 Perhaps, someone can find this of any help.
 
 Regards,

Raul


Index: /home/raul/workspace/juice/java/src/org/apache/security/jce/SHA.java
===================================================================
--- 
/home/raul/workspace/juice/java/src/org/apache/security/jce/SHA.java   
 (revision 122617)
+++ 
/home/raul/workspace/juice/java/src/org/apache/security/jce/SHA.java   
 (working copy)
@@ -25,7 +25,8 @@
 public class SHA extends MessageDigestSpi {
 
     static final int SHA_DIGEST_LENGTH = 20;
-    private ByteArrayOutputStream byteStore = new ByteArrayOutputStream();
+    private long pointerToStruct=0L;
+    //private ByteArrayOutputStream byteStore = new 
ByteArrayOutputStream();
 
     static {
         System.loadLibrary("juice");
@@ -40,21 +41,40 @@
     }
 
     protected void engineReset() {
-        byteStore.reset();
+        if (pointerToStruct!=0) {
+            reset(pointerToStruct);
+        }
     }
 
     protected byte[] engineDigest() {
-        return digest(byteStore.toByteArray());
+        if (pointerToStruct!=0) {
+            return digest(pointerToStruct);
+        }
+        return null;
     }
 
     protected void engineUpdate(byte input) {
-        byteStore.write(input);
+        byte a[]={input};
+        pointerToStruct=update(a,0,1,pointerToStruct);
     }
 
     protected void engineUpdate(byte[] input, int offset, int len) {
-        byteStore.write(input, offset, len);
+        pointerToStruct=update(input,offset,len,pointerToStruct);
     }
+    
+    //Just do the free of the mallocs
+    protected void finalize() throws Throwable {
+        if (pointerToStruct!=0L) {
+            destroy(pointerToStruct);
+        }
+        super.finalize();
+    }
+    
+    static private native void reset(long handle);
+    static private native long update(byte[] clear, int offset,int 
len,long handle);
 
-    private native byte[] digest(byte[] clear);
+    static private native byte[] digest(long handle);
+    
+    static private native void destroy(long handle);
 
 }
Index: /home/raul/workspace/juice/native/src/digest.c
===================================================================
--- /home/raul/workspace/juice/native/src/digest.c    (revision 122617)
+++ /home/raul/workspace/juice/native/src/digest.c    (working copy)
@@ -42,3 +42,32 @@
 #endif
 }
 
+EVP_MD_CTX *_juice_evp_create(const EVP_MD  *type) {
+    EVP_MD_CTX *ctx=EVP_MD_CTX_create();
+    EVP_DigestInit_ex (ctx, type,NULL);
+    return ctx;    
+}
+
+void _juice_evp_destroy(EVP_MD_CTX *ctx) {
+    EVP_MD_CTX_destroy(ctx);
+}
+
+void _juice_evp_reset(EVP_MD_CTX  *ctx,const EVP_MD  *type) {
+    EVP_DigestInit_ex(ctx,type,NULL);
+}
+int _juice_evp_update (void          *data,
+                      unsigned int   count,
+                      EVP_MD_CTX  *ctx)
+{
+  EVP_DigestUpdate (ctx, data, count);
+
+  return 1;
+}
+
+int _juice_evp_digestCtx(unsigned char *md,
+                         unsigned int  *size,
+                         EVP_MD_CTX  *ctx) {
+  EVP_DigestFinal_ex(ctx, md, size);
+  return 1;
+}
+
Index: /home/raul/workspace/juice/native/src/juice.h
===================================================================
--- /home/raul/workspace/juice/native/src/juice.h    (revision 122617)
+++ /home/raul/workspace/juice/native/src/juice.h    (working copy)
@@ -1,87 +1,103 @@
 /* DO NOT EDIT THIS FILE - it is machine generated */
-#include <jni.h>
+#include <jni.h>
 /* Header for class org_apache_security_jce_MD5 */
 
 #ifndef _Included_org_apache_security_jce_MD5
-#define _Included_org_apache_security_jce_MD5
+#define _Included_org_apache_security_jce_MD5
 #ifdef __cplusplus
 extern "C" {
-#endif
+#endif
 #undef org_apache_security_jce_MD5_MD5_DIGEST_LENGTH
-#define org_apache_security_jce_MD5_MD5_DIGEST_LENGTH 16L
-/*
- * Class:     org_apache_security_jce_MD5
- * Method:    digest
- * Signature: ([B)[B
- */
-JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_MD5_digest
+#define org_apache_security_jce_MD5_MD5_DIGEST_LENGTH 16L
+/*
+ * Class:     org_apache_security_jce_MD5
+ * Method:    digest
+ * Signature: ([B)[B
+ */
+JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_MD5_digest
   (JNIEnv *, jobject, jbyteArray);
-
+
 #ifdef __cplusplus
 }
-#endif
-#endif
+#endif
+#endif
 /* Header for class org_apache_security_jce_SHA */
 
 #ifndef _Included_org_apache_security_jce_SHA
-#define _Included_org_apache_security_jce_SHA
+#define _Included_org_apache_security_jce_SHA
 #ifdef __cplusplus
 extern "C" {
-#endif
+#endif
 #undef org_apache_security_jce_SHA_SHA_DIGEST_LENGTH
-#define org_apache_security_jce_SHA_SHA_DIGEST_LENGTH 20L
-/*
- * Class:     org_apache_security_jce_SHA
- * Method:    digest
- * Signature: ([B)[B
- */
-JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_SHA_digest
-  (JNIEnv *, jobject, jbyteArray);
-
+#define org_apache_security_jce_SHA_SHA_DIGEST_LENGTH 20L
+/*
+ * Class:     org_apache_security_jce_SHA
+ * Method:    reset
+ * Signature: (J)V
+ */
+JNIEXPORT void JNICALL Java_org_apache_security_jce_SHA_reset
+  (JNIEnv *, jclass, jlong);
+
+/*
+ * Class:     org_apache_security_jce_SHA
+ * Method:    update
+ * Signature: ([BIIJ)J
+ */
+JNIEXPORT jlong JNICALL Java_org_apache_security_jce_SHA_update
+  (JNIEnv *, jclass, jbyteArray, jint, jint, jlong);
+
+/*
+ * Class:     org_apache_security_jce_SHA
+ * Method:    digest
+ * Signature: (J)[B
+ */
+JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_SHA_digest
+  (JNIEnv *, jclass, jlong);
+
 #ifdef __cplusplus
 }
-#endif
-#endif
+#endif
+#endif
 /* Header for class org_apache_security_jce_RSA */
 
 #ifndef _Included_org_apache_security_jce_RSA
-#define _Included_org_apache_security_jce_RSA
+#define _Included_org_apache_security_jce_RSA
 #ifdef __cplusplus
 extern "C" {
-#endif
-/*
- * Class:     org_apache_security_jce_RSA
- * Method:    publicEncrypt
- * Signature: (Ljava/security/interfaces/RSAPublicKey;[B)[B
- */
-JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_RSA_publicEncrypt
+#endif
+/*
+ * Class:     org_apache_security_jce_RSA
+ * Method:    publicEncrypt
+ * Signature: (Ljava/security/interfaces/RSAPublicKey;[B)[B
+ */
+JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_RSA_publicEncrypt
   (JNIEnv *, jobject, jobject, jbyteArray);
-
-/*
- * Class:     org_apache_security_jce_RSA
- * Method:    publicDecrypt
- * Signature: (Ljava/security/interfaces/RSAPublicKey;[B)[B
- */
-JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_RSA_publicDecrypt
+
+/*
+ * Class:     org_apache_security_jce_RSA
+ * Method:    publicDecrypt
+ * Signature: (Ljava/security/interfaces/RSAPublicKey;[B)[B
+ */
+JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_RSA_publicDecrypt
   (JNIEnv *, jobject, jobject, jbyteArray);
-
-/*
- * Class:     org_apache_security_jce_RSA
- * Method:    privateEncrypt
- * Signature: (Ljava/security/interfaces/RSAPrivateCrtKey;[B)[B
- */
-JNIEXPORT jbyteArray JNICALL 
Java_org_apache_security_jce_RSA_privateEncrypt
+
+/*
+ * Class:     org_apache_security_jce_RSA
+ * Method:    privateEncrypt
+ * Signature: (Ljava/security/interfaces/RSAPrivateCrtKey;[B)[B
+ */
+JNIEXPORT jbyteArray JNICALL 
Java_org_apache_security_jce_RSA_privateEncrypt
   (JNIEnv *, jobject, jobject, jbyteArray);
-
-/*
- * Class:     org_apache_security_jce_RSA
- * Method:    privateDecrypt
- * Signature: (Ljava/security/interfaces/RSAPrivateCrtKey;[B)[B
- */
-JNIEXPORT jbyteArray JNICALL 
Java_org_apache_security_jce_RSA_privateDecrypt
+
+/*
+ * Class:     org_apache_security_jce_RSA
+ * Method:    privateDecrypt
+ * Signature: (Ljava/security/interfaces/RSAPrivateCrtKey;[B)[B
+ */
+JNIEXPORT jbyteArray JNICALL 
Java_org_apache_security_jce_RSA_privateDecrypt
   (JNIEnv *, jobject, jobject, jbyteArray);
-
+
 #ifdef __cplusplus
 }
-#endif
-#endif
+#endif
+#endif
Index: /home/raul/workspace/juice/native/src/digest.h
===================================================================
--- /home/raul/workspace/juice/native/src/digest.h    (revision 122617)
+++ /home/raul/workspace/juice/native/src/digest.h    (working copy)
@@ -31,6 +31,15 @@
                   unsigned int  *size,
                   const EVP_MD  *type);
 
+EVP_MD_CTX *_juice_evp_create(const EVP_MD  *type);
+
+void _juice_evp_reset(EVP_MD_CTX  *ctx,const EVP_MD  *type);
+
+int _juice_evp_update (void          *data, unsigned int   
count,EVP_MD_CTX  *ctx);
+
+int _juice_evp_digestCtx(unsigned char *md,unsigned int  
*size,EVP_MD_CTX  *ctx) ;
+
+void _juice_evp_destroy(EVP_MD_CTX *ctx);
 #ifdef __cplusplus
 }
 #endif
Index: /home/raul/workspace/juice/native/src/sha-1.c
===================================================================
--- /home/raul/workspace/juice/native/src/sha-1.c    (revision 122617)
+++ /home/raul/workspace/juice/native/src/sha-1.c    (working copy)
@@ -29,7 +29,7 @@
 #include "digest.h"
 
 JNIEXPORT jbyteArray JNICALL
-Java_org_apache_security_jce_SHA_digest(JNIEnv *env, jobject obj, 
jbyteArray input) {
+Java_org_apache_security_jce_SHA_digest1(JNIEnv *env, jobject obj, 
jbyteArray input) {
 
     jbyte *inputBytes = (*env)->GetByteArrayElements(env, input, NULL);
     jsize arrayLength = (*env)->GetArrayLength(env, input);
@@ -44,3 +44,36 @@
     (*env)->SetByteArrayRegion(env, jb, 0, SHA_DIGEST_LENGTH, (jbyte *)md);
     return jb;
 }
+
+JNIEXPORT void JNICALL Java_org_apache_security_jce_SHA_reset
+  (JNIEnv *env, jclass cl, jlong handle) {  
+  _juice_evp_reset((EVP_MD_CTX  *)handle,EVP_sha1());
+}
+JNIEXPORT jlong JNICALL Java_org_apache_security_jce_SHA_update
+  (JNIEnv *env, jclass cl, jbyteArray input, jint offset, jint lon, 
jlong handle) {
+  jbyte *inputBytes = (*env)->GetByteArrayElements(env, input, NULL);
+  if (handle==0) {
+      handle=(jlong)_juice_evp_create(EVP_sha1());
+  }
+  printf("poraqui");
+  _juice_evp_update ((void*)(inputBytes+offset), lon,
+                      (EVP_MD_CTX  *)handle);
+  (*env)->ReleaseByteArrayElements(env, input, inputBytes, 
JNI_ABORT);                      
+  return handle;
+}
+
+JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_SHA_digest
+  (JNIEnv *env, jclass cl, jlong handle) {
+    jbyteArray jb;
+    unsigned char md[SHA_DIGEST_LENGTH];
+
+    _juice_evp_digestCtx (md, NULL, (EVP_MD_CTX  *)handle);
+
+    jb = (*env)->NewByteArray(env, SHA_DIGEST_LENGTH);
+    (*env)->SetByteArrayRegion(env, jb, 0, SHA_DIGEST_LENGTH, (jbyte *)md);
+    return jb;  
+}
+JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_SHA_destroy
+  (JNIEnv *env, jclass cl, jlong handle) {
+  _juice_evp_destroy((EVP_MD_CTX*)handle);
+}


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


Re: Patch: make the digest method to update instead of using a ByteArray

Posted by Raul Benito <ra...@r-bg.com>.
Davanum Srinivas wrote:

>Raul,
>
>Since you are interested in juice and we need to jumpstart juice dev
>again...i've taken the liberty of granting you Karma for the juice
>project. Please go ahead and check in the changes.
>
>thanks,
>dims
>
>  
>
Thanks Davanum, but I don't know if I'm going to have time to work on 
it: my daily job got my doing other real uninteresting stuff, and drains 
much energy from me. And I have other task in my TODO list, like finish 
the SAX signature stuff,  and integrate the xml-sec with xml-beans, but 
I will try to give the juice some care from time to time.

Merry Christmas to every one.

Raul

>On Thu, 23 Dec 2004 00:28:30 +0100, Raul Benito <ra...@r-bg.com> wrote:
>  
>
>>Hi All,
>> I've created a little Patch for juice so when digesting it just use
>>update instead of using a ByteArray to copy the thing to diges and do a
>>whole digesting. In the 1.2 version of xml-sec this behaviour is very
>>slow so I've create this patch in order to solve this thing.
>> It uses the common little hack to matain the memory in C in sync with
>>java(using a Long to store memory address return by malloc), if anyone
>>knows a better approach...
>> Perhaps, someone can find this of any help.
>>
>> Regards,
>>
>>Raul
>>
>>Index: /home/raul/workspace/juice/java/src/org/apache/security/jce/SHA.java
>>===================================================================
>>---
>>/home/raul/workspace/juice/java/src/org/apache/security/jce/SHA.java
>> (revision 122617)
>>+++
>>/home/raul/workspace/juice/java/src/org/apache/security/jce/SHA.java
>> (working copy)
>>@@ -25,7 +25,8 @@
>> public class SHA extends MessageDigestSpi {
>>
>>     static final int SHA_DIGEST_LENGTH = 20;
>>-    private ByteArrayOutputStream byteStore = new ByteArrayOutputStream();
>>+    private long pointerToStruct=0L;
>>+    //private ByteArrayOutputStream byteStore = new
>>ByteArrayOutputStream();
>>
>>     static {
>>         System.loadLibrary("juice");
>>@@ -40,21 +41,40 @@
>>     }
>>
>>     protected void engineReset() {
>>-        byteStore.reset();
>>+        if (pointerToStruct!=0) {
>>+            reset(pointerToStruct);
>>+        }
>>     }
>>
>>     protected byte[] engineDigest() {
>>-        return digest(byteStore.toByteArray());
>>+        if (pointerToStruct!=0) {
>>+            return digest(pointerToStruct);
>>+        }
>>+        return null;
>>     }
>>
>>     protected void engineUpdate(byte input) {
>>-        byteStore.write(input);
>>+        byte a[]={input};
>>+        pointerToStruct=update(a,0,1,pointerToStruct);
>>     }
>>
>>     protected void engineUpdate(byte[] input, int offset, int len) {
>>-        byteStore.write(input, offset, len);
>>+        pointerToStruct=update(input,offset,len,pointerToStruct);
>>     }
>>+
>>+    //Just do the free of the mallocs
>>+    protected void finalize() throws Throwable {
>>+        if (pointerToStruct!=0L) {
>>+            destroy(pointerToStruct);
>>+        }
>>+        super.finalize();
>>+    }
>>+
>>+    static private native void reset(long handle);
>>+    static private native long update(byte[] clear, int offset,int
>>len,long handle);
>>
>>-    private native byte[] digest(byte[] clear);
>>+    static private native byte[] digest(long handle);
>>+
>>+    static private native void destroy(long handle);
>>
>> }
>>Index: /home/raul/workspace/juice/native/src/digest.c
>>===================================================================
>>--- /home/raul/workspace/juice/native/src/digest.c    (revision 122617)
>>+++ /home/raul/workspace/juice/native/src/digest.c    (working copy)
>>@@ -42,3 +42,32 @@
>> #endif
>> }
>>
>>+EVP_MD_CTX *_juice_evp_create(const EVP_MD  *type) {
>>+    EVP_MD_CTX *ctx=EVP_MD_CTX_create();
>>+    EVP_DigestInit_ex (ctx, type,NULL);
>>+    return ctx;
>>+}
>>+
>>+void _juice_evp_destroy(EVP_MD_CTX *ctx) {
>>+    EVP_MD_CTX_destroy(ctx);
>>+}
>>+
>>+void _juice_evp_reset(EVP_MD_CTX  *ctx,const EVP_MD  *type) {
>>+    EVP_DigestInit_ex(ctx,type,NULL);
>>+}
>>+int _juice_evp_update (void          *data,
>>+                      unsigned int   count,
>>+                      EVP_MD_CTX  *ctx)
>>+{
>>+  EVP_DigestUpdate (ctx, data, count);
>>+
>>+  return 1;
>>+}
>>+
>>+int _juice_evp_digestCtx(unsigned char *md,
>>+                         unsigned int  *size,
>>+                         EVP_MD_CTX  *ctx) {
>>+  EVP_DigestFinal_ex(ctx, md, size);
>>+  return 1;
>>+}
>>+
>>Index: /home/raul/workspace/juice/native/src/juice.h
>>===================================================================
>>--- /home/raul/workspace/juice/native/src/juice.h    (revision 122617)
>>+++ /home/raul/workspace/juice/native/src/juice.h    (working copy)
>>@@ -1,87 +1,103 @@
>> /* DO NOT EDIT THIS FILE - it is machine generated */
>>-#include <jni.h>
>>+#include <jni.h>
>> /* Header for class org_apache_security_jce_MD5 */
>>
>> #ifndef _Included_org_apache_security_jce_MD5
>>-#define _Included_org_apache_security_jce_MD5
>>+#define _Included_org_apache_security_jce_MD5
>> #ifdef __cplusplus
>> extern "C" {
>>-#endif
>>+#endif
>> #undef org_apache_security_jce_MD5_MD5_DIGEST_LENGTH
>>-#define org_apache_security_jce_MD5_MD5_DIGEST_LENGTH 16L
>>-/*
>>- * Class:     org_apache_security_jce_MD5
>>- * Method:    digest
>>- * Signature: ([B)[B
>>- */
>>-JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_MD5_digest
>>+#define org_apache_security_jce_MD5_MD5_DIGEST_LENGTH 16L
>>+/*
>>+ * Class:     org_apache_security_jce_MD5
>>+ * Method:    digest
>>+ * Signature: ([B)[B
>>+ */
>>+JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_MD5_digest
>>   (JNIEnv *, jobject, jbyteArray);
>>-
>>+
>> #ifdef __cplusplus
>> }
>>-#endif
>>-#endif
>>+#endif
>>+#endif
>> /* Header for class org_apache_security_jce_SHA */
>>
>> #ifndef _Included_org_apache_security_jce_SHA
>>-#define _Included_org_apache_security_jce_SHA
>>+#define _Included_org_apache_security_jce_SHA
>> #ifdef __cplusplus
>> extern "C" {
>>-#endif
>>+#endif
>> #undef org_apache_security_jce_SHA_SHA_DIGEST_LENGTH
>>-#define org_apache_security_jce_SHA_SHA_DIGEST_LENGTH 20L
>>-/*
>>- * Class:     org_apache_security_jce_SHA
>>- * Method:    digest
>>- * Signature: ([B)[B
>>- */
>>-JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_SHA_digest
>>-  (JNIEnv *, jobject, jbyteArray);
>>-
>>+#define org_apache_security_jce_SHA_SHA_DIGEST_LENGTH 20L
>>+/*
>>+ * Class:     org_apache_security_jce_SHA
>>+ * Method:    reset
>>+ * Signature: (J)V
>>+ */
>>+JNIEXPORT void JNICALL Java_org_apache_security_jce_SHA_reset
>>+  (JNIEnv *, jclass, jlong);
>>+
>>+/*
>>+ * Class:     org_apache_security_jce_SHA
>>+ * Method:    update
>>+ * Signature: ([BIIJ)J
>>+ */
>>+JNIEXPORT jlong JNICALL Java_org_apache_security_jce_SHA_update
>>+  (JNIEnv *, jclass, jbyteArray, jint, jint, jlong);
>>+
>>+/*
>>+ * Class:     org_apache_security_jce_SHA
>>+ * Method:    digest
>>+ * Signature: (J)[B
>>+ */
>>+JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_SHA_digest
>>+  (JNIEnv *, jclass, jlong);
>>+
>> #ifdef __cplusplus
>> }
>>-#endif
>>-#endif
>>+#endif
>>+#endif
>> /* Header for class org_apache_security_jce_RSA */
>>
>> #ifndef _Included_org_apache_security_jce_RSA
>>-#define _Included_org_apache_security_jce_RSA
>>+#define _Included_org_apache_security_jce_RSA
>> #ifdef __cplusplus
>> extern "C" {
>>-#endif
>>-/*
>>- * Class:     org_apache_security_jce_RSA
>>- * Method:    publicEncrypt
>>- * Signature: (Ljava/security/interfaces/RSAPublicKey;[B)[B
>>- */
>>-JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_RSA_publicEncrypt
>>+#endif
>>+/*
>>+ * Class:     org_apache_security_jce_RSA
>>+ * Method:    publicEncrypt
>>+ * Signature: (Ljava/security/interfaces/RSAPublicKey;[B)[B
>>+ */
>>+JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_RSA_publicEncrypt
>>   (JNIEnv *, jobject, jobject, jbyteArray);
>>-
>>-/*
>>- * Class:     org_apache_security_jce_RSA
>>- * Method:    publicDecrypt
>>- * Signature: (Ljava/security/interfaces/RSAPublicKey;[B)[B
>>- */
>>-JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_RSA_publicDecrypt
>>+
>>+/*
>>+ * Class:     org_apache_security_jce_RSA
>>+ * Method:    publicDecrypt
>>+ * Signature: (Ljava/security/interfaces/RSAPublicKey;[B)[B
>>+ */
>>+JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_RSA_publicDecrypt
>>   (JNIEnv *, jobject, jobject, jbyteArray);
>>-
>>-/*
>>- * Class:     org_apache_security_jce_RSA
>>- * Method:    privateEncrypt
>>- * Signature: (Ljava/security/interfaces/RSAPrivateCrtKey;[B)[B
>>- */
>>-JNIEXPORT jbyteArray JNICALL
>>Java_org_apache_security_jce_RSA_privateEncrypt
>>+
>>+/*
>>+ * Class:     org_apache_security_jce_RSA
>>+ * Method:    privateEncrypt
>>+ * Signature: (Ljava/security/interfaces/RSAPrivateCrtKey;[B)[B
>>+ */
>>+JNIEXPORT jbyteArray JNICALL
>>Java_org_apache_security_jce_RSA_privateEncrypt
>>   (JNIEnv *, jobject, jobject, jbyteArray);
>>-
>>-/*
>>- * Class:     org_apache_security_jce_RSA
>>- * Method:    privateDecrypt
>>- * Signature: (Ljava/security/interfaces/RSAPrivateCrtKey;[B)[B
>>- */
>>-JNIEXPORT jbyteArray JNICALL
>>Java_org_apache_security_jce_RSA_privateDecrypt
>>+
>>+/*
>>+ * Class:     org_apache_security_jce_RSA
>>+ * Method:    privateDecrypt
>>+ * Signature: (Ljava/security/interfaces/RSAPrivateCrtKey;[B)[B
>>+ */
>>+JNIEXPORT jbyteArray JNICALL
>>Java_org_apache_security_jce_RSA_privateDecrypt
>>   (JNIEnv *, jobject, jobject, jbyteArray);
>>-
>>+
>> #ifdef __cplusplus
>> }
>>-#endif
>>-#endif
>>+#endif
>>+#endif
>>Index: /home/raul/workspace/juice/native/src/digest.h
>>===================================================================
>>--- /home/raul/workspace/juice/native/src/digest.h    (revision 122617)
>>+++ /home/raul/workspace/juice/native/src/digest.h    (working copy)
>>@@ -31,6 +31,15 @@
>>                   unsigned int  *size,
>>                   const EVP_MD  *type);
>>
>>+EVP_MD_CTX *_juice_evp_create(const EVP_MD  *type);
>>+
>>+void _juice_evp_reset(EVP_MD_CTX  *ctx,const EVP_MD  *type);
>>+
>>+int _juice_evp_update (void          *data, unsigned int
>>count,EVP_MD_CTX  *ctx);
>>+
>>+int _juice_evp_digestCtx(unsigned char *md,unsigned int
>>*size,EVP_MD_CTX  *ctx) ;
>>+
>>+void _juice_evp_destroy(EVP_MD_CTX *ctx);
>> #ifdef __cplusplus
>> }
>> #endif
>>Index: /home/raul/workspace/juice/native/src/sha-1.c
>>===================================================================
>>--- /home/raul/workspace/juice/native/src/sha-1.c    (revision 122617)
>>+++ /home/raul/workspace/juice/native/src/sha-1.c    (working copy)
>>@@ -29,7 +29,7 @@
>> #include "digest.h"
>>
>> JNIEXPORT jbyteArray JNICALL
>>-Java_org_apache_security_jce_SHA_digest(JNIEnv *env, jobject obj,
>>jbyteArray input) {
>>+Java_org_apache_security_jce_SHA_digest1(JNIEnv *env, jobject obj,
>>jbyteArray input) {
>>
>>     jbyte *inputBytes = (*env)->GetByteArrayElements(env, input, NULL);
>>     jsize arrayLength = (*env)->GetArrayLength(env, input);
>>@@ -44,3 +44,36 @@
>>     (*env)->SetByteArrayRegion(env, jb, 0, SHA_DIGEST_LENGTH, (jbyte *)md);
>>     return jb;
>> }
>>+
>>+JNIEXPORT void JNICALL Java_org_apache_security_jce_SHA_reset
>>+  (JNIEnv *env, jclass cl, jlong handle) {
>>+  _juice_evp_reset((EVP_MD_CTX  *)handle,EVP_sha1());
>>+}
>>+JNIEXPORT jlong JNICALL Java_org_apache_security_jce_SHA_update
>>+  (JNIEnv *env, jclass cl, jbyteArray input, jint offset, jint lon,
>>jlong handle) {
>>+  jbyte *inputBytes = (*env)->GetByteArrayElements(env, input, NULL);
>>+  if (handle==0) {
>>+      handle=(jlong)_juice_evp_create(EVP_sha1());
>>+  }
>>+  printf("poraqui");
>>+  _juice_evp_update ((void*)(inputBytes+offset), lon,
>>+                      (EVP_MD_CTX  *)handle);
>>+  (*env)->ReleaseByteArrayElements(env, input, inputBytes,
>>JNI_ABORT);
>>+  return handle;
>>+}
>>+
>>+JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_SHA_digest
>>+  (JNIEnv *env, jclass cl, jlong handle) {
>>+    jbyteArray jb;
>>+    unsigned char md[SHA_DIGEST_LENGTH];
>>+
>>+    _juice_evp_digestCtx (md, NULL, (EVP_MD_CTX  *)handle);
>>+
>>+    jb = (*env)->NewByteArray(env, SHA_DIGEST_LENGTH);
>>+    (*env)->SetByteArrayRegion(env, jb, 0, SHA_DIGEST_LENGTH, (jbyte *)md);
>>+    return jb;
>>+}
>>+JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_SHA_destroy
>>+  (JNIEnv *env, jclass cl, jlong handle) {
>>+  _juice_evp_destroy((EVP_MD_CTX*)handle);
>>+}
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: juice-dev-unsubscribe@xml.apache.org
>>For additional commands, e-mail: juice-dev-help@xml.apache.org
>>
>>
>>    
>>
>
>
>  
>


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


Re: Patch: make the digest method to update instead of using a ByteArray

Posted by Davanum Srinivas <da...@gmail.com>.
Raul,

Since you are interested in juice and we need to jumpstart juice dev
again...i've taken the liberty of granting you Karma for the juice
project. Please go ahead and check in the changes.

thanks,
dims


On Thu, 23 Dec 2004 00:28:30 +0100, Raul Benito <ra...@r-bg.com> wrote:
> Hi All,
>  I've created a little Patch for juice so when digesting it just use
> update instead of using a ByteArray to copy the thing to diges and do a
> whole digesting. In the 1.2 version of xml-sec this behaviour is very
> slow so I've create this patch in order to solve this thing.
>  It uses the common little hack to matain the memory in C in sync with
> java(using a Long to store memory address return by malloc), if anyone
> knows a better approach...
>  Perhaps, someone can find this of any help.
> 
>  Regards,
> 
> Raul
> 
> Index: /home/raul/workspace/juice/java/src/org/apache/security/jce/SHA.java
> ===================================================================
> ---
> /home/raul/workspace/juice/java/src/org/apache/security/jce/SHA.java
>  (revision 122617)
> +++
> /home/raul/workspace/juice/java/src/org/apache/security/jce/SHA.java
>  (working copy)
> @@ -25,7 +25,8 @@
>  public class SHA extends MessageDigestSpi {
> 
>      static final int SHA_DIGEST_LENGTH = 20;
> -    private ByteArrayOutputStream byteStore = new ByteArrayOutputStream();
> +    private long pointerToStruct=0L;
> +    //private ByteArrayOutputStream byteStore = new
> ByteArrayOutputStream();
> 
>      static {
>          System.loadLibrary("juice");
> @@ -40,21 +41,40 @@
>      }
> 
>      protected void engineReset() {
> -        byteStore.reset();
> +        if (pointerToStruct!=0) {
> +            reset(pointerToStruct);
> +        }
>      }
> 
>      protected byte[] engineDigest() {
> -        return digest(byteStore.toByteArray());
> +        if (pointerToStruct!=0) {
> +            return digest(pointerToStruct);
> +        }
> +        return null;
>      }
> 
>      protected void engineUpdate(byte input) {
> -        byteStore.write(input);
> +        byte a[]={input};
> +        pointerToStruct=update(a,0,1,pointerToStruct);
>      }
> 
>      protected void engineUpdate(byte[] input, int offset, int len) {
> -        byteStore.write(input, offset, len);
> +        pointerToStruct=update(input,offset,len,pointerToStruct);
>      }
> +
> +    //Just do the free of the mallocs
> +    protected void finalize() throws Throwable {
> +        if (pointerToStruct!=0L) {
> +            destroy(pointerToStruct);
> +        }
> +        super.finalize();
> +    }
> +
> +    static private native void reset(long handle);
> +    static private native long update(byte[] clear, int offset,int
> len,long handle);
> 
> -    private native byte[] digest(byte[] clear);
> +    static private native byte[] digest(long handle);
> +
> +    static private native void destroy(long handle);
> 
>  }
> Index: /home/raul/workspace/juice/native/src/digest.c
> ===================================================================
> --- /home/raul/workspace/juice/native/src/digest.c    (revision 122617)
> +++ /home/raul/workspace/juice/native/src/digest.c    (working copy)
> @@ -42,3 +42,32 @@
>  #endif
>  }
> 
> +EVP_MD_CTX *_juice_evp_create(const EVP_MD  *type) {
> +    EVP_MD_CTX *ctx=EVP_MD_CTX_create();
> +    EVP_DigestInit_ex (ctx, type,NULL);
> +    return ctx;
> +}
> +
> +void _juice_evp_destroy(EVP_MD_CTX *ctx) {
> +    EVP_MD_CTX_destroy(ctx);
> +}
> +
> +void _juice_evp_reset(EVP_MD_CTX  *ctx,const EVP_MD  *type) {
> +    EVP_DigestInit_ex(ctx,type,NULL);
> +}
> +int _juice_evp_update (void          *data,
> +                      unsigned int   count,
> +                      EVP_MD_CTX  *ctx)
> +{
> +  EVP_DigestUpdate (ctx, data, count);
> +
> +  return 1;
> +}
> +
> +int _juice_evp_digestCtx(unsigned char *md,
> +                         unsigned int  *size,
> +                         EVP_MD_CTX  *ctx) {
> +  EVP_DigestFinal_ex(ctx, md, size);
> +  return 1;
> +}
> +
> Index: /home/raul/workspace/juice/native/src/juice.h
> ===================================================================
> --- /home/raul/workspace/juice/native/src/juice.h    (revision 122617)
> +++ /home/raul/workspace/juice/native/src/juice.h    (working copy)
> @@ -1,87 +1,103 @@
>  /* DO NOT EDIT THIS FILE - it is machine generated */
> -#include <jni.h>
> +#include <jni.h>
>  /* Header for class org_apache_security_jce_MD5 */
> 
>  #ifndef _Included_org_apache_security_jce_MD5
> -#define _Included_org_apache_security_jce_MD5
> +#define _Included_org_apache_security_jce_MD5
>  #ifdef __cplusplus
>  extern "C" {
> -#endif
> +#endif
>  #undef org_apache_security_jce_MD5_MD5_DIGEST_LENGTH
> -#define org_apache_security_jce_MD5_MD5_DIGEST_LENGTH 16L
> -/*
> - * Class:     org_apache_security_jce_MD5
> - * Method:    digest
> - * Signature: ([B)[B
> - */
> -JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_MD5_digest
> +#define org_apache_security_jce_MD5_MD5_DIGEST_LENGTH 16L
> +/*
> + * Class:     org_apache_security_jce_MD5
> + * Method:    digest
> + * Signature: ([B)[B
> + */
> +JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_MD5_digest
>    (JNIEnv *, jobject, jbyteArray);
> -
> +
>  #ifdef __cplusplus
>  }
> -#endif
> -#endif
> +#endif
> +#endif
>  /* Header for class org_apache_security_jce_SHA */
> 
>  #ifndef _Included_org_apache_security_jce_SHA
> -#define _Included_org_apache_security_jce_SHA
> +#define _Included_org_apache_security_jce_SHA
>  #ifdef __cplusplus
>  extern "C" {
> -#endif
> +#endif
>  #undef org_apache_security_jce_SHA_SHA_DIGEST_LENGTH
> -#define org_apache_security_jce_SHA_SHA_DIGEST_LENGTH 20L
> -/*
> - * Class:     org_apache_security_jce_SHA
> - * Method:    digest
> - * Signature: ([B)[B
> - */
> -JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_SHA_digest
> -  (JNIEnv *, jobject, jbyteArray);
> -
> +#define org_apache_security_jce_SHA_SHA_DIGEST_LENGTH 20L
> +/*
> + * Class:     org_apache_security_jce_SHA
> + * Method:    reset
> + * Signature: (J)V
> + */
> +JNIEXPORT void JNICALL Java_org_apache_security_jce_SHA_reset
> +  (JNIEnv *, jclass, jlong);
> +
> +/*
> + * Class:     org_apache_security_jce_SHA
> + * Method:    update
> + * Signature: ([BIIJ)J
> + */
> +JNIEXPORT jlong JNICALL Java_org_apache_security_jce_SHA_update
> +  (JNIEnv *, jclass, jbyteArray, jint, jint, jlong);
> +
> +/*
> + * Class:     org_apache_security_jce_SHA
> + * Method:    digest
> + * Signature: (J)[B
> + */
> +JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_SHA_digest
> +  (JNIEnv *, jclass, jlong);
> +
>  #ifdef __cplusplus
>  }
> -#endif
> -#endif
> +#endif
> +#endif
>  /* Header for class org_apache_security_jce_RSA */
> 
>  #ifndef _Included_org_apache_security_jce_RSA
> -#define _Included_org_apache_security_jce_RSA
> +#define _Included_org_apache_security_jce_RSA
>  #ifdef __cplusplus
>  extern "C" {
> -#endif
> -/*
> - * Class:     org_apache_security_jce_RSA
> - * Method:    publicEncrypt
> - * Signature: (Ljava/security/interfaces/RSAPublicKey;[B)[B
> - */
> -JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_RSA_publicEncrypt
> +#endif
> +/*
> + * Class:     org_apache_security_jce_RSA
> + * Method:    publicEncrypt
> + * Signature: (Ljava/security/interfaces/RSAPublicKey;[B)[B
> + */
> +JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_RSA_publicEncrypt
>    (JNIEnv *, jobject, jobject, jbyteArray);
> -
> -/*
> - * Class:     org_apache_security_jce_RSA
> - * Method:    publicDecrypt
> - * Signature: (Ljava/security/interfaces/RSAPublicKey;[B)[B
> - */
> -JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_RSA_publicDecrypt
> +
> +/*
> + * Class:     org_apache_security_jce_RSA
> + * Method:    publicDecrypt
> + * Signature: (Ljava/security/interfaces/RSAPublicKey;[B)[B
> + */
> +JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_RSA_publicDecrypt
>    (JNIEnv *, jobject, jobject, jbyteArray);
> -
> -/*
> - * Class:     org_apache_security_jce_RSA
> - * Method:    privateEncrypt
> - * Signature: (Ljava/security/interfaces/RSAPrivateCrtKey;[B)[B
> - */
> -JNIEXPORT jbyteArray JNICALL
> Java_org_apache_security_jce_RSA_privateEncrypt
> +
> +/*
> + * Class:     org_apache_security_jce_RSA
> + * Method:    privateEncrypt
> + * Signature: (Ljava/security/interfaces/RSAPrivateCrtKey;[B)[B
> + */
> +JNIEXPORT jbyteArray JNICALL
> Java_org_apache_security_jce_RSA_privateEncrypt
>    (JNIEnv *, jobject, jobject, jbyteArray);
> -
> -/*
> - * Class:     org_apache_security_jce_RSA
> - * Method:    privateDecrypt
> - * Signature: (Ljava/security/interfaces/RSAPrivateCrtKey;[B)[B
> - */
> -JNIEXPORT jbyteArray JNICALL
> Java_org_apache_security_jce_RSA_privateDecrypt
> +
> +/*
> + * Class:     org_apache_security_jce_RSA
> + * Method:    privateDecrypt
> + * Signature: (Ljava/security/interfaces/RSAPrivateCrtKey;[B)[B
> + */
> +JNIEXPORT jbyteArray JNICALL
> Java_org_apache_security_jce_RSA_privateDecrypt
>    (JNIEnv *, jobject, jobject, jbyteArray);
> -
> +
>  #ifdef __cplusplus
>  }
> -#endif
> -#endif
> +#endif
> +#endif
> Index: /home/raul/workspace/juice/native/src/digest.h
> ===================================================================
> --- /home/raul/workspace/juice/native/src/digest.h    (revision 122617)
> +++ /home/raul/workspace/juice/native/src/digest.h    (working copy)
> @@ -31,6 +31,15 @@
>                    unsigned int  *size,
>                    const EVP_MD  *type);
> 
> +EVP_MD_CTX *_juice_evp_create(const EVP_MD  *type);
> +
> +void _juice_evp_reset(EVP_MD_CTX  *ctx,const EVP_MD  *type);
> +
> +int _juice_evp_update (void          *data, unsigned int
> count,EVP_MD_CTX  *ctx);
> +
> +int _juice_evp_digestCtx(unsigned char *md,unsigned int
> *size,EVP_MD_CTX  *ctx) ;
> +
> +void _juice_evp_destroy(EVP_MD_CTX *ctx);
>  #ifdef __cplusplus
>  }
>  #endif
> Index: /home/raul/workspace/juice/native/src/sha-1.c
> ===================================================================
> --- /home/raul/workspace/juice/native/src/sha-1.c    (revision 122617)
> +++ /home/raul/workspace/juice/native/src/sha-1.c    (working copy)
> @@ -29,7 +29,7 @@
>  #include "digest.h"
> 
>  JNIEXPORT jbyteArray JNICALL
> -Java_org_apache_security_jce_SHA_digest(JNIEnv *env, jobject obj,
> jbyteArray input) {
> +Java_org_apache_security_jce_SHA_digest1(JNIEnv *env, jobject obj,
> jbyteArray input) {
> 
>      jbyte *inputBytes = (*env)->GetByteArrayElements(env, input, NULL);
>      jsize arrayLength = (*env)->GetArrayLength(env, input);
> @@ -44,3 +44,36 @@
>      (*env)->SetByteArrayRegion(env, jb, 0, SHA_DIGEST_LENGTH, (jbyte *)md);
>      return jb;
>  }
> +
> +JNIEXPORT void JNICALL Java_org_apache_security_jce_SHA_reset
> +  (JNIEnv *env, jclass cl, jlong handle) {
> +  _juice_evp_reset((EVP_MD_CTX  *)handle,EVP_sha1());
> +}
> +JNIEXPORT jlong JNICALL Java_org_apache_security_jce_SHA_update
> +  (JNIEnv *env, jclass cl, jbyteArray input, jint offset, jint lon,
> jlong handle) {
> +  jbyte *inputBytes = (*env)->GetByteArrayElements(env, input, NULL);
> +  if (handle==0) {
> +      handle=(jlong)_juice_evp_create(EVP_sha1());
> +  }
> +  printf("poraqui");
> +  _juice_evp_update ((void*)(inputBytes+offset), lon,
> +                      (EVP_MD_CTX  *)handle);
> +  (*env)->ReleaseByteArrayElements(env, input, inputBytes,
> JNI_ABORT);
> +  return handle;
> +}
> +
> +JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_SHA_digest
> +  (JNIEnv *env, jclass cl, jlong handle) {
> +    jbyteArray jb;
> +    unsigned char md[SHA_DIGEST_LENGTH];
> +
> +    _juice_evp_digestCtx (md, NULL, (EVP_MD_CTX  *)handle);
> +
> +    jb = (*env)->NewByteArray(env, SHA_DIGEST_LENGTH);
> +    (*env)->SetByteArrayRegion(env, jb, 0, SHA_DIGEST_LENGTH, (jbyte *)md);
> +    return jb;
> +}
> +JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_SHA_destroy
> +  (JNIEnv *env, jclass cl, jlong handle) {
> +  _juice_evp_destroy((EVP_MD_CTX*)handle);
> +}
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: juice-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: juice-dev-help@xml.apache.org
> 
> 


-- 
Davanum Srinivas - http://webservices.apache.org/~dims/

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