before aes

This commit is contained in:
mikgaw@st.amu.edu.pl 2023-12-05 11:16:09 +01:00
parent 06e5e3c578
commit 621158df66
3 changed files with 130 additions and 119 deletions

View File

@ -37,7 +37,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.10.0' implementation 'com.google.android.material:material:1.10.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.google.firebase:firebase-auth:22.3.0' implementation 'at.favre.lib:bcrypt:0.9.0'
testImplementation 'junit:junit:4.13.2' testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

View File

@ -1,5 +1,4 @@
package com.example.bsm_notatnik; package com.example.bsm_notatnik;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
@ -28,9 +27,7 @@ public class MainActivity extends AppCompatActivity {
Button buttonLogout, buttonChangePassword, buttonAddNewNote; Button buttonLogout, buttonChangePassword, buttonAddNewNote;
private static final String SHARED_NAME_CREDENTIALS = "Credentials"; private static final String SHARED_NAME_CREDENTIALS = "Credentials";
private static final String SHARED_NOTES_NAME = "Notes"; private static final String SHARED_NOTES_NAME = "Notes";
private static String HASHED_EMAIL = ""; private static String HASHED_EMAIL = "";
private List<Note> noteList; private List<Note> noteList;
private LinearLayout notesContainer; private LinearLayout notesContainer;
@ -46,15 +43,13 @@ public class MainActivity extends AppCompatActivity {
notesContainer = findViewById(R.id.notesContainer); notesContainer = findViewById(R.id.notesContainer);
noteList = new ArrayList<>(); noteList = new ArrayList<>();
loadNotesFromPreferences(); loadNotesFromPreferencesToList();
displayNotes(); displayNotes();
buttonLogout = findViewById(R.id.btn_logout); buttonLogout = findViewById(R.id.btn_logout);
buttonChangePassword = findViewById(R.id.btn_change_password); buttonChangePassword = findViewById(R.id.btn_change_password);
buttonAddNewNote = findViewById(R.id.btn_add_note); buttonAddNewNote = findViewById(R.id.btn_add_note);
buttonLogout.setOnClickListener(view -> logOut()); buttonLogout.setOnClickListener(view -> logOut());
buttonChangePassword.setOnClickListener(view -> showPasswordChangeDialog(current_username_hashed)); buttonChangePassword.setOnClickListener(view -> showPasswordChangeDialog(current_username_hashed));
@ -63,6 +58,9 @@ public class MainActivity extends AppCompatActivity {
} }
private void logOut(){ private void logOut(){
Toast.makeText(getApplicationContext(), "Logout Successful!", Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), "Logout Successful!", Toast.LENGTH_SHORT).show();
@ -73,18 +71,14 @@ public class MainActivity extends AppCompatActivity {
private void showPasswordChangeDialog(String hashedEmail){ private void showPasswordChangeDialog(String hashedEmail){
// Inflate the dialog layout
LayoutInflater inflater = getLayoutInflater(); LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.password_change_dialog, null); View dialogView = inflater.inflate(R.layout.password_change_dialog, null);
// Create the AlertDialog builder
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialogView); builder.setView(dialogView);
builder.setTitle("Change Password"); builder.setTitle("Change Password");
// Set up the positive (OK) button
builder.setPositiveButton("Change", (dialogInterface, i) -> { builder.setPositiveButton("Change", (dialogInterface, i) -> {
// Handle password change logic here
EditText editTextOldPassword = dialogView.findViewById(R.id.editTextOldPassword); EditText editTextOldPassword = dialogView.findViewById(R.id.editTextOldPassword);
EditText editTextNewPassword = dialogView.findViewById(R.id.editTextNewPassword); EditText editTextNewPassword = dialogView.findViewById(R.id.editTextNewPassword);
EditText editTextConfirmPassword = dialogView.findViewById(R.id.editTextConfirmPassword); EditText editTextConfirmPassword = dialogView.findViewById(R.id.editTextConfirmPassword);
@ -108,7 +102,6 @@ public class MainActivity extends AppCompatActivity {
return; return;
} }
// Perform password change validation and logic
if (newPassword.equals(confirmPassword)) { if (newPassword.equals(confirmPassword)) {
updatePassword(hashedEmail, newPassword); updatePassword(hashedEmail, newPassword);
Toast.makeText(MainActivity.this, "Password Changed", Toast.LENGTH_SHORT).show(); Toast.makeText(MainActivity.this, "Password Changed", Toast.LENGTH_SHORT).show();
@ -125,84 +118,6 @@ public class MainActivity extends AppCompatActivity {
alertDialog.show(); alertDialog.show();
} }
private void showAddNewNoteDialog(){
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.create_note_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialogView);
builder.setTitle("Create new note");
builder.setPositiveButton("Save", (dialogInterface, i) -> {
EditText noteTitleEditText = dialogView.findViewById(R.id.noteTitleEditText);
EditText noteContentEditText = dialogView.findViewById(R.id.noteContentEditText);
String title = noteTitleEditText.getText().toString();
String content = noteContentEditText.getText().toString();
if (!title.isEmpty() && !content.isEmpty()){
Note note = new Note();
note.setTitle(title);
note.setContent(content);
noteList.add(note);
saveNotesToPreferences("add");
createNoteView(note);
}
Toast.makeText(MainActivity.this, "Note saved!", Toast.LENGTH_SHORT).show();
});
builder.setNegativeButton("Cancel", (dialogInterface, i) -> dialogInterface.dismiss());
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
private void showEditNoteDialog(Note note){
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.create_note_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialogView);
builder.setTitle("Edit note");
builder.setPositiveButton("Save", (dialogInterface, i) -> {
EditText noteTitleEditText = dialogView.findViewById(R.id.noteTitleEditText);
EditText noteContentEditText = dialogView.findViewById(R.id.noteContentEditText);
String title = noteTitleEditText.getText().toString();
String content = noteContentEditText.getText().toString();
if (!title.isEmpty() && !content.isEmpty()){
deleteNoteAndRefresh(note);
note.setTitle(title);
note.setContent(content);
noteList.add(note);
saveNotesToPreferences("add");
createNoteView(note);
}
Toast.makeText(MainActivity.this, "Note Edited!", Toast.LENGTH_SHORT).show();
});
builder.setNegativeButton("Cancel", (dialogInterface, i) -> dialogInterface.dismiss());
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
private boolean validatePassword(String password){ private boolean validatePassword(String password){
final String PASSWORD_PATTERN = "^.{6,}$"; final String PASSWORD_PATTERN = "^.{6,}$";
Pattern pattern = Pattern.compile(PASSWORD_PATTERN); Pattern pattern = Pattern.compile(PASSWORD_PATTERN);
@ -245,14 +160,102 @@ public class MainActivity extends AppCompatActivity {
} }
private void showAddNewNoteDialog(){
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.create_note_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialogView);
builder.setTitle("Create new note");
builder.setPositiveButton("Save", (dialogInterface, i) -> {
EditText noteTitleEditText = dialogView.findViewById(R.id.noteTitleEditText);
EditText noteContentEditText = dialogView.findViewById(R.id.noteContentEditText);
String title = noteTitleEditText.getText().toString();
String content = noteContentEditText.getText().toString();
if (!title.isEmpty() && !content.isEmpty()){
Note note = new Note();
note.setTitle(title);
note.setContent(content);
noteList.add(note);
saveNotesToPreferences("add");
createNoteView(note);
}
Toast.makeText(MainActivity.this, "Note saved!", Toast.LENGTH_SHORT).show();
});
builder.setNegativeButton("Cancel", (dialogInterface, i) -> dialogInterface.dismiss());
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
private void showEditNoteDialog(Note note){
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.create_note_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialogView);
builder.setTitle("Edit note");
EditText noteTitleEditText = dialogView.findViewById(R.id.noteTitleEditText);
EditText noteContentEditText = dialogView.findViewById(R.id.noteContentEditText);
noteTitleEditText.setText(note.getTitle());
noteContentEditText.setText(note.getContent());
builder.setPositiveButton("Save", (dialogInterface, i) -> {
String title = noteTitleEditText.getText().toString();
String content = noteContentEditText.getText().toString();
if (!title.isEmpty() && !content.isEmpty()){
deleteNoteAndRefresh(note);
note.setTitle(title);
note.setContent(content);
noteList.add(note);
saveNotesToPreferences("add");
createNoteView(note);
}else {
Toast.makeText(MainActivity.this, "Enter title and content!", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", (dialogInterface, i) -> dialogInterface.dismiss());
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
private void genSecretKey(){
}
private void saveNotesToPreferences(String mode){ private void saveNotesToPreferences(String mode){
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NOTES_NAME, MODE_PRIVATE); SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NOTES_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit(); SharedPreferences.Editor editor = sharedPreferences.edit();
if (mode.equals("del")){ if (mode.equals("del")){
editor.clear(); int noteCount = sharedPreferences.getInt("notecount_"+HASHED_EMAIL, 0);
for(int i=0; i<noteCount; i++){
editor.remove(i + "_title_" + HASHED_EMAIL);
editor.remove(i + "_content_" + HASHED_EMAIL);
}
} }
editor.putInt("notecount_" + HASHED_EMAIL, noteList.size()); editor.putInt("notecount_" + HASHED_EMAIL, noteList.size());
for(int i=0; i<noteList.size(); i++){ for(int i=0; i<noteList.size(); i++){
Note note = noteList.get(i); Note note = noteList.get(i);
@ -263,7 +266,7 @@ public class MainActivity extends AppCompatActivity {
} }
private void loadNotesFromPreferences(){ private void loadNotesFromPreferencesToList(){
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NOTES_NAME, MODE_PRIVATE); SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NOTES_NAME, MODE_PRIVATE);
int noteCount = sharedPreferences.getInt("notecount_" + HASHED_EMAIL, 0); int noteCount = sharedPreferences.getInt("notecount_" + HASHED_EMAIL, 0);
@ -296,29 +299,19 @@ public class MainActivity extends AppCompatActivity {
noteView.setOnLongClickListener(new View.OnLongClickListener() { noteView.setOnLongClickListener(new View.OnLongClickListener() {
@Override @Override
public boolean onLongClick(View view) { public boolean onLongClick(View view) {
showEditNoteDialog(note); showEditNoteDialog(note);
return true; return true;
} }
}); });
notesContainer.addView(noteView); notesContainer.addView(noteView);
} }
private void showDeleteDialog(final Note note){ private void showDeleteDialog(final Note note){
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Delete this note"); builder.setTitle("Delete this note");
builder.setMessage("Are you sure you want to delete it?"); builder.setMessage("Are you sure you want to delete it?");
builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() { builder.setPositiveButton("Delete", (dialogInterface, i) -> deleteNoteAndRefresh(note));
@Override
public void onClick(DialogInterface dialogInterface, int i) {
deleteNoteAndRefresh(note);
}
});
builder.setNegativeButton("Cancel", null); builder.setNegativeButton("Cancel", null);
builder.show(); builder.show();
} }
@ -339,6 +332,26 @@ public class MainActivity extends AppCompatActivity {
createNoteView(note); createNoteView(note);
} }
} }
} }

View File

@ -12,16 +12,7 @@ import android.widget.EditText;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.Base64; import java.util.Base64;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -106,10 +97,12 @@ public class Register extends AppCompatActivity {
byte[] salt = Utility.generateSalt(); byte[] salt1 = Utility.generateSalt();
saveSaltForUser(hashedEmail, salt); byte[] salt2 = Utility.generateSalt();
saveSaltsForUser(hashedEmail, salt1, salt2);
hashedPassword = Utility.hashCredential(password, salt);
hashedPassword = Utility.hashCredential(password, salt1);
saveNewUser(hashedEmail, hashedPassword); saveNewUser(hashedEmail, hashedPassword);
@ -143,12 +136,17 @@ public class Register extends AppCompatActivity {
return matcher.matches(); return matcher.matches();
} }
private void saveSaltForUser(String hashedemail, byte[] salt){ private void saveSaltsForUser(String hashedemail, byte[] salt1, byte[] salt2){
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_CREDENTIALS, MODE_PRIVATE); SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_CREDENTIALS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit(); SharedPreferences.Editor editor = sharedPreferences.edit();
String saltString = Base64.getEncoder().encodeToString(salt); String salt1String = Base64.getEncoder().encodeToString(salt1);
editor.putString("salt_" + hashedemail, saltString); String salt2String = Base64.getEncoder().encodeToString(salt2);
editor.putString("salt_" + hashedemail, salt1String);
editor.putString("salt_2_" + hashedemail, salt2String);
editor.apply(); editor.apply();
} }