//
// Created by Saki on 2018/7/22.
//
#include "com_saki_d_n.h"
#include "context.h"

JNIEXPORT jstring JNICALL Java_com_saki_d_n_a(JNIEnv *env, jclass obj, jbyteArray byt){
    jclass clz=env->FindClass("com/saki/c/e");
    //获取签名信息
    jstring sing = getSignature(env);
    //获取MD5加密
    jmethodID methodM=env->GetStaticMethodID(clz,"m","(Ljava/lang/String;)[B");
    //获取签名MD5
    jbyteArray md5=(jbyteArray)(env->CallStaticObjectMethod(clz,methodM,sing));
    //生成key
    jbyteArray key=env->NewByteArray(16);
    jbyte* md5data=env->GetByteArrayElements(md5,0);
    jbyte buf[16];
    buf[0] = md5data[0] + 3;
    buf[1] = md5data[1] + 0;
    buf[2] = md5data[2] + 2;
    buf[3] = md5data[3] + 6;
    buf[4] = md5data[4] + 5;
    buf[5] = md5data[5] + 2;
    buf[6] = md5data[6] + 0;
    buf[7] = md5data[7] + 3;
    buf[8] = md5data[8] + 4;
    buf[9] = md5data[9];
    buf[10] = md5data[10];
    buf[11] = md5data[11];
    buf[12] = md5data[12];
    buf[13] = md5data[13];
    buf[14] = md5data[14];
    buf[15] = md5data[15];
    //释放数组
    env->ReleaseByteArrayElements(md5, md5data, 0);
    env->SetByteArrayRegion(key, 0, 16, buf);
    //加密
    jmethodID mid=env->GetStaticMethodID(clz,"a","([B[B)Ljava/lang/String;");
    jstring encode=(jstring)(env->CallStaticObjectMethod(clz,mid,byt,key));
    return encode;
}

JNIEXPORT jstring JNICALL Java_com_saki_d_n_b(JNIEnv *env, jclass obj, jbyteArray byt){
    jclass clz=env->FindClass("com/saki/c/e");
    //获取签名信息
    jstring sing = getSignature(env);
    //获取MD5加密
    jmethodID methodM=env->GetStaticMethodID(clz,"m","(Ljava/lang/String;)[B");
    //获取签名MD5
    jbyteArray md5=(jbyteArray)(env->CallStaticObjectMethod(clz,methodM,sing));
    //生成key
    jbyteArray key=env->NewByteArray(16);
    jbyte* md5data=env->GetByteArrayElements(md5,0);
    jbyte buf[16];
    buf[0] = md5data[0] + 3;
    buf[1] = md5data[1] + 0;
    buf[2] = md5data[2] + 2;
    buf[3] = md5data[3] + 6;
    buf[4] = md5data[4] + 5;
    buf[5] = md5data[5] + 2;
    buf[6] = md5data[6] + 0;
    buf[7] = md5data[7] + 3;
    buf[8] = md5data[8] + 4;
    buf[9] = md5data[9];
    buf[10] = md5data[10];
    buf[11] = md5data[11];
    buf[12] = md5data[12];
    buf[13] = md5data[13];
    buf[14] = md5data[14];
    buf[15] = md5data[15];
    //释放数组
    env->ReleaseByteArrayElements(md5, md5data, 0);
    env->SetByteArrayRegion(key, 0, 16, buf);
    //加密
    jmethodID mid=env->GetStaticMethodID(clz,"b","([B[B)Ljava/lang/String;");
    jstring encode=(jstring)(env->CallStaticObjectMethod(clz,mid,byt,key));
    return encode;
}
