gotowe do prezentacji
This commit is contained in:
parent
2736b3df36
commit
91f6890acd
@ -9,26 +9,15 @@ import androidx.core.content.ContextCompat;
|
|||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.security.keystore.KeyGenParameterSpec;
|
|
||||||
import android.security.keystore.KeyProperties;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.Toast;
|
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 java.util.concurrent.Executor;
|
||||||
|
|
||||||
import javax.crypto.Cipher;
|
|
||||||
import javax.crypto.KeyGenerator;
|
|
||||||
import javax.crypto.SecretKey;
|
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
private static final String KEY_NAME = "nazwaKlucza";
|
|
||||||
Button btnLogIn;
|
Button btnLogIn;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -86,16 +75,13 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
});
|
});
|
||||||
|
|
||||||
promptInfo = new BiometricPrompt.PromptInfo.Builder()
|
promptInfo = new BiometricPrompt.PromptInfo.Builder()
|
||||||
.setTitle("Biometric login for my app")
|
.setTitle("Biometric login for your notes")
|
||||||
.setSubtitle("Log in using biometric credential")
|
.setSubtitle("Log in using biometric credential")
|
||||||
.setNegativeButtonText("xd")
|
.setNegativeButtonText(" ")
|
||||||
.setAllowedAuthenticators(BiometricManager.Authenticators.BIOMETRIC_STRONG)
|
.setAllowedAuthenticators(BiometricManager.Authenticators.BIOMETRIC_STRONG)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
btnLogIn.setOnClickListener(view -> {
|
btnLogIn.setOnClickListener(view -> biometricPrompt.authenticate(promptInfo));
|
||||||
biometricPrompt.authenticate(promptInfo);
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,15 +2,11 @@ package com.example.bsm_fingerptint_tutorial;
|
|||||||
|
|
||||||
import androidx.appcompat.app.AlertDialog;
|
import androidx.appcompat.app.AlertDialog;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.biometric.BiometricPrompt;
|
|
||||||
import androidx.lifecycle.LifecycleCoroutineScope;
|
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.security.keystore.KeyGenParameterSpec;
|
|
||||||
import android.security.keystore.KeyProperties;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
@ -21,17 +17,11 @@ import android.widget.TextView;
|
|||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import java.security.GeneralSecurityException;
|
import java.security.GeneralSecurityException;
|
||||||
import java.security.KeyStore;
|
|
||||||
import java.security.SecureRandom;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.crypto.Cipher;
|
|
||||||
import javax.crypto.KeyGenerator;
|
|
||||||
import javax.crypto.SecretKey;
|
import javax.crypto.SecretKey;
|
||||||
import javax.crypto.spec.IvParameterSpec;
|
import javax.crypto.spec.IvParameterSpec;
|
||||||
|
|
||||||
@ -39,13 +29,11 @@ public class NotepadActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
private List<Note> noteList;
|
private List<Note> noteList;
|
||||||
private LinearLayout notesContainer;
|
private LinearLayout notesContainer;
|
||||||
private static final String SHARED_NAME_NOTES = "Notatki";
|
private static final String SHARED_NAME_NOTES = "Notes";
|
||||||
|
private static final String KEY_NAME = "MyKey1";
|
||||||
private static final String KEY_NAME = "nazwaKluczaa";
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Button buttonLogout, buttonAddNote;
|
Button buttonLogout, buttonAddNote;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
@ -57,7 +45,7 @@ public class NotepadActivity extends AppCompatActivity {
|
|||||||
try {
|
try {
|
||||||
loadNotesFromPreferencesToList();
|
loadNotesFromPreferencesToList();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
Log.e("Error from NotepadActivity", "Caused by loadNotesFromPreferencesToList() invoked from onCreate()" + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
displayNotes();
|
displayNotes();
|
||||||
|
|
||||||
@ -99,10 +87,8 @@ public class NotepadActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
saveNotesToPreferences("add");
|
saveNotesToPreferences("add");
|
||||||
} catch (GeneralSecurityException e) {
|
} catch (GeneralSecurityException | IOException e) {
|
||||||
throw new RuntimeException(e);
|
Log.e("Error from NotepadActivity", "Caused by saveNotesToPreferences() invoked from showAddNoteDialog()" + e.getMessage(), e);
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
createNoteView(note);
|
createNoteView(note);
|
||||||
@ -146,10 +132,8 @@ public class NotepadActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
saveNotesToPreferences("add");
|
saveNotesToPreferences("add");
|
||||||
} catch (GeneralSecurityException e) {
|
} catch (GeneralSecurityException | IOException e) {
|
||||||
throw new RuntimeException(e);
|
Log.e("Error from NotepadActivity", "Caused by saveNotesToPreferences() invoked from showEditNoteDialog()" + e.getMessage(), e);
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}else {
|
}else {
|
||||||
@ -164,11 +148,11 @@ public class NotepadActivity extends AppCompatActivity {
|
|||||||
alertDialog.show();
|
alertDialog.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void saveNotesToPreferences(String mode) throws GeneralSecurityException, IOException {
|
private void saveNotesToPreferences(String mode) throws GeneralSecurityException, IOException {
|
||||||
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_NOTES, MODE_PRIVATE);
|
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_NOTES, MODE_PRIVATE);
|
||||||
SharedPreferences.Editor editor = sharedPreferences.edit();
|
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||||
|
|
||||||
|
|
||||||
if (mode.equals("del")){
|
if (mode.equals("del")){
|
||||||
int noteCount = sharedPreferences.getInt("_notecount_", 0);
|
int noteCount = sharedPreferences.getInt("_notecount_", 0);
|
||||||
for(int i=0; i<noteCount; i++){
|
for(int i=0; i<noteCount; i++){
|
||||||
@ -183,7 +167,6 @@ public class NotepadActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
SecretKey secretKey = UtilsCipher.generateRandomKey(KEY_NAME);
|
SecretKey secretKey = UtilsCipher.generateRandomKey(KEY_NAME);
|
||||||
|
|
||||||
|
|
||||||
editor.putInt("_notecount_", noteList.size());
|
editor.putInt("_notecount_", 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);
|
||||||
@ -204,12 +187,12 @@ public class NotepadActivity extends AppCompatActivity {
|
|||||||
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_NOTES, MODE_PRIVATE);
|
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_NOTES, MODE_PRIVATE);
|
||||||
int noteCount = sharedPreferences.getInt("_notecount_", 0);
|
int noteCount = sharedPreferences.getInt("_notecount_", 0);
|
||||||
|
|
||||||
|
if (noteCount!=0){
|
||||||
String ivString = getIVStringFromShared();
|
String ivString = getIVStringFromShared();
|
||||||
IvParameterSpec iv = stringToIv(ivString);
|
IvParameterSpec iv = stringToIv(ivString);
|
||||||
|
|
||||||
SecretKey secretKey = UtilsCipher.retrieveKey(KEY_NAME);
|
SecretKey secretKey = UtilsCipher.retrieveKey(KEY_NAME);
|
||||||
|
|
||||||
|
|
||||||
for(int i=0; i<noteCount; i++){
|
for(int i=0; i<noteCount; i++){
|
||||||
String title = sharedPreferences.getString(i + "_title_", "");
|
String title = sharedPreferences.getString(i + "_title_", "");
|
||||||
String content = sharedPreferences.getString(i + "_content_", "");
|
String content = sharedPreferences.getString(i + "_content_", "");
|
||||||
@ -224,6 +207,7 @@ public class NotepadActivity extends AppCompatActivity {
|
|||||||
noteList.add(note);
|
noteList.add(note);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void saveIvStringToShared(String ivString){
|
private void saveIvStringToShared(String ivString){
|
||||||
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_NOTES, MODE_PRIVATE);
|
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_NOTES, MODE_PRIVATE);
|
||||||
@ -249,7 +233,6 @@ public class NotepadActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void createNoteView(final Note note){
|
private void createNoteView(final Note note){
|
||||||
@SuppressLint("InflateParams") View noteView = getLayoutInflater().inflate(R.layout.note_item, null);
|
@SuppressLint("InflateParams") View noteView = getLayoutInflater().inflate(R.layout.note_item, null);
|
||||||
TextView noteTitleTextView = noteView.findViewById(R.id.note_title_text_view);
|
TextView noteTitleTextView = noteView.findViewById(R.id.note_title_text_view);
|
||||||
@ -284,10 +267,9 @@ public class NotepadActivity extends AppCompatActivity {
|
|||||||
noteList.remove(note);
|
noteList.remove(note);
|
||||||
try {
|
try {
|
||||||
saveNotesToPreferences("del");
|
saveNotesToPreferences("del");
|
||||||
} catch (GeneralSecurityException e) {
|
} catch (GeneralSecurityException | IOException e) {
|
||||||
throw new RuntimeException(e);
|
Log.e("Error from NotepadActivity", "Caused by saveNotesToPreferences() invoked from deleteNoteAndRefresh()" + e.getMessage(), e);
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
refreshNotesView();
|
refreshNotesView();
|
||||||
}
|
}
|
||||||
@ -296,23 +278,8 @@ public class NotepadActivity extends AppCompatActivity {
|
|||||||
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", (dialogInterface, i) -> {
|
builder.setPositiveButton("Delete", (dialogInterface, i) -> deleteNoteAndRefresh(note));
|
||||||
deleteNoteAndRefresh(note);
|
|
||||||
});
|
|
||||||
builder.setNegativeButton("Cancel", null);
|
builder.setNegativeButton("Cancel", null);
|
||||||
builder.show();
|
builder.show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,17 +2,12 @@ package com.example.bsm_fingerptint_tutorial;
|
|||||||
|
|
||||||
import android.security.keystore.KeyGenParameterSpec;
|
import android.security.keystore.KeyGenParameterSpec;
|
||||||
import android.security.keystore.KeyProperties;
|
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.GeneralSecurityException;
|
||||||
import java.security.InvalidAlgorithmParameterException;
|
import java.security.InvalidAlgorithmParameterException;
|
||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
import java.security.Key;
|
|
||||||
import java.security.KeyStore;
|
import java.security.KeyStore;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.security.NoSuchProviderException;
|
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
|
|
||||||
@ -23,14 +18,11 @@ import javax.crypto.KeyGenerator;
|
|||||||
import javax.crypto.NoSuchPaddingException;
|
import javax.crypto.NoSuchPaddingException;
|
||||||
import javax.crypto.SecretKey;
|
import javax.crypto.SecretKey;
|
||||||
import javax.crypto.spec.IvParameterSpec;
|
import javax.crypto.spec.IvParameterSpec;
|
||||||
import javax.crypto.spec.SecretKeySpec;
|
|
||||||
|
|
||||||
public class UtilsCipher {
|
public class UtilsCipher {
|
||||||
|
|
||||||
public static SecretKey generateRandomKey(String keyAlias) throws NoSuchProviderException,
|
// generates random key and saves it to keystore under keyAlias alias
|
||||||
NoSuchAlgorithmException {
|
public static SecretKey generateRandomKey(String keyAlias) throws GeneralSecurityException {
|
||||||
|
|
||||||
try {
|
|
||||||
KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
|
KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
|
||||||
KeyGenParameterSpec keyGenParameterSpec = new KeyGenParameterSpec.Builder(
|
KeyGenParameterSpec keyGenParameterSpec = new KeyGenParameterSpec.Builder(
|
||||||
keyAlias,
|
keyAlias,
|
||||||
@ -41,16 +33,11 @@ public class UtilsCipher {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
keyGenerator.init(keyGenParameterSpec);
|
keyGenerator.init(keyGenParameterSpec);
|
||||||
Log.i("KEY_GENERATEOT", keyGenerator.generateKey().toString());
|
|
||||||
return keyGenerator.generateKey();
|
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 {
|
public static SecretKey retrieveKey(String keyAlias) throws Exception {
|
||||||
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
|
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
|
||||||
keyStore.load(null);
|
keyStore.load(null);
|
||||||
@ -85,7 +72,4 @@ public class UtilsCipher {
|
|||||||
return new IvParameterSpec(iv);
|
return new IvParameterSpec(iv);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user