2019-10-15 13:33:13 +02:00
|
|
|
package com.zadanie1.mw;
|
2019-11-16 23:21:46 +01:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.RequiresApi;
|
2019-10-15 13:33:13 +02:00
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
2019-11-16 23:21:46 +01:00
|
|
|
import androidx.biometric.BiometricPrompt;
|
2019-10-15 13:33:13 +02:00
|
|
|
import android.content.Intent;
|
2019-11-16 23:21:46 +01:00
|
|
|
import android.os.Build;
|
2019-10-15 13:33:13 +02:00
|
|
|
import android.os.Bundle;
|
2019-11-16 23:21:46 +01:00
|
|
|
import android.os.Handler;
|
2019-10-15 13:33:13 +02:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.view.View.OnClickListener;
|
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.TextView;
|
2019-10-19 16:45:05 +02:00
|
|
|
import android.widget.Toast;
|
2019-10-15 13:33:13 +02:00
|
|
|
|
|
|
|
import java.io.File;
|
2019-10-17 13:40:41 +02:00
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.IOException;
|
2019-11-16 23:21:46 +01:00
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.util.concurrent.Executor;
|
2019-10-15 13:33:13 +02:00
|
|
|
|
|
|
|
|
2019-10-17 13:40:41 +02:00
|
|
|
public class MainActivity extends AppCompatActivity {
|
2019-10-15 13:33:13 +02:00
|
|
|
TextView response;
|
|
|
|
Button button;
|
|
|
|
Button restartpassword;
|
|
|
|
private EditText textPassword;
|
|
|
|
private View viewPassword;
|
2019-10-17 13:40:41 +02:00
|
|
|
private static final String filename = "passwd.txt";
|
2019-11-16 23:21:46 +01:00
|
|
|
private static String input;
|
2019-10-17 13:40:41 +02:00
|
|
|
|
|
|
|
public String readFile(String filename) throws IOException {
|
|
|
|
String content = null;
|
|
|
|
File file = new File(filename); // For example, foo.txt
|
|
|
|
FileReader reader = null;
|
|
|
|
try {
|
|
|
|
reader = new FileReader(file);
|
|
|
|
char[] chars = new char[(int) file.length()];
|
|
|
|
reader.read(chars);
|
|
|
|
content = new String(chars);
|
|
|
|
reader.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} finally {
|
|
|
|
if(reader != null){
|
|
|
|
reader.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
2019-10-21 22:26:54 +02:00
|
|
|
private boolean isFileExists(File file) {
|
|
|
|
return file.exists() && !file.isDirectory();
|
|
|
|
|
|
|
|
}
|
2019-10-17 13:40:41 +02:00
|
|
|
|
2019-11-16 23:21:46 +01:00
|
|
|
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
|
|
|
|
private String unSHA512(String passwordToHash)
|
|
|
|
{
|
|
|
|
String generatedPassword = null;
|
|
|
|
String salt = "cnsakn";
|
|
|
|
try
|
|
|
|
{
|
|
|
|
MessageDigest md = MessageDigest.getInstance("SHA-512");
|
|
|
|
md.update(salt.getBytes(StandardCharsets.UTF_8));
|
|
|
|
byte[] bytes = md.digest(passwordToHash.getBytes(StandardCharsets.UTF_8));
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
for(int i=0; i< bytes.length ;i++)
|
|
|
|
{
|
|
|
|
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
|
|
|
|
}
|
|
|
|
generatedPassword = sb.toString();
|
|
|
|
}
|
|
|
|
catch (NoSuchAlgorithmException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return generatedPassword;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Handler handler = new Handler();
|
|
|
|
|
|
|
|
private Executor executor = new Executor() {
|
|
|
|
@Override
|
|
|
|
public void execute(Runnable command) {
|
|
|
|
handler.post(command);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-10-15 13:33:13 +02:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_main);
|
|
|
|
|
2019-10-21 22:26:54 +02:00
|
|
|
final String filename = getFilesDir() + "/passwd.txt";
|
|
|
|
File file = new File(filename);
|
|
|
|
|
|
|
|
if (!isFileExists(file)){
|
|
|
|
Intent myIntent3 = new Intent(MainActivity.this,
|
|
|
|
FirstPassword.class);
|
|
|
|
startActivity(myIntent3);
|
|
|
|
}
|
2019-11-16 23:21:46 +01:00
|
|
|
else { showBiometricPrompt();
|
|
|
|
}
|
|
|
|
|
2019-10-21 22:26:54 +02:00
|
|
|
|
2019-10-15 13:33:13 +02:00
|
|
|
response = (TextView) findViewById(R.id.textView);
|
|
|
|
button = (Button) findViewById(R.id.button);
|
|
|
|
restartpassword = (Button) findViewById(R.id.restartpassword);
|
|
|
|
textPassword = findViewById(R.id.editText);
|
|
|
|
|
|
|
|
restartpassword.setOnClickListener(new OnClickListener() {
|
|
|
|
public void onClick(View arg0) {
|
|
|
|
Intent myIntent = new Intent(MainActivity.this,
|
|
|
|
FormPassword.class);
|
|
|
|
startActivity(myIntent);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
button.setOnClickListener(new OnClickListener() {
|
2019-10-17 13:40:41 +02:00
|
|
|
private final String filename = getFilesDir() + "/passwd.txt";
|
2019-10-19 16:45:05 +02:00
|
|
|
|
2019-10-15 13:33:13 +02:00
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
2019-10-19 16:45:05 +02:00
|
|
|
File file = new File(filename);
|
|
|
|
|
2019-11-16 23:21:46 +01:00
|
|
|
try {
|
|
|
|
input = textPassword.getText().toString();
|
|
|
|
String SHAinput = unSHA512(input);
|
|
|
|
textPassword.setText("");
|
|
|
|
String password = readFile(filename).toString();
|
|
|
|
|
|
|
|
if (isFileExists(file)) {
|
|
|
|
|
|
|
|
if(SHAinput.equals(password)){
|
|
|
|
Toast toast = Toast.makeText(getApplicationContext(),
|
|
|
|
"Password is correct!",
|
|
|
|
Toast.LENGTH_SHORT);
|
|
|
|
toast.show();
|
|
|
|
Intent myIntent2 = new Intent(MainActivity.this,
|
|
|
|
Noto.class);
|
|
|
|
startActivity(myIntent2);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
Toast toast = Toast.makeText(getApplicationContext(),
|
|
|
|
"Password is NOT correct",
|
|
|
|
Toast.LENGTH_SHORT);
|
|
|
|
toast.show();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Intent myIntent3 = new Intent(MainActivity.this,
|
|
|
|
FirstPassword.class);
|
|
|
|
startActivity(myIntent3);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch(IOException e){
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2019-10-21 22:26:54 +02:00
|
|
|
textPassword.getText().clear();
|
2019-10-15 13:33:13 +02:00
|
|
|
}
|
|
|
|
});
|
2019-10-21 22:26:54 +02:00
|
|
|
|
2019-10-15 13:33:13 +02:00
|
|
|
}
|
2019-11-16 23:21:46 +01:00
|
|
|
public static String getInput() {
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void showBiometricPrompt() {
|
|
|
|
BiometricPrompt.PromptInfo promptInfo =
|
|
|
|
new BiometricPrompt.PromptInfo.Builder()
|
|
|
|
.setTitle("Biometric login for my app")
|
|
|
|
.setSubtitle("Log in using your biometric credential")
|
|
|
|
.setNegativeButtonText("Cancel")
|
|
|
|
.build();
|
|
|
|
|
|
|
|
BiometricPrompt biometricPrompt = new BiometricPrompt(MainActivity.this,
|
|
|
|
executor, new BiometricPrompt.AuthenticationCallback() {
|
|
|
|
@Override
|
|
|
|
public void onAuthenticationError(int errorCode,
|
|
|
|
@NonNull CharSequence errString) {
|
|
|
|
super.onAuthenticationError(errorCode, errString);
|
|
|
|
Toast.makeText(getApplicationContext(),
|
|
|
|
"Authentication error: " + errString, Toast.LENGTH_SHORT)
|
|
|
|
.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAuthenticationSucceeded(
|
|
|
|
@NonNull BiometricPrompt.AuthenticationResult result) {
|
|
|
|
super.onAuthenticationSucceeded(result);
|
|
|
|
BiometricPrompt.CryptoObject authenticatedCryptoObject =
|
|
|
|
result.getCryptoObject();
|
|
|
|
// User has verified the signature, cipher, or message
|
|
|
|
// authentication code (MAC) associated with the crypto object,
|
|
|
|
// so you can use it in your app's crypto-driven workflows.
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAuthenticationFailed() {
|
|
|
|
super.onAuthenticationFailed();
|
|
|
|
Toast.makeText(getApplicationContext(), "Authentication failed",
|
|
|
|
Toast.LENGTH_SHORT)
|
|
|
|
.show();
|
|
|
|
try {
|
|
|
|
Thread.sleep(1000);
|
|
|
|
System.exit(1);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Displays the "log in" prompt.
|
|
|
|
biometricPrompt.authenticate(promptInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-15 13:33:13 +02:00
|
|
|
}
|