* Android NDK JNI directory entry notes *
beginning
Previous study, we have mastered the necessary knowledge NDK development.
The next step is more practice, through the creation of the problem and solve the problem, to increase proficiency to enhance the experience.
Daily development, often used MD5
SHA1
SHA256
such as HASH algorithm, we can implement these functions in Native.
Hash algorithm
HASH mainly used in the field of information security encryption algorithms, some of the information he converted to different lengths of 128-bit code in messy called HASH value. It can be said, hash is to find a mapping relationship between data content and data storage address.
Android development, review the certificate thumbprint:
Hands
As before create a Native C ++ project template
Project Preparation
Selection of open source C ++ library HASH algorithm
Android project to introduce C ++ source code library
CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
# 该命令会查找指定目录下的所有源文件,然后将结果存进指定变量名
aux_source_directory(hash-library HASH_LIBRARY_SRC)
# 添加头文件搜索目录
include_directories(hash-library)
add_library(
native-lib
SHARED
# 将 hash-library 源文件添加进来
native-lib.cpp ${HASH_LIBRARY_SRC})
find_library(
log-lib
log )
target_link_libraries(
native-lib
${log-lib} )
Realize the function
MD5
Java version:
public static String md5(String plainText) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] md5Byte = md5.digest(plainText.getBytes());
return byteArrayToString(md5Byte);
} catch (Exception e) {
return "";
}
}
Native version:
extern "C"
JNIEXPORT jstring JNICALL
Java_com_ihubin_ndkjni_NativeUtil_md5(JNIEnv *env, jclass clazz, jstring plaintext) {
const char * plaintextChar = env->GetStringUTFChars(plaintext, 0);
std::string plaintextStr = std::string(plaintextChar);
MD5 md5;
std::string md5String = md5(plaintextStr);
char * tabStr = new char [md5String.length()+1];
strcpy(tabStr, md5String.c_str());
char md5Result[128] = {0};
formatSignature(tabStr, md5Result);
return env->NewStringUTF(md5Result);
}
SHA1
Java version:
public static String sha1(String plainText) {
try {
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
byte[] sha1Byte = sha1.digest(plainText.getBytes());
return byteArrayToString(sha1Byte);
} catch (Exception e) {
return "";
}
}
Native version:
extern "C"
JNIEXPORT jstring JNICALL
Java_com_ihubin_ndkjni_NativeUtil_sha1(JNIEnv *env, jclass clazz, jstring plaintext) {
const char * plaintextChar = env->GetStringUTFChars(plaintext, 0);
std::string plaintextStr = std::string(plaintextChar);
SHA1 sha1;
std::string sha1String = sha1(plaintextStr);
char * tabStr = new char [sha1String.length()+1];
strcpy(tabStr, sha1String.c_str());
char sha1Result[128] = {0};
formatSignature(tabStr, sha1Result);
return env->NewStringUTF(sha1Result);
}
SHA256
Java version:
public static String sha256(String plainText) {
try {
MessageDigest sha256 = MessageDigest.getInstance("SHA256");
byte[] sha256Byte = sha256.digest(plainText.getBytes());
return byteArrayToString(sha256Byte);
} catch (Exception e) {
return "";
}
}
Native version:
extern "C"
JNIEXPORT jstring JNICALL
Java_com_ihubin_ndkjni_NativeUtil_sha256(JNIEnv *env, jclass clazz, jstring plaintext) {
const char * plaintextChar = env->GetStringUTFChars(plaintext, 0);
std::string plaintextStr = std::string(plaintextChar);
SHA256 sha256;
std::string sha256String = sha256(plaintextStr);
char * tabStr = new char [sha256String.length()+1];
strcpy(tabStr, sha256String.c_str());
char sha256Result[128] = {0};
formatSignature(tabStr, sha256Result);
return env->NewStringUTF(sha256Result);
}
final effect
So far, we have learned referenced in the Android open source project in C ++ libraries, we also realize realize HASH algorithm Native.
Code:
References:
Oracle - JNI Types and Data Structures