package edu.erau.hackriddle.cryptapic;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private EditText userField;
    private EditText passField;
    private Button accountButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        userField = (EditText) findViewById(R.id.editUsername);
        passField = (EditText) findViewById(R.id.editPassword);
        accountButton = (Button) findViewById(R.id.accountButton);

        if (keysExist()) {
            userField.setVisibility(View.GONE);
            //accountButton.setVisibility(View.GONE);
        }
    }

    private boolean keysExist() {
        return new File(getFilesDir(), "userKeys").exists();
    }

    public void attemptLogin(View view) {
        //Going to auth later...
        Intent gallery = new Intent(this, GalleryView.class);
        startActivity(gallery);
    }

    public void recoverAccount(View view) {
        //This is all test code for the keysExist method
        File testFile = new File(getFilesDir(), "userKeys");
        if (keysExist()) {
            if (!testFile.delete()) {
                Log.wtf("cryptapic", "Couldn't delete test file though exists?");
            }
            Toast.makeText(this, "Keys exist-- deleted", Toast.LENGTH_SHORT).show();
        } else {
            try (FileOutputStream stream = new FileOutputStream(testFile)) {
                stream.write(0x30);
            } catch (IOException e) {
                Toast.makeText(this, "Key creation failed", Toast.LENGTH_SHORT)
                        .show();
                Log.e("cryptapic", e.toString(), e);
            }
            Toast.makeText(this, "Keys created", Toast.LENGTH_SHORT).show();
        }

        //"Actual" code
        //Intent accountExists = new Intent(this, AccountExists.class);
        //startActivity(accountExists);
    }
}

@@ -420,16 +420,313 @@
 vity %7B%0A%0A
+    /**%0A     * Prepend this string to private keys before encrypting them with the user%0A     * password. Use this to detect whether later decryption is successful.%0A     */%0A    private static final String PRIVATE_KEY_PREFIX = %22CAP-PK%22;%0A    public static final String KEY_FILE_NAME = %22.userKeys%22;%0A%0A%0A
     priv
@@ -1395,26 +1395,29 @@
 sDir(), 
-%22userKeys%22
+KEY_FILE_NAME
 ).exists
@@ -1481,125 +1481,1313 @@
     
-//Go
+Log.v(%22cryptapic%22, %22Login attempt.%22);%0A        if (keysExist()) %7B%0A            //Log 
 in
-g
  t
-o auth later...%0A        Intent gallery = new Intent(this, GalleryView.class);%0A        startActivity(gallery)
+he existing user%0A            String decryptResult = decryptPrivateKey(passField.getText().toString());%0A            if (decryptResult.startsWith(PRIVATE_KEY_PREFIX)) %7B%0A                //Successfully decrypted%0A%0A                Intent gallery = new Intent(this, GalleryView.class);%0A                //TODO Strip prefix from PK & store this in the intent%0A%0A                //Open the gallery!%0A                startActivity(gallery);%0A            %7D else %7B%0A                //Decrypt fail%0A                passField.setText(%22%22);%0A                Toast.makeText(this, %22The password was wrong%22, Toast.LENGTH_LONG).show();%0A            %7D%0A        %7D else %7B%0A            Log.v(%22cryptapic%22, %22Creating a new user!%22);%0A            //Create new key file/DB user%0A%0A            //Generate an RSA? key pair securely%0A%0A            //Write the public key to KEY_FILE_NAME as the first? line%0A%0A            //Encrypt PRIVATE_KEY_PREFIX + the private key using the user password%0A%0A            //Write that as the second line of keyfile%0A%0A            //Publish public key & username to AWS or similar%0A%0A        %7D%0A    %7D%0A%0A    private String decryptPrivateKey(String userPassword) %7B%0A        return PRIVATE_KEY_PREFIX + %22result of decrypting PK in KEY_FILE_NAME%22
 ;%0A  
@@ -2944,18 +2944,21 @@
 (), 
-%22userKeys%22
+KEY_FILE_NAME
 );%0A 

Add framework comments & etc for login process