diff --git a/app/build.gradle b/app/build.gradle index 9f5e1d3..4f5aad9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -37,7 +37,7 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.material:material:1.10.0' 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' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' diff --git a/app/src/main/java/com/example/bsm_notatnik/MainActivity.java b/app/src/main/java/com/example/bsm_notatnik/MainActivity.java index d5f64f6..3119d2c 100644 --- a/app/src/main/java/com/example/bsm_notatnik/MainActivity.java +++ b/app/src/main/java/com/example/bsm_notatnik/MainActivity.java @@ -1,5 +1,4 @@ package com.example.bsm_notatnik; -import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; @@ -28,9 +27,7 @@ public class MainActivity extends AppCompatActivity { Button buttonLogout, buttonChangePassword, buttonAddNewNote; private static final String SHARED_NAME_CREDENTIALS = "Credentials"; private static final String SHARED_NOTES_NAME = "Notes"; - private static String HASHED_EMAIL = ""; - private List noteList; private LinearLayout notesContainer; @@ -46,15 +43,13 @@ public class MainActivity extends AppCompatActivity { notesContainer = findViewById(R.id.notesContainer); noteList = new ArrayList<>(); - loadNotesFromPreferences(); + loadNotesFromPreferencesToList(); displayNotes(); buttonLogout = findViewById(R.id.btn_logout); buttonChangePassword = findViewById(R.id.btn_change_password); buttonAddNewNote = findViewById(R.id.btn_add_note); - - buttonLogout.setOnClickListener(view -> logOut()); buttonChangePassword.setOnClickListener(view -> showPasswordChangeDialog(current_username_hashed)); @@ -63,6 +58,9 @@ public class MainActivity extends AppCompatActivity { } + + + private void logOut(){ Toast.makeText(getApplicationContext(), "Logout Successful!", Toast.LENGTH_SHORT).show(); @@ -73,18 +71,14 @@ public class MainActivity extends AppCompatActivity { private void showPasswordChangeDialog(String hashedEmail){ - // Inflate the dialog layout LayoutInflater inflater = getLayoutInflater(); View dialogView = inflater.inflate(R.layout.password_change_dialog, null); - // Create the AlertDialog builder AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setView(dialogView); builder.setTitle("Change Password"); - // Set up the positive (OK) button builder.setPositiveButton("Change", (dialogInterface, i) -> { - // Handle password change logic here EditText editTextOldPassword = dialogView.findViewById(R.id.editTextOldPassword); EditText editTextNewPassword = dialogView.findViewById(R.id.editTextNewPassword); EditText editTextConfirmPassword = dialogView.findViewById(R.id.editTextConfirmPassword); @@ -108,7 +102,6 @@ public class MainActivity extends AppCompatActivity { return; } - // Perform password change validation and logic if (newPassword.equals(confirmPassword)) { updatePassword(hashedEmail, newPassword); Toast.makeText(MainActivity.this, "Password Changed", Toast.LENGTH_SHORT).show(); @@ -125,84 +118,6 @@ public class MainActivity extends AppCompatActivity { 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){ final String PASSWORD_PATTERN = "^.{6,}$"; 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){ SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NOTES_NAME, MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); if (mode.equals("del")){ - editor.clear(); + int noteCount = sharedPreferences.getInt("notecount_"+HASHED_EMAIL, 0); + for(int i=0; i deleteNoteAndRefresh(note)); builder.setNegativeButton("Cancel", null); builder.show(); } @@ -339,6 +332,26 @@ public class MainActivity extends AppCompatActivity { createNoteView(note); } } + + + + + + + + + + + + + + + + + + + + } diff --git a/app/src/main/java/com/example/bsm_notatnik/Register.java b/app/src/main/java/com/example/bsm_notatnik/Register.java index 3ac4c7f..c7d5f07 100644 --- a/app/src/main/java/com/example/bsm_notatnik/Register.java +++ b/app/src/main/java/com/example/bsm_notatnik/Register.java @@ -12,16 +12,7 @@ import android.widget.EditText; import android.widget.ProgressBar; import android.widget.TextView; 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.regex.Matcher; @@ -106,10 +97,12 @@ public class Register extends AppCompatActivity { - byte[] salt = Utility.generateSalt(); - saveSaltForUser(hashedEmail, salt); + byte[] salt1 = Utility.generateSalt(); + byte[] salt2 = Utility.generateSalt(); + saveSaltsForUser(hashedEmail, salt1, salt2); - hashedPassword = Utility.hashCredential(password, salt); + + hashedPassword = Utility.hashCredential(password, salt1); saveNewUser(hashedEmail, hashedPassword); @@ -143,12 +136,17 @@ public class Register extends AppCompatActivity { 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.Editor editor = sharedPreferences.edit(); - String saltString = Base64.getEncoder().encodeToString(salt); - editor.putString("salt_" + hashedemail, saltString); + String salt1String = Base64.getEncoder().encodeToString(salt1); + String salt2String = Base64.getEncoder().encodeToString(salt2); + + editor.putString("salt_" + hashedemail, salt1String); + editor.putString("salt_2_" + hashedemail, salt2String); + + editor.apply(); }