gotowe do prezentacji

This commit is contained in:
mikgaw@st.amu.edu.pl 2023-12-12 12:44:31 +01:00
parent 2736b3df36
commit 91f6890acd
3 changed files with 56 additions and 119 deletions

View File

@ -9,26 +9,15 @@ import androidx.core.content.ContextCompat;
import android.content.Intent;
import android.os.Bundle;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
import java.nio.charset.Charset;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.Arrays;
import java.util.concurrent.Executor;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class MainActivity extends AppCompatActivity {
private static final String KEY_NAME = "nazwaKlucza";
Button btnLogIn;
@Override
@ -86,16 +75,13 @@ public class MainActivity extends AppCompatActivity {
});
promptInfo = new BiometricPrompt.PromptInfo.Builder()
.setTitle("Biometric login for my app")
.setTitle("Biometric login for your notes")
.setSubtitle("Log in using biometric credential")
.setNegativeButtonText("xd")
.setNegativeButtonText(" ")
.setAllowedAuthenticators(BiometricManager.Authenticators.BIOMETRIC_STRONG)
.build();
btnLogIn.setOnClickListener(view -> {
biometricPrompt.authenticate(promptInfo);
});
btnLogIn.setOnClickListener(view -> biometricPrompt.authenticate(promptInfo));
}

View File

@ -2,15 +2,11 @@ package com.example.bsm_fingerptint_tutorial;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.biometric.BiometricPrompt;
import androidx.lifecycle.LifecycleCoroutineScope;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
@ -21,17 +17,11 @@ import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.nio.charset.Charset;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
@ -39,13 +29,11 @@ public class NotepadActivity extends AppCompatActivity {
private List<Note> noteList;
private LinearLayout notesContainer;
private static final String SHARED_NAME_NOTES = "Notatki";
private static final String KEY_NAME = "nazwaKluczaa";
private static final String SHARED_NAME_NOTES = "Notes";
private static final String KEY_NAME = "MyKey1";
Button buttonLogout, buttonAddNote;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -57,7 +45,7 @@ public class NotepadActivity extends AppCompatActivity {
try {
loadNotesFromPreferencesToList();
} catch (Exception e) {
throw new RuntimeException(e);
Log.e("Error from NotepadActivity", "Caused by loadNotesFromPreferencesToList() invoked from onCreate()" + e.getMessage(), e);
}
displayNotes();
@ -99,10 +87,8 @@ public class NotepadActivity extends AppCompatActivity {
try {
saveNotesToPreferences("add");
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (GeneralSecurityException | IOException e) {
Log.e("Error from NotepadActivity", "Caused by saveNotesToPreferences() invoked from showAddNoteDialog()" + e.getMessage(), e);
}
createNoteView(note);
@ -146,10 +132,8 @@ public class NotepadActivity extends AppCompatActivity {
try {
saveNotesToPreferences("add");
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (GeneralSecurityException | IOException e) {
Log.e("Error from NotepadActivity", "Caused by saveNotesToPreferences() invoked from showEditNoteDialog()" + e.getMessage(), e);
}
}else {
@ -164,11 +148,11 @@ public class NotepadActivity extends AppCompatActivity {
alertDialog.show();
}
private void saveNotesToPreferences(String mode) throws GeneralSecurityException, IOException {
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_NOTES, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
if (mode.equals("del")){
int noteCount = sharedPreferences.getInt("_notecount_", 0);
for(int i=0; i<noteCount; i++){
@ -183,7 +167,6 @@ public class NotepadActivity extends AppCompatActivity {
SecretKey secretKey = UtilsCipher.generateRandomKey(KEY_NAME);
editor.putInt("_notecount_", noteList.size());
for(int i=0; i<noteList.size(); i++){
Note note = noteList.get(i);
@ -204,12 +187,12 @@ public class NotepadActivity extends AppCompatActivity {
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_NOTES, MODE_PRIVATE);
int noteCount = sharedPreferences.getInt("_notecount_", 0);
if (noteCount!=0){
String ivString = getIVStringFromShared();
IvParameterSpec iv = stringToIv(ivString);
SecretKey secretKey = UtilsCipher.retrieveKey(KEY_NAME);
for(int i=0; i<noteCount; i++){
String title = sharedPreferences.getString(i + "_title_", "");
String content = sharedPreferences.getString(i + "_content_", "");
@ -224,6 +207,7 @@ public class NotepadActivity extends AppCompatActivity {
noteList.add(note);
}
}
}
private void saveIvStringToShared(String ivString){
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_NOTES, MODE_PRIVATE);
@ -249,7 +233,6 @@ public class NotepadActivity extends AppCompatActivity {
}
private void createNoteView(final Note note){
@SuppressLint("InflateParams") View noteView = getLayoutInflater().inflate(R.layout.note_item, null);
TextView noteTitleTextView = noteView.findViewById(R.id.note_title_text_view);
@ -284,10 +267,9 @@ public class NotepadActivity extends AppCompatActivity {
noteList.remove(note);
try {
saveNotesToPreferences("del");
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (GeneralSecurityException | IOException e) {
Log.e("Error from NotepadActivity", "Caused by saveNotesToPreferences() invoked from deleteNoteAndRefresh()" + e.getMessage(), e);
}
refreshNotesView();
}
@ -296,23 +278,8 @@ public class NotepadActivity extends AppCompatActivity {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Delete this note");
builder.setMessage("Are you sure you want to delete it?");
builder.setPositiveButton("Delete", (dialogInterface, i) -> {
deleteNoteAndRefresh(note);
});
builder.setPositiveButton("Delete", (dialogInterface, i) -> deleteNoteAndRefresh(note));
builder.setNegativeButton("Cancel", null);
builder.show();
}
}

View File

@ -2,17 +2,12 @@ package com.example.bsm_fingerptint_tutorial;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
import android.util.Log;
import java.io.IOException;
import java.nio.charset.Charset;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyStore;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.util.Base64;
@ -23,14 +18,11 @@ import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class UtilsCipher {
public static SecretKey generateRandomKey(String keyAlias) throws NoSuchProviderException,
NoSuchAlgorithmException {
try {
// generates random key and saves it to keystore under keyAlias alias
public static SecretKey generateRandomKey(String keyAlias) throws GeneralSecurityException {
KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
KeyGenParameterSpec keyGenParameterSpec = new KeyGenParameterSpec.Builder(
keyAlias,
@ -41,16 +33,11 @@ public class UtilsCipher {
.build();
keyGenerator.init(keyGenParameterSpec);
Log.i("KEY_GENERATEOT", keyGenerator.generateKey().toString());
return keyGenerator.generateKey();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
// returns key from keystore saved under keyAlias alias
public static SecretKey retrieveKey(String keyAlias) throws Exception {
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
@ -85,7 +72,4 @@ public class UtilsCipher {
return new IvParameterSpec(iv);
}
}