WIP: adding wolfssl-jni to project
This commit is contained in:
parent
37ec1d1c66
commit
1780a8942d
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
|
||||
*/
|
||||
package io.malloc.ccc;
|
||||
|
||||
import android.support.v4.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.wolfssl.wolfcrypt.Aes;
|
||||
import com.wolfssl.wolfcrypt.WolfCryptException;
|
||||
|
||||
import io.malloc.ccc.Util;
|
||||
/*
|
||||
* Simple Java UI to trigger jni function. It is exactly same as Java code
|
||||
* in hello-jni.
|
||||
*/
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
private static final byte[] KEY = Util.h2b("00112233445566778899AABBCCDDEEFF");
|
||||
private static final byte[] IV = Util.h2b("000102030405060708090A0B0C0D0E0F");
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
|
||||
// Aes aes = new Aes();
|
||||
// TextView tv = new TextView(this);
|
||||
// tv.setText( stringFromJNI() );
|
||||
// setContentView(tv);
|
||||
}
|
||||
// public native String stringFromJNI();
|
||||
static {
|
||||
|
||||
System.loadLibrary("wolfssl-jni");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
// final Button button = (Button) findViewById(R.id.AESbutton);
|
||||
// button.setOnClickListener(new View.OnClickListener() {
|
||||
// public void onClick(View v) {
|
||||
// checkSetKeyParams();
|
||||
// }
|
||||
// });
|
||||
}
|
||||
|
||||
public void checkSetKeyParams() {
|
||||
/* iv is optional, should not raise. */
|
||||
Aes aes = new Aes(KEY, null, Aes.ENCRYPT_MODE);
|
||||
|
||||
try {
|
||||
aes.setKey(null, IV, Aes.ENCRYPT_MODE);
|
||||
} catch (WolfCryptException e) {
|
||||
System.out.println("ERROR: WolfCrypt: J3G: "+e.getMessage());
|
||||
}
|
||||
|
||||
aes.setKey(KEY, IV, Aes.ENCRYPT_MODE);
|
||||
aes.releaseNativeStruct();
|
||||
|
||||
try {
|
||||
aes.setKey(KEY, IV, Aes.ENCRYPT_MODE);
|
||||
} catch (WolfCryptException e) {
|
||||
System.out.println("ERROR: WolfCrypt: J3G: "+e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
|
||||
*/
|
||||
package io.malloc.ccc;
|
||||
|
||||
class Util {
|
||||
public static byte[] h2b(String s) {
|
||||
int len = s.length();
|
||||
byte[] data = new byte[len / 2];
|
||||
|
||||
for (int i = 0; i < len; i += 2) {
|
||||
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character
|
||||
.digit(s.charAt(i + 1), 16));
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
|
||||
|
||||
public static String b2h(byte[] bytes) {
|
||||
char[] hexChars = new char[bytes.length * 2];
|
||||
|
||||
for (int j = 0; j < bytes.length; j++) {
|
||||
int v = bytes[j] & 0xFF;
|
||||
hexChars[j * 2] = hexArray[v >>> 4];
|
||||
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
|
||||
}
|
||||
|
||||
return new String(hexChars);
|
||||
}
|
||||
}
|
Binary file not shown.
Loading…
Reference in New Issue