示例#1
0
std::string get_hmac_sha1(std::string md5, std::string apikey)
{
	const char *test = md5.c_str(); 
	const char  *key = apikey.c_str();
	//char key[41] = "e_kunyan98f5103019170612fd3a486e3d872c48";
	//std::cout << key << std::endl;

	BYTE digest[20];
	CHMAC_SHA1 HMAC_SHA1;
	HMAC_SHA1.HMAC_SHA1((BYTE*)test, strlen(test), (BYTE*)key, strlen(key), digest);

	// Check with digest equal to 0xb617318655057264e28bc0b6fb378c8ef146be00
	// or not
	std::string hamc_sha1 = byteToHexStr((BYTE*)digest, sizeof(digest));
	//cout << hamc_sha1 << endl;
	return hamc_sha1;
}
示例#2
0
/**
 * 初始化, 校验签名
 */
void checkSign(JNIEnv *env) {
    if (!CHECK_SIGN) {
        if (DEBUG)
            ALOGD("skip check sign.");
        return;
    }
    int result = JNI_FALSE;
    jclass clsApplication = env->FindClass("me/yinzhong/jnisignchecker/DemoApplication");
    // "()Lcom/johnnyyin/ndkdemo/DemoApplication;"
    jmethodID midGetInstance = env->GetStaticMethodID(clsApplication, "getInstance",
                                                      "()Lme/yinzhong/jnisignchecker/DemoApplication;");
    jobject application = env->CallStaticObjectMethod(clsApplication, midGetInstance);

    jmethodID midGetPackageManager = env->GetMethodID(clsApplication, "getPackageManager",
                                                      "()Landroid/content/pm/PackageManager;");
    jmethodID midGetPackageName = env->GetMethodID(clsApplication, "getPackageName",
                                                   "()Ljava/lang/String;");
    jmethodID midGetPackageInfo = env->GetMethodID(
            env->FindClass("android/content/pm/PackageManager"), "getPackageInfo",
            "(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;");

    jobject packageManager = env->CallObjectMethod(application, midGetPackageManager);
    jstring packageName = (jstring) env->CallObjectMethod(application, midGetPackageName);

    jobject packageInfo = env->CallObjectMethod(packageManager, midGetPackageInfo, packageName,
                                                0x00000040);

    jfieldID fidSignatures = env->GetFieldID(env->FindClass("android/content/pm/PackageInfo"),
                                             "signatures",
                                             "[Landroid/content/pm/Signature;");
    jobjectArray signatures = (jobjectArray) env->GetObjectField(packageInfo, fidSignatures);
    int length = env->GetArrayLength(signatures);
    for (int i = 0; i < length; i++) {
        jobject signature = env->GetObjectArrayElement(signatures, i);

        // localSignature.toByteArray()
        jmethodID midToByteArray = env->GetMethodID(env->GetObjectClass(signature), "toByteArray",
                                                    "()[B");
        jobject obj_sign_byte_array = env->CallObjectMethod(signature, midToByteArray);

        //      MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
        jclass clsMessageDigest = env->FindClass("java/security/MessageDigest");
        jmethodID midMessageDigestGetInstance = env->GetStaticMethodID(clsMessageDigest,
                                                                       "getInstance",
                                                                       "(Ljava/lang/String;)Ljava/security/MessageDigest;");
        jobject objMd5 = env->CallStaticObjectMethod(clsMessageDigest, midMessageDigestGetInstance,
                                                     env->NewStringUTF("md5"));
        // localMessageDigest.reset();
        jmethodID midReset = env->GetMethodID(clsMessageDigest, "reset", "()V");
        env->CallVoidMethod(objMd5, midReset);

        //      localMessageDigest.update(localSignature.toByteArray());
        //tem_class = (*env)->GetObjectClass(env, obj_md5);
        jmethodID midUpdate = env->GetMethodID(clsMessageDigest, "update", "([B)V");
        env->CallVoidMethod(objMd5, midUpdate, obj_sign_byte_array);
        // localMessageDigest.digest()
        jmethodID midDigest = env->GetMethodID(clsMessageDigest, "digest", "()[B");
        // 这个是md5以后的byte数组,现在只要将它转换成16进制字符串,就可以和之前的比较了
        jbyteArray objArraySign = (jbyteArray) env->CallObjectMethod(objMd5, midDigest);
        //      // 这个就是签名的md5值
        //      String str2 = toHex(localMessageDigest.digest());
        jsize arrayLength = env->GetArrayLength(objArraySign);
        jbyte *byteArrayElements = env->GetByteArrayElements(objArraySign, JNI_FALSE);
        char *charResult = (char *) malloc((size_t) arrayLength * 2 + 1);// 后面+1预留\0结束符
        // 将byte数组转换成16进制字符串, 这里强转为unsigned char *, 因为上面返回的byte数组是没符号的。
        byteToHexStr((unsigned char *) byteArrayElements, charResult, arrayLength);

        int cmpResult = strcmp(charResult, SIGN_HEX);

        if (DEBUG) {
            ALOGD(env->GetStringUTFChars(env->NewStringUTF(charResult), JNI_FALSE));
        }

        // release
        env->ReleaseByteArrayElements(objArraySign, byteArrayElements, JNI_ABORT);
        free(charResult);

        result = (cmpResult == 0 ? JNI_TRUE : JNI_FALSE);
    }

    if (result == JNI_TRUE) {
        if (DEBUG)
            ALOGD("checkSign success.");
    } else {
        if (DEBUG)
            ALOGD("checkSign fail.");
        exit(0);
    }
}