diff --git a/benchmark/build.gradle b/benchmark/build.gradle index 2c21b12..4940a88 100644 --- a/benchmark/build.gradle +++ b/benchmark/build.gradle @@ -27,7 +27,8 @@ dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.4.0' - compile 'com.github.tgio:rncryptor-native:0.0.8' + //compile 'com.github.tgio:rncryptor-native:0.0.8' compile project(':jncryptor') + compile project(':rncryptor-native') compile 'com.github.PhilJay:MPAndroidChart:v2.2.4' } diff --git a/benchmark/src/main/java/tgio/benchmark/MainActivity.java b/benchmark/src/main/java/tgio/benchmark/MainActivity.java index ab888a7..b601571 100644 --- a/benchmark/src/main/java/tgio/benchmark/MainActivity.java +++ b/benchmark/src/main/java/tgio/benchmark/MainActivity.java @@ -55,6 +55,16 @@ protected void onCreate(Bundle savedInstanceState) { getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main); + + + /*char[] arr = new char[1024]; + arr = RNCryptorNative.generateKey("12", password); + String key = String.valueOf(arr); + System.out.println(key);*/ + byte[] arr = new byte[1024]; + arr = RNCryptorNative.generateKey("1298", password); + + handler = new Handler(); mChart = (BarChart) findViewById(R.id.chart1); mChart.setOnChartValueSelectedListener(this); @@ -106,6 +116,7 @@ public void run() { }); } + void setupStrings() { RandomString rnstr = new RandomString(200); for (int i = 0; i < INTSTRINGS_COUNT; i++) { diff --git a/build.gradle b/build.gradle index d54de86..fa2afb2 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.1.0' + classpath 'com.android.tools.build:gradle:2.1.2' classpath "com.android.tools.build:gradle-experimental:0.7.0" classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' diff --git a/jncryptor/jncryptor.iml b/jncryptor/jncryptor.iml new file mode 100644 index 0000000..918e942 --- /dev/null +++ b/jncryptor/jncryptor.iml @@ -0,0 +1,116 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/rncryptor-native/src/main/java/tgio/rncryptor/RNCryptorNative.java b/rncryptor-native/src/main/java/tgio/rncryptor/RNCryptorNative.java index 36a7e0d..80bf7d8 100644 --- a/rncryptor-native/src/main/java/tgio/rncryptor/RNCryptorNative.java +++ b/rncryptor-native/src/main/java/tgio/rncryptor/RNCryptorNative.java @@ -54,6 +54,14 @@ public class RNCryptorNative { */ public native String decrypt(String encrypted, String password); + /** + * Generates Key with salt and password. + * @param salt + * @param password + + */ + public static native byte[] generateKey(final String salt, final String password); + /** * Decrypts encrypted base64 string and returns via callback diff --git a/rncryptor-native/src/main/jni/rncryptor-native.cpp b/rncryptor-native/src/main/jni/rncryptor-native.cpp index d0e6293..ba78495 100644 --- a/rncryptor-native/src/main/jni/rncryptor-native.cpp +++ b/rncryptor-native/src/main/jni/rncryptor-native.cpp @@ -68,6 +68,27 @@ Java_tgio_rncryptor_RNCryptorNative_getABI(JNIEnv *env, jobject thiz) { #endif return (env)->NewStringUTF(ABI); } +jbyteArray Java_tgio_rncryptor_RNCryptorNative_generateKey(JNIEnv *env, jobject instance, const jstring salt_, const jstring password_) { + const char *salt = env->GetStringUTFChars(salt_, 0); + const char *password = env->GetStringUTFChars(password_, 0); + RNCryptor *cryptor = new RNCryptor(); + string value = (char *) cryptor->generateKey(salt, password).data(); + const char *buf = value.c_str(); + + delete cryptor; + env->ReleaseStringUTFChars(salt_, salt); + env->ReleaseStringUTFChars(password_, password); + jbyteArray array = env->NewByteArray (1024); + env->SetByteArrayRegion (array, 0, 1024, (jbyte *)(buf)); + return array; + /*char array[1024]; + strcpy(array, value.c_str()); + jcharArray charArr = env->NewCharArray(1024); + env->SetCharArrayRegion(charArr, 0, 1024, (jchar *) array); + return charArr;*/ + + +} jbyteArray Java_tgio_rncryptor_RNCryptorNative_encrypt(JNIEnv *env, jobject instance, jstring raw_, jstring password_) { const char *raw = env->GetStringUTFChars(raw_, 0); diff --git a/rncryptor-native/src/main/jni/rncryptor-native.h b/rncryptor-native/src/main/jni/rncryptor-native.h index 14f99f1..27eb474 100644 --- a/rncryptor-native/src/main/jni/rncryptor-native.h +++ b/rncryptor-native/src/main/jni/rncryptor-native.h @@ -26,6 +26,7 @@ extern "C" { JNIEXPORT jstring JNICALL Java_tgio_rncryptor_RNCryptorNative_getABI(JNIEnv* env, jobject thiz); JNIEXPORT jbyteArray JNICALL Java_tgio_rncryptor_RNCryptorNative_encrypt(JNIEnv *env, jobject instance, jstring raw_, jstring password_); JNIEXPORT jstring JNICALL Java_tgio_rncryptor_RNCryptorNative_decrypt(JNIEnv *env, jobject instance, jstring encrypted_, jstring password_); + JNIEXPORT jbyteArray JNICALL Java_tgio_rncryptor_RNCryptorNative_generateKey(JNIEnv *env, jobject instance, const jstring salt_, const jstring password_); #ifdef __cplusplus } #endif diff --git a/rncryptor-native/src/main/jni/rncryptor.cpp b/rncryptor-native/src/main/jni/rncryptor.cpp index 5186b0c..ba41464 100755 --- a/rncryptor-native/src/main/jni/rncryptor.cpp +++ b/rncryptor-native/src/main/jni/rncryptor.cpp @@ -130,8 +130,7 @@ string RNCryptor::generateHmac(RNCryptorPayloadComponents components, string pas return hmac; } - -SecByteBlock RNCryptor::generateKey(const string salt, const string password) + SecByteBlock RNCryptor::generateKey(const string salt, const string password) { SecByteBlock key(RNCryptor::pbkdf2_keyLength); diff --git a/rncryptor-native/src/main/jni/rncryptor.h b/rncryptor-native/src/main/jni/rncryptor.h index d3b1a24..2c2bb3b 100755 --- a/rncryptor-native/src/main/jni/rncryptor.h +++ b/rncryptor-native/src/main/jni/rncryptor.h @@ -57,7 +57,7 @@ class RNCryptor { RNCryptorHmacAlgorithm hmac_algorithm; string generateHmac(RNCryptorPayloadComponents components, string password); - SecByteBlock generateKey(const string salt, const string password); + static string base64_encode(string plaintext); static string base64_decode(string encoded); @@ -72,9 +72,11 @@ class RNCryptor { static const int pbkdf2_iterations = 10000; static const short pbkdf2_keyLength = 32; static const short hmac_length = 32; + static SecByteBlock generateKey(const string salt, const string password); RNCryptor(); void configureSettings(RNCryptorSchema schemaVersion); + }; #endif