• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Implementing security class into App

Wes94

Lurker
I'm working with a few friends to develop an android app relating to fitness and have a question about including a class of mine. What currently happens is when the user clicks the "CREATE AN ACCOUNT" button, they are referred to the create account page and are prompted to enter their credentials from there. Once that's taken care of, they're redirected back to the login page to then login to their respective accounts.

What I'd like to have happen is to have user passwords hashed via my written class before input for account creation, and to then also compare hashed input against the hash stored in the database for login. I removed the imports from the linked code to reduce the amount to scroll through. Thanx for the tips The code that pertains to this is as follows:
/**create account screen**/
package fitfastapp;



public class CreateAccount extends AppCompatActivity {

SharedPreferences sPref;
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";
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<String, Object> 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 fitfastapp;



/**
* 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<String, Object> 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();
}
}
}

and finally the class that provides the hashing utility


public class Authenticator {

public static byte[] generateHash(String password, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException {
String algorithm = "PBKDF2WithHmacSHA512";
int length = 512;
int iterations = 60000;
KeySpec sp = new PBEKeySpec(password.toCharArray(), salt, iterations, length);
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());
}



}
 
Last edited:
Back
Top Bottom