相应的java方法:
void getsign(Context context) throws Exception {
PackageInfo localPackageInfo = context.getPackageManager()
.getPackageInfo(context.getPackageName(), 64);
StringBuilder localStringBuilder = new StringBuilder();
Signature[] arrayOfSignature = localPackageInfo.signatures;
Signature localSignature = arrayOfSignature[0];
MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
localMessageDigest.update(localSignature.toByteArray());
String str2 = toHex(localMessageDigest.digest());
}
private String toHex(byte[] paramArrayOfByte) {
StringBuffer localStringBuffer = new StringBuffer();
for (int i = 0; i < paramArrayOfByte.length; i++) {
Object[] arrayOfObject = new Object[1];
arrayOfObject[0] = Byte.valueOf(paramArrayOfByte[i]);
localStringBuffer.append(String.format("%02x", arrayOfObject));
}
return localStringBuffer.toString();
}
jni中能够调用java的方法,參照上面的java方法能够相应写出jni的c代码:
void ByteToHexStr(const unsigned char* source, char* dest, int sourceLen)
{
short i;
unsigned char highByte, lowByte;
for (i = 0; i < sourceLen; i++)
{
highByte = source[i] >> 4;
lowByte = source[i] & 0x0f;
highByte += 0x30;
if (highByte > 0x39)
dest[i * 2] = highByte + 0x07;
else
dest[i * 2] = highByte;
lowByte += 0x30;
if (lowByte > 0x39)
dest[i * 2 + 1] = lowByte + 0x07;
else
dest[i * 2 + 1] = lowByte;
}
return;
}
JNIEXPORT jstring JNICALL Java_com_hengbao_util_DecodeUtil_test(JNIEnv * env, jobject thiz, jobject context) {
jclass tem_class;
jmethodID tem_method;
jclass class_context = (*env)->GetObjectClass(env, context);
tem_method = (*env)->GetMethodID(env, class_context, "getPackageManager", "()Landroid/content/pm/PackageManager;");
jobject obj_package_manager = (*env)->CallObjectMethod(env, context, tem_method);
tem_method = (*env)->GetMethodID(env, class_context, "getPackageName", "()Ljava/lang/String;");
jobject obj_package_name = (*env)->CallObjectMethod(env, context, tem_method);
tem_class = (*env)->GetObjectClass(env, obj_package_manager);
tem_method = (*env)->GetMethodID(env, tem_class, "getPackageInfo", "(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;");
jobject obj_package_info = (*env)->CallObjectMethod(env, obj_package_manager, tem_method, obj_package_name, 64);
tem_class = (*env)->GetObjectClass(env, obj_package_info);
jfieldID fieldID_signatures = (*env)->GetFieldID(env, tem_class, "signatures", "[Landroid/content/pm/Signature;");
jobjectArray signatures = (*env)->GetObjectField(env, obj_package_info, fieldID_signatures);
jobject signature = (*env)->GetObjectArrayElement(env, signatures, 0);
tem_class = (*env)->GetObjectClass(env, signature);
tem_method = (*env)->GetMethodID(env, tem_class, "toByteArray", "()[B");
jobject obj_sign_byte_array = (*env)->CallObjectMethod(env, signature, tem_method);
jclass class_MessageDigest = (*env)->FindClass(env, "java/security/MessageDigest");
tem_method = (*env)->GetStaticMethodID(env, class_MessageDigest, "getInstance", "(Ljava/lang/String;)Ljava/security/MessageDigest;");
jobject obj_md5 = (*env)->CallStaticObjectMethod(env, class_MessageDigest, tem_method, (*env)->NewStringUTF(env, "md5"));
tem_method = (*env)->GetMethodID(env, class_MessageDigest, "update", "([B)V");
(*env)->CallVoidMethod(env, obj_md5, tem_method, obj_sign_byte_array);
tem_method = (*env)->GetMethodID(env, class_MessageDigest, "digest", "()[B");
jobject obj_array_sign = (*env)->CallObjectMethod(env, obj_md5, tem_method);
jsize int_array_length = (*env)->GetArrayLength(env, obj_array_sign);
jbyte* byte_array_elements = (*env)->GetByteArrayElements(env, obj_array_sign, JNI_FALSE);
char* char_result = (char*) malloc(int_array_length*2+1);
ByteToHexStr(byte_array_elements, char_result, int_array_length);
*(char_result+int_array_length*2) = ' ';
jstring string_result = (*env)->NewStringUTF(env, char_result);
(*env)->ReleaseByteArrayElements(env, obj_array_sign, byte_array_elements, JNI_ABORT);
free(char_result);
return string_result;
}