I used a high performance c++ implementation which I load with JNI For more details write a comment, please.
I used a high performance c++ implementation which I load with JNI. For more details write a comment, please. EDIT: Requirements for JNI is the Android NDK.
For Windows is needed in addition cygwin or something similar. If you decided for cygwin, I give you some little instructions how to get it working with the NDK: Download the setup. Exe from cygwin and execute it.
Click on Next and choice Install from Internet confirm with Next. The next two steps adjust the settings as desired and as always click Next. Select your internet connection and the same procedure as in the final stages.
A download page will catch the eye select it or take just a download page, which is in your country. There is nothing more to say. We need the packages make and gcc-g++.
You can find them using the search in the left upper corner, click on the Skip til a version is displayed and the first field is selected. Do that what we have always done after a selection. You will get the information, that there are dependencies, which must be resolved.It is usually not necessary to do it yourself and confirm it.
The download and installation started. If you need you can create shortcuts otherwise click on exceptional Finish. Download the zip file and extract the NDK to a non space containing path.
You can start now cygwin. Navigate to the NDK. The path /cydrive gives you all available drives f.e.
Cd /cygdrive/d navigates to the drive with the letter D. In the root folder of the NDK you can execute the file ndk-build with . /ndk-build.
There should be an error occurs like Android NDK: Could not find application project directory!. You have to navigate in an Android project to execute the command.So let's start with a project. Before we can start with the project search for a C/C++ implementation of the hash algorithm.
I took the code from this site CSHA1. You should edit the source code for your requirements. Now we can start with JNI.
You create a folder called jni in your Android project.It contains all native source files and the Android. Mk (more about that file later), too. Copy your downloaded (and edited) source files in that folder.My java package is called de.dhbw.file.
Sha1, so I named my source files similar to find them easily. Android.Mk: LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_LDLIBS := -llog # How the lib is called? LOCAL_MODULE := SHA1Calc # Which is your main SOURCE(!) file?
LOCAL_SRC_FILES := de_dhbw_file_sha1_SHA1Calc. Cpp include $(BUILD_SHARED_LIBRARY) Java code: I used the AsyncTask with a ProgressDialog to give the user some feedback about the action. Package de.dhbw.file.
Sha1; // TODO: Add imports public class SHA1HashFileAsyncTask extends AsyncTask { // ... static { // loads a native library System. LoadLibrary("SHA1Calc"); } // ... // native is the indicator for native written methods protected native void calcFileSha1(String filePath); protected native int getProgress(); protected native void unlockMutex(); protected native String getHash(); // ... } Native code (C++): Remember accessing variables inside native code or other way around using threads needs synchronizing or you will get a segmentation fault soon! For JNI usage you have to add #include .
For logging insert following include #include . Now you can log with __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "Version %s", "19");. The first argument is the type of message and the second the causing library.
You can see I had a version number in my code.It is very helpful because sometimes the apk builder doesn't use the new native libraries. Troubleshooting can be extremely shortened, if the wrong version is online. The naming conventions in the native code are a little bit crasier: Java_package name_class name_method name.
The first to arguments are always given, but depending on the application you should distinguish: func(JNIEnv * env, jobject jobj) -> JNI call is an instance method func(JNIEnv * env, jclass jclazz) -> JNI call is a static method The header for the method calcFileSha1(...): JNIEXPORT void JNICALL Java_de_dhbw_file_sha1_SHA1HashFileAsyncTask_calcFileSha1(JNIEnv * env, jobject jobj, jstring file) The JDK delivers the binary javah. Exe, which generates the header file for the native code. The usage is very simple, simply call it with the full qualified class: javah de.dhbw.file.
Sha1. SHA1HashFileAsyncTask In my case I have to give the bootclasspath additionally, because I use Android classes: javah -bootclasspath de.dhbw.file. Sha1.
SHA1HashFileAsyncTask That would be the generated file: /* DO NOT EDIT THIS FILE - it is machine generated */ #include /* Header for class de_dhbw_file_sha1_SHA1HashFileAsyncTask */ #ifndef _Included_de_dhbw_file_sha1_SHA1HashFileAsyncTask #define _Included_de_dhbw_file_sha1_SHA1HashFileAsyncTask #ifdef __cplusplus extern "C" { #endif #undef de_dhbw_file_sha1_SHA1HashFileAsyncTask_ERROR_CODE #define de_dhbw_file_sha1_SHA1HashFileAsyncTask_ERROR_CODE -1L #undef de_dhbw_file_sha1_SHA1HashFileAsyncTask_PROGRESS_CODE #define de_dhbw_file_sha1_SHA1HashFileAsyncTask_PROGRESS_CODE 1L /* * Class: de_dhbw_file_sha1_SHA1HashFileAsyncTask * Method: calcFileSha1 * Signature: (Ljava/lang/String;)V */ JNIEXPORT void JNICALL Java_de_dhbw_file_sha1_SHA1HashFileAsyncTask_calcFileSha1 (JNIEnv *, jobject, jstring); /* * Class: de_dhbw_file_sha1_SHA1HashFileAsyncTask * Method: getProgress * Signature: ()I */ JNIEXPORT jint JNICALL Java_de_dhbw_file_sha1_SHA1HashFileAsyncTask_getProgress (JNIEnv *, jobject); /* * Class: de_dhbw_file_sha1_SHA1HashFileAsyncTask * Method: unlockMutex * Signature: ()V */ JNIEXPORT void JNICALL Java_de_dhbw_file_sha1_SHA1HashFileAsyncTask_unlockMutex (JNIEnv *, jobject); /* * Class: de_dhbw_file_sha1_SHA1HashFileAsyncTask * Method: getHash * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_de_dhbw_file_sha1_SHA1HashFileAsyncTask_getHash (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif You can change the file without further notice. But do not use javah again! Class and methods To get a class instance you can use jclass clz = callEnv->FindClass(CALL_CLASS);.
In this case is CALL_CLASS the full qualified path to the class de/dhbw/file/sha1/SHA1HashFileAsyncTask. To find a method you need the JNIEnv and an instance of the class: jmethodID midSet = callEnv->GetMethodID(callClass, "setFileSize", "(J)V"); The first argument is the instance of the class, the second the name of the method and the third is the signature of the method. The signature you can get with the from JDK given binary javap.exe.
Simply call it with the full qualified path of the class f.e. Javap -s de.dhbw.file. Sha1.
SHA1HashFileAsyncTask. You will get an result like: Compiled from "SHA1HashFileAsyncTask. Java" public class de.dhbw.file.
Sha1. SHA1HashFileAsyncTask extends android.os. AsyncTas k { ... static {}; Signature: ()V public de.dhbw.file.
Sha1. SHA1HashFileAsyncTask(android.content. Context, de.
Dhb w.file. Sha1. SHA1HashFileAsyncTask$SHA1AsyncTaskListener); Signature: (Landroid/content/Context;Lde/dhbw/file/sha1/SHA1HashFileAsyncTas k$SHA1AsyncTaskListener;)V protected native void calcFileSha1(java.lang.
String); Signature: (Ljava/lang/String;)V protected native int getProgress(); Signature: ()I protected native void unlockMutex(); Signature: ()V protected native java.lang. String getHash(); Signature: ()Ljava/lang/String; ... public void setFileSize(long); Signature: (J)V ... } If the method is found the variable is not equal 0. Calling the method is very easy: callEnv->CallVoidMethod(callObj, midSet, size); The first argument is the given jobject from the "main" method and I think the others are clear.
Remember that you can call from native code although private methods of the class, because the native code is part of it! Strings The given string would be converted with following code: jboolean jbol; const char *fileName = env->GetStringUTFChars(file, &jbol); And the other way: TCHAR* szReport = new TCHAR; jstring result = callEnv->NewStringUTF(szReport); It can be every char* variable. Exceptions Can be thrown with the JNIEnv: callEnv->ThrowNew(callEnv->FindClass("java/lang/Exception"), "Hash generation failed"); You can also check if there is an exception occurred also with JNIEnv: if (callEnv->ExceptionOccurred()) { callEnv->ExceptionDescribe(); callEnv->ExceptionClear(); } Specifications Java Native Interface Specifications Build/Clean Build After we have created all files and filled them with content, we can build it.
Open cygwin, navigate to the project root and execute from there the ndk-build, which is in the NDK root. This start the compile, if it is success you will get an output like that: $ /cygdrive/d/android-ndk-r5c/ndk-build Compile++ thumb : SHA1Calc libs/armeabi/libSHA1Calc. So If there is any error, you will get the typical output from the compiler.
Clean Open cygwin, switch in your Android project and execute the command /cygdrive/d/android-ndk-r5c/ndk-build clean. Build apk After you have build the native libraries you can build your project. I've found clean, it is advantageous to use the eclipse feature clean project.
Debugging Debugging of java code isn't different as before. The debugging of c++ code will follow in the next time.
– Matt Wolfe Jun 29 '11 at 19:11 Have a look on my edit. :) – CSchulz Jun 30 '11 at 10:45.
Do this: MessageDigest md = MessageDigest. GetInstance("SHA1"); InputStream in = new FileInputStream("hereyourinputfilename"); byte buf = new byte8192; for (;;) { int len = in. Read(buf); if (len Update(buf, 0, len); } in.close(); byte hash = md.digest(); Performance comes from handling data by blocks.
An 8 kB buffer, as here, ought to be blocky enough. You do not have to use a BufferedInputStream since the 8 kB buffer also serves as I/O buffer.
It is fast on my pc, but not on my Android device. – CSchulz Jun 14 '11 at 22:15 @H3llGhost: the MessageDigest instance you get is most probably written in pure Java, and Java, on Android 2.1 devices, is slow at computing intensive tasks. Things are allegedly much better on 2.2 and beyond (the JVM then has a JIT compiler).
There is not much you can do to change that. – Thomas Pornin Jun 14 '11 at 22:19 Thanks for the advice. I am using already 2.2.Perhaps I should look for sha1 functions based on c++ and invoked by JNI.
– CSchulz Jun 14 '11 at 22:55.
The reason the fast one is fast is (I think) that your code is not hashing the file contents! FileInputStream fis = new FileInputStream("C:/Users/Ich/Downloads/srware_iron. Exe"); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fis.toString().getBytes()); The fis.toString() call does not read the contents of the file.
Rather it gives you a string that (I suspect) looks something like this: "java.io. FileInputStream@xxxxxxxx" which you are then proceeding to calculate the SHA1 hash for. The simple way to read the entire contents of an InputStream to a byte is to use an Apache Commons I/O helper method - IOUtils.
ToByteArray(InputStream).
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.