diff --git a/exampleapp/build.gradle b/exampleapp/build.gradle new file mode 100644 index 0000000..e37740e --- /dev/null +++ b/exampleapp/build.gradle @@ -0,0 +1,18 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 26 + buildToolsVersion "26.0.1" + defaultConfig { + applicationId "com.google.keytransparency.exampleapp" + minSdkVersion 18 + targetSdkVersion 26 + versionCode 1 + versionName "1.0" + } +} + +dependencies { + compile project(':keytransparency') + compile 'com.android.support:appcompat-v7:26.+' +} diff --git a/exampleapp/src/main/AndroidManifest.xml b/exampleapp/src/main/AndroidManifest.xml new file mode 100644 index 0000000..163999c --- /dev/null +++ b/exampleapp/src/main/AndroidManifest.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + diff --git a/exampleapp/src/main/java/com/google/keytransparency/exampleapp/KeyTransparencyExample.java b/exampleapp/src/main/java/com/google/keytransparency/exampleapp/KeyTransparencyExample.java new file mode 100644 index 0000000..ad26ec9 --- /dev/null +++ b/exampleapp/src/main/java/com/google/keytransparency/exampleapp/KeyTransparencyExample.java @@ -0,0 +1,99 @@ +package com.google.keytransparency.exampleapp; + +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.text.method.ScrollingMovementMethod; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.TextView; + +import com.google.keytransparency.client.KeyTransparencyClient; +import com.google.keytransparency.client.KeyTransparencyException; +import com.google.keytransparency.client.LogReceiver; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; + +import static android.R.attr.button; + +public class KeyTransparencyExample extends AppCompatActivity { + + private static final String TAG_LOGS_FROM_GOBIND = "GoKtClient:"; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_key_transparency_example); + final TextView tv = (TextView) findViewById(R.id.textView); + + tv.setText(""); + tv.setMovementMethod(new ScrollingMovementMethod()); + + KeyTransparencyClient.addVerboseLogsDestination(new LogReceiver() { + @Override + public long write(byte[] bytes) throws Exception { + tv.append(TAG_LOGS_FROM_GOBIND + new String(bytes, "UTF-8")); + return bytes.length; + } + }); + + final EditText urlEditText = (EditText) findViewById(R.id.ktURL); + urlEditText.setText("35.184.134.53:8080"); + + final EditText emailEditText = (EditText) findViewById(R.id.email); + emailEditText.setText("non_existing_user@gmail.com"); + final EditText appIdEditText = (EditText) findViewById(R.id.appId); + appIdEditText.setText("app1"); + + final Button addServerButton = (Button) findViewById(R.id.addServerButton); + addServerButton.setOnClickListener(new View.OnClickListener() { + public void onClick(View v) { + String ktUrl = urlEditText.getText().toString(); + tv.append("\n\n --- AddServer test --- \n"); + tv.append("\nAdding " + ktUrl + "as a KeyTransparency server\n"); + try { + KeyTransparencyClient.addKtServer(ktUrl, true, null, null); + } catch (KeyTransparencyException e) { + tv.append("\nError connecting to the server: " + e.getMessage()); + e.printStackTrace(); + } + } + }); + + final Button getEntryButton = (Button) findViewById(R.id.getEntryButton); + getEntryButton.setOnClickListener(new View.OnClickListener() { + public void onClick(View v) { + String email = emailEditText.getText().toString(); + String appId = appIdEditText.getText().toString(); + String ktUrl = urlEditText.getText().toString(); + + tv.append("\n\n --- GetEntry test --- \n"); + tv.append("\nTrying to get public key for (" + email + "," + appId + ") from server " + ktUrl + "\n"); + + try { + byte[] entry = KeyTransparencyClient.getEntry(ktUrl, email, appId); + if (entry == null) { + tv.append("Received key is null: entry does not exists"); + } else { + tv.append("Received key: " + bytesToHex(entry)); + } + } catch (KeyTransparencyException e) { + tv.append("\nError getting the key: " + e.getMessage()); + } + } + }); + } + + private final static char[] hexArray = "0123456789ABCDEF".toCharArray(); + public static String bytesToHex(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); + } + +} diff --git a/exampleapp/src/main/keytransparency-web.png b/exampleapp/src/main/keytransparency-web.png new file mode 100644 index 0000000..f5d6341 Binary files /dev/null and b/exampleapp/src/main/keytransparency-web.png differ diff --git a/exampleapp/src/main/keytransparency_round-web.png b/exampleapp/src/main/keytransparency_round-web.png new file mode 100644 index 0000000..bffdb82 Binary files /dev/null and b/exampleapp/src/main/keytransparency_round-web.png differ diff --git a/exampleapp/src/main/res/layout/activity_key_transparency_example.xml b/exampleapp/src/main/res/layout/activity_key_transparency_example.xml new file mode 100644 index 0000000..9606541 --- /dev/null +++ b/exampleapp/src/main/res/layout/activity_key_transparency_example.xml @@ -0,0 +1,86 @@ + + + + + + + +