return env->NewStringUTF((const char*)digest);

expects a null-terminated string.  Any non-text data could have
embedded nulls and isn't guaranteed to be terminated by a null, so the
function could walk off the end of the string or stop short.

More to the point, though, you can't convert arbitrary binary data to
UTF16.  The conversion will attempt to interpret the binary data as
UTF8 and will inevitably error on some bit combinations.  And should
the string "sneak" through, what you'd have coming out would be a
meaningless jumble.  You need to be returning a byte array or some
such.

On May 13, 5:15 am, Vinay Julme <vinayju...@gmail.com> wrote:
> In my code i pass a string and then hash it using HMAC SHA1 algo and then
> try to return it to Java. But it is not returning. It crashes or stop as VM
> aborts.
>
> The code that aborts is as follows:
>
> return env->NewStringUTF((const char*)digest);
> On this statement VM Aborts.
>
> where digest is and unsigned char which has a hashed characters. I logged
> digest variable and it showed some ascii values. Probably after hashing it
> mush have got few characters like that.
>
> My code is as follows:
> <code>
>     typedef unsigned char BYTE ;
>     unsigned char *test = (unsigned char *)"Hi There" ;
>     BYTE Key[20] ;
>     BYTE digest[20] ;
>     jstring Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
> jobject thiz )
>     {
>        memset(Key, 0x0b, 20) ;
>
>             ////////Next 2 lines are for Hashing the given text
>            CHMAC_SHA1 HMAC_SHA1 ;
>            HMAC_SHA1.HMAC_SHA1(test, strlen((const char *)test), Key,
> sizeof(Key), digest) ;
>             ////////
>        __android_log_write(ANDROID_LOG_ERROR,"Hello-Sample",(const char
> *)digest);
>        jstring temp = env->NewStringUTF((const char*)digest);
>         return temp;
>     }
> </code>
> While executing    env->NewStringUTF((const char*)digest);  VM aborts
>
> Can someone please tell me how can i rectify it.
>
> Regards
>
> Vinay Julme
> **************

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to