Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to include the Authenticator class into the CreateAccount class so t

ID: 3761231 • Letter: I

Question

I'm trying to include the Authenticator class into the CreateAccount class so that when a user creates an account, their password is hashed and the hash along with the salt for that user is stored in the DB. To log in, the user enters his/her credentials and if the hash that's stored in the DB matches the hash of the string they entered, they're logged in. I'm not sure how to go about this. At this point, sharedpreferences isn't unique to each user. Brand new to Android, obviously, and have no real experience dealing with something of this nature. Any suggestions as to going about this?

package fitfastapp;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.couchbase.lite.CouchbaseLiteException;
import com.couchbase.lite.Database;
import com.couchbase.lite.Document;
import com.couchbase.lite.Manager;
import com.couchbase.lite.android.AndroidContext;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


public class CreateAccount extends AppCompatActivity {

private final String PREFS = "SharedPreferences";
private final String genderKey = "gender";
private final String activityKey = "activity_level";
private final String timeKey = "time_frame";
private final String nameKey = "name";
private final String ageKey = "age";
private final String heightKey = "height";
private final String weightKey = "weight";
private final String goalKey = "target";
private final String passwordKey = "password";
SharedPreferences sPref;
private Context mContext;
private Manager manager;
private Database db;
  
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_account);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

sPref = getSharedPreferences(PREFS, Context.MODE_PRIVATE);


}

@Override
public void onResume(){
super.onResume();
mContext = getApplicationContext();
try {
manager = new Manager(new AndroidContext(mContext), Manager.DEFAULT_OPTIONS);
db = manager.getDatabase("accounts");
} catch (IOException io) {
Log.e("Manager Error:", "Cannot create Manager instance");
} catch (CouchbaseLiteException ce) {
Log.e("Couchbase Error:","Cannot open database");
}
}

public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
SharedPreferences.Editor editor = sPref.edit();
switch(view.getId()) {
case R.id.male:
if (checked)
editor.putString(genderKey,"Male");
break;
case R.id.female:
if (checked)
editor.putString(genderKey, "Female");
break;
case R.id.low:
if (checked)
editor.putString(activityKey,"Low");
break;
case R.id.medium:
if (checked)
editor.putString(activityKey,"Medium");
break;
case R.id.high:
if (checked)
editor.putString(activityKey,"high");
break;
case R.id.one:
if (checked)
editor.putString(timeKey,"3");
break;
case R.id.three:
if (checked)
editor.putString(timeKey,"6");
break;
case R.id.six:
if (checked)
editor.putString(timeKey,"12");
break;
}
editor.commit();
}

public void done(View view){
SharedPreferences.Editor editor = sPref.edit();

EditText editName = (EditText) findViewById(R.id.set_name);
String name = editName.getText().toString().toLowerCase().trim();
editor.putString(nameKey, name);

EditText editPassword = (EditText) findViewById(R.id.set_password);
String password = editPassword.getText().toString().trim();
editor.putString(passwordKey,password);

EditText editAge = (EditText) findViewById(R.id.set_age);
editor.putString(ageKey, editAge.getText().toString());

EditText editHeight = (EditText) findViewById(R.id.set_height);
editor.putString(heightKey, editHeight.getText().toString());

EditText editWeight = (EditText) findViewById(R.id.set_weight);
editor.putString(weightKey, editWeight.getText().toString());

EditText editGoal = (EditText) findViewById(R.id.set_goal);
editor.putString(goalKey, editGoal.getText().toString());

Document doc = db.getDocument(name);
Map properties = new HashMap<>();
try {
properties.put(passwordKey, password);
try {
doc.putProperties(properties);
Toast.makeText(mContext, String.format("Added user %s",name),Toast.LENGTH_LONG).show();
} catch (CouchbaseLiteException ce) {
Log.e("Couchbase Error:", "Error updating field for user :" + name);
}
} catch ( NullPointerException e) {
Toast.makeText(mContext, "Null pointer, whatever that means",
Toast.LENGTH_LONG).show();
}

editor.commit();

Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
}

package cse280.fitfastapp;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
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 com.couchbase.lite.CouchbaseLiteException;
import com.couchbase.lite.Database;
import com.couchbase.lite.Document;
import com.couchbase.lite.Manager;
import com.couchbase.lite.android.AndroidContext;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
* A login screen that offers login via email/password.
*/
public class LoginActivity extends AppCompatActivity {

EditText mEmailView, mPasswordView;
Button loginButton, accountButton;
private final String PREFS = "SharedPreferences";
private SharedPreferences sPref;
private Manager manager;
private Database db;
private static Context mContext;

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

mContext = getApplicationContext();
try {
manager = new Manager(new AndroidContext(mContext), Manager.DEFAULT_OPTIONS);
db = manager.getDatabase("accounts");
} catch (IOException io) {
Log.e("Manager Error:","Cannot create Manager instance");
return;
} catch (CouchbaseLiteException ce) {
Log.e("Couchbase Error:","Cannot open database");
}

sPref = getSharedPreferences(PREFS, Context.MODE_PRIVATE);
// Get Views from Login activity
mEmailView = (EditText) findViewById(R.id.userName);
mPasswordView = (EditText) findViewById(R.id.password);
loginButton = (Button) findViewById(R.id.email_sign_in_button);
accountButton = (Button) findViewById(R.id.create_an_account);


Map properties = new HashMap<>();
properties.put("type", "user");
properties.put("password", "pass1234");
properties.put("exists", true);
properties.put("encrypted", false);
Document document = db.getDocument("scensorECHO".toLowerCase());
try {
document.putProperties(properties);
} catch( CouchbaseLiteException ce ) {
Log.e("Couchbase Error:","Cannot submit document to database");
}

}

public void createNew(View view){
Intent intent = new Intent(this, CreateAccount.class);

String loginString = mEmailView.getText().toString();
String passString = mPasswordView.getText().toString();

SharedPreferences.Editor editor = sPref.edit();
editor.putString("login",loginString);
editor.putString("password",passString);
editor.commit();

startActivityForResult(intent, RESULT_OK);


}

public void login(View view) {
Intent intent = new Intent(this, DashBoard.class);
String loginString = mEmailView.getText().toString().trim();
String passString = mPasswordView.getText().toString().trim();

SharedPreferences.Editor editor = sPref.edit();
editor.putString("login",loginString);
editor.putString("password",passString);
editor.commit();

Document login = db.getDocument(loginString.toLowerCase());
try {
if (passString.equals(login.getProperty("password"))) {
startActivity(intent);
} else {
Toast.makeText(mContext, "Specified password does not match",
Toast.LENGTH_LONG).show();
}
} catch (NullPointerException e) {
Log.e("Document Error: ","No properties to retrieve");
Toast.makeText(mContext, "That user does not exist in our system",
Toast.LENGTH_LONG).show();
}
}
}

  
package fitfast.security;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.Arrays;

import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;


public class Authenticator {

public static byte[] generateHash(String password, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException {
String algorithm = "PBKDF2WithHmacSHA512";
int length = 512;
int iterations = 60000;
SecretKeyFactory kf = SecretKeyFactory.getInstance(algorithm);
return kf.generateSecret(sp).getEncoded();

}

public static byte[] generateSalt() throws NoSuchAlgorithmException {
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
byte[] salt = new byte[8];
sr.nextBytes(salt);
return salt;
}

public static boolean check(byte[] hash_Input, String hash_User) {
return Arrays.equals(hash_Input, hash_User.getBytes());
}

  

}

Explanation / Answer

}

}

}