summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorMatthew Wong <wongma@protonmail.ch>2015-07-14 22:41:04 -0400
committerMatthew Wong <wongma@protonmail.ch>2015-07-14 22:41:04 -0400
commit6f25a8bb232833e78c23ca67f157f75d109d421b (patch)
treec869a580fc2f5b6cf9de6a2b112e0e2420f55380 /app
parent32cf7f7813c89f7eff43712705b1d1453637a180 (diff)
Create ssh keygen activity with two fragments: the keygen & a screen to show the generated public key
Diffstat (limited to 'app')
-rw-r--r--app/src/main/AndroidManifest.xml7
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/SshKeyGen.java141
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/pwgenDialogFragment.java2
-rw-r--r--app/src/main/res/layout/activity_ssh_keygen.xml7
-rw-r--r--app/src/main/res/layout/fragment_show_ssh_key.xml50
-rw-r--r--app/src/main/res/layout/fragment_ssh_keygen.xml59
6 files changed, 265 insertions, 1 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 7a566d3e..8deb8826 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -33,6 +33,13 @@
android:value="com.zeapo.pwdstore.PasswordStore" />
</activity>
+ <activity android:name=".SshKeyGen"
+ android:parentActivityName=".PasswordStore">
+
+ <meta-data android:name="android.support.PARENT_ACTIVITY"
+ android:value="com.zeapo.pwdstore.PasswordStore" />
+ </activity>
+
<activity android:name="net.rdrei.android.dirchooser.DirectoryChooserActivity" />
</application>
diff --git a/app/src/main/java/com/zeapo/pwdstore/SshKeyGen.java b/app/src/main/java/com/zeapo/pwdstore/SshKeyGen.java
new file mode 100644
index 00000000..a1c76ffb
--- /dev/null
+++ b/app/src/main/java/com/zeapo/pwdstore/SshKeyGen.java
@@ -0,0 +1,141 @@
+package com.zeapo.pwdstore;
+
+import android.app.Fragment;
+import android.content.ClipData;
+import android.content.ClipboardManager;
+import android.content.Context;
+import android.os.Bundle;
+import android.support.v7.app.AppCompatActivity;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.view.inputmethod.InputMethodManager;
+import android.widget.ArrayAdapter;
+import android.widget.Spinner;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import com.jcraft.jsch.JSch;
+import com.jcraft.jsch.KeyPair;
+
+import org.apache.commons.io.FileUtils;
+
+import java.io.File;
+import java.io.FileOutputStream;
+
+public class SshKeyGen extends AppCompatActivity {
+
+ // SSH key generation UI
+ public static class SshKeyGenFragment extends Fragment {
+ public SshKeyGenFragment() {
+ }
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ View v = inflater.inflate(R.layout.fragment_ssh_keygen, container, false);
+
+ Spinner spinner = (Spinner) v.findViewById(R.id.length);
+ Integer[] lengths = new Integer[]{2048, 4096};
+ ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(getActivity(),
+ android.R.layout.simple_spinner_dropdown_item, lengths);
+ spinner.setAdapter(adapter);
+
+ return v;
+ }
+ }
+
+ // Displays the generated public key .ssh_key.pub
+ public static class ShowSshKeyFragment extends Fragment {
+ public ShowSshKeyFragment() {
+ }
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ View v = inflater.inflate(R.layout.fragment_show_ssh_key, container, false);
+
+ TextView textView = (TextView) v.findViewById(R.id.public_key);
+ File file = new File(getActivity().getFilesDir() + "/.ssh_key.pub");
+ try {
+ textView.setText(FileUtils.readFileToString(file));
+ Toast.makeText(getActivity(), "SSH-key generated", Toast.LENGTH_LONG).show();
+ } catch (Exception e) {
+ System.out.println("Exception caught :(");
+ e.printStackTrace();
+ }
+
+ v.findViewById(R.id.ok_ssh_key).setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ getActivity().finish();
+ }
+ });
+
+ return v;
+ }
+ }
+
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ getSupportActionBar().setDisplayHomeAsUpEnabled(true);
+
+ setTitle("Generate SSH Key");
+
+ setContentView(R.layout.activity_ssh_keygen);
+
+ if (savedInstanceState == null) {
+ getFragmentManager().beginTransaction()
+ .replace(android.R.id.content, new SshKeyGenFragment()).commit();
+ }
+ }
+
+ // Invoked when 'Generate' button of SshKeyGenFragment clicked. Generates a
+ // private and public key, then replaces the SshKeyGenFragment with a
+ // ShowSshKeyFragment which displays the public key.
+ public void generate(View view) {
+ InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
+ imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
+
+ Spinner spinner = (Spinner) findViewById(R.id.length);
+ int length = (Integer) spinner.getSelectedItem();
+
+ TextView textView = (TextView) findViewById(R.id.passphrase);
+ String passphrase = textView.getText().toString();
+
+ textView = (TextView) findViewById(R.id.comment);
+ String comment = textView.getText().toString();
+
+ JSch jsch = new JSch();
+ try {
+ KeyPair kp = KeyPair.genKeyPair(jsch, KeyPair.RSA, length);
+
+ File file = new File(getFilesDir() + "/.ssh_key");
+ FileOutputStream out = new FileOutputStream(file, false);
+ kp.writePrivateKey(out, passphrase.getBytes());
+
+ file = new File(getFilesDir() + "/.ssh_key.pub");
+ out = new FileOutputStream(file, false);
+ kp.writePublicKey(out, comment);
+ } catch (Exception e) {
+ System.out.println("Exception caught :(");
+ e.printStackTrace();
+ return;
+ }
+ getFragmentManager().beginTransaction()
+ .replace(android.R.id.content, new ShowSshKeyFragment()).commit();
+ }
+
+ // Invoked when 'Copy' button of ShowSshKeyFragment clicked. Copies the
+ // displayed public key to the clipboard.
+ public void copy (View view) {
+ TextView textView = (TextView) findViewById(R.id.public_key);
+ ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
+ ClipData clip = ClipData.newPlainText("public key", textView.getText().toString());
+ clipboard.setPrimaryClip(clip);
+ }
+
+}
diff --git a/app/src/main/java/com/zeapo/pwdstore/pwgenDialogFragment.java b/app/src/main/java/com/zeapo/pwdstore/pwgenDialogFragment.java
index af905b5d..0e2b96ef 100644
--- a/app/src/main/java/com/zeapo/pwdstore/pwgenDialogFragment.java
+++ b/app/src/main/java/com/zeapo/pwdstore/pwgenDialogFragment.java
@@ -116,7 +116,7 @@ public class pwgenDialogFragment extends DialogFragment {
try {
int length = Integer.valueOf(textView.getText().toString());
return pwgen.setPrefs(getActivity().getApplicationContext(), preferences, length);
- } catch(NumberFormatException e) {
+ } catch (NumberFormatException e) {
return pwgen.setPrefs(getActivity().getApplicationContext(), preferences);
}
}
diff --git a/app/src/main/res/layout/activity_ssh_keygen.xml b/app/src/main/res/layout/activity_ssh_keygen.xml
new file mode 100644
index 00000000..fb3d8a25
--- /dev/null
+++ b/app/src/main/res/layout/activity_ssh_keygen.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:orientation="vertical"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent">
+
+</LinearLayout> \ No newline at end of file
diff --git a/app/src/main/res/layout/fragment_show_ssh_key.xml b/app/src/main/res/layout/fragment_show_ssh_key.xml
new file mode 100644
index 00000000..6590a98f
--- /dev/null
+++ b/app/src/main/res/layout/fragment_show_ssh_key.xml
@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="utf-8"?>
+<ScrollView
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent">
+
+ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:orientation="vertical">
+
+ <TextView
+ android:id="@+id/public_key"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:layout_margin="16dp"
+ android:textIsSelectable="true"/>
+
+ <TextView
+ android:id="@+id/public_key_tip"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginLeft="16dp"
+ android:text="@string/ssh_keygen_tip"
+ android:textAppearance="?android:attr/textAppearanceMedium"/>
+
+ <LinearLayout
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="right"
+ android:layout_marginRight="16dp"
+ android:layout_marginTop="8dp"
+ android:orientation="horizontal">
+
+ <Button
+ android:id="@+id/copy_public_key"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="@string/ssh_keygen_copy"
+ android:onClick="copy" />
+
+ <Button
+ android:id="@+id/ok_ssh_key"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="@string/dialog_ok"/>
+ </LinearLayout>
+
+ </LinearLayout>
+</ScrollView> \ No newline at end of file
diff --git a/app/src/main/res/layout/fragment_ssh_keygen.xml b/app/src/main/res/layout/fragment_ssh_keygen.xml
new file mode 100644
index 00000000..0ebea432
--- /dev/null
+++ b/app/src/main/res/layout/fragment_ssh_keygen.xml
@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="utf-8"?>
+<ScrollView
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent">
+
+ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:orientation="vertical">
+
+ <TextView
+ android:id="@+id/label_length"
+ android:layout_width="wrap_content"
+ android:layout_height="48dp"
+ android:layout_marginLeft="16dp"
+ android:gravity="center_vertical"
+ android:text="Length"/>
+
+ <Spinner
+ android:id="@+id/length"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:layout_marginLeft="16dp"
+ android:layout_marginRight="16dp"
+ android:spinnerMode="dropdown"/>
+
+ <EditText
+ android:id="@+id/passphrase"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:layout_marginLeft="16dp"
+ android:layout_marginRight="16dp"
+ android:layout_marginTop="8dp"
+ android:hint="Passphrase"
+ android:inputType="textVisiblePassword"/>
+
+ <EditText
+ android:id="@+id/comment"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:layout_marginLeft="16dp"
+ android:layout_marginRight="16dp"
+ android:layout_marginTop="8dp"
+ android:hint="Comment"
+ android:inputType="textShortMessage"/>
+
+ <Button
+ android:id="@+id/generate_ssh_key"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:layout_marginLeft="16dp"
+ android:layout_marginRight="16dp"
+ android:layout_marginTop="8dp"
+ android:onClick="generate"
+ android:text="Generate"/>
+
+ </LinearLayout>
+</ScrollView> \ No newline at end of file