Android NDK JNI 入门笔记-day04-NDK实现Hash算法
binglingziyu 人气:2
[* Android NDK JNI 入门笔记目录 *](https://www.ihubin.com/blog/android-ndk-jni-basic-catagory/)
# 开头
前面的学习,我们已经掌握了 NDK 开发的必备知识。
下一步就要多实践,通过创造问题并解决问题,来增加熟练度,提升经验。
日常开发中,经常会用到 `MD5` `SHA1` `SHA256` 等 HASH 算法,我们可以在 Native 实现这些功能。
# Hash 算法
> HASH 主要用于信息安全领域中加密算法,他把一些不同长度的信息转化成杂乱的 128 位的编码里,叫做 HASH 值。也可以说,hash 就是找到一种数据内容和数据存放地址之间的映射关系。
Android 开发中,查看证书指纹:
![android-cert-fingerprint](http://cloudstorage.ihubin.com/blog/Android/ndk-jnihttps://img.qb5200.com/download-x/day04/android-cert-fingerprint.jpg)
# 动手实践
像之前一样[创建一个 Native C++ 模板项目](https://www.ihubin.com/blog/android-ndk-jni-basic-day01/)
## 项目准备
![day04-example-preview](http://cloudstorage.ihubin.com/blog/Android/ndk-jnihttps://img.qb5200.com/download-x/day04https://img.qb5200.com/download-x/day04-example-preview.jpeg)
## 选用 C++ 开源 HASH 算法实现库
> [Portable C++ hashing library](https://github.com/stbrumme/hash-library)
![cpp-hash-library](http://cloudstorage.ihubin.com/blog/Android/ndk-jnihttps://img.qb5200.com/download-x/day04/cpp-hash-library-file-tree.png)
## Android 工程引入 C++ 库源码
![android-use-cpp-library-source](http://cloudstorage.ihubin.com/blog/Android/ndk-jnihttps://img.qb5200.com/download-x/day04/android-use-cpp-library-source.png)
## 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} )
```
## 实现功能
### MD5
> Java 版本:
```java
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 版本:
```c++
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 版本:
```java
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 版本:
```c++
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 版本:
```
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 版本:
```
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);
}
```
## 最终效果
![day04-example-result](http://cloudstorage.ihubin.com/blog/Android/ndk-jnihttps://img.qb5200.com/download-x/day04https://img.qb5200.com/download-x/day04-example-result.jpeg)
> 至此,我们已经学会了在 Android 项目中引用开源 C++ 库,同时我们也实现在 Native 实现 HASH 算法。
-----
代码:
[NDKJNIday04](https://github.com/binglingziyu/NDK-JNI/tree/master/NDKJNIday04)
参考资料:
[Oracle - JNI Types and Data Structures](https:/https://img.qb5200.com/download-x/docs.oracle.com/javase/7https://img.qb5200.com/download-x/docs/technotes/guides/jni/spec/types.html)
[C++ hash-library](https://github.com/stbrumme/hash-library)
[CMake入门](https://www.cnblogs.com/binglingziyu/tag/CMake/)
-----
加载全部内容