2019-10-15 13:33:13 +02:00
|
|
|
package com.zadanie1.mw;
|
|
|
|
|
2019-10-19 16:45:05 +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;
|
|
|
|
import android.app.Activity;
|
2019-10-19 16:45:05 +02:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
2019-10-15 13:33:13 +02:00
|
|
|
|
2019-11-16 23:21:46 +01:00
|
|
|
import androidx.annotation.RequiresApi;
|
|
|
|
|
2019-10-21 22:26:54 +02:00
|
|
|
import java.io.File;
|
2019-10-19 16:45:05 +02:00
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileOutputStream;
|
2019-10-21 22:26:54 +02:00
|
|
|
import java.io.FileReader;
|
2019-10-19 16:45:05 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.PrintWriter;
|
2019-11-16 23:21:46 +01:00
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2019-10-15 13:33:13 +02:00
|
|
|
|
2019-10-19 16:45:05 +02:00
|
|
|
public class FormPassword extends Activity {
|
|
|
|
private static final String FILE_NAME = "passwd.txt";
|
2019-11-16 23:21:46 +01:00
|
|
|
private static String input;
|
2019-10-21 22:26:54 +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-11-16 23:21:46 +01:00
|
|
|
public static String SHA512(String s)
|
|
|
|
{
|
|
|
|
MessageDigest digest;
|
|
|
|
String generatedPassword = null;
|
|
|
|
String salt = "cnsakn";
|
|
|
|
try
|
|
|
|
{
|
|
|
|
MessageDigest md = MessageDigest.getInstance("SHA-512");
|
|
|
|
md.update(salt.getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
|
|
|
byte[] bytes = md.digest(s.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;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-15 13:33:13 +02:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
2019-10-21 22:26:54 +02:00
|
|
|
|
2019-10-15 13:33:13 +02:00
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_form_password);
|
|
|
|
|
2019-10-21 22:26:54 +02:00
|
|
|
final EditText currentpassword;
|
2019-10-19 16:45:05 +02:00
|
|
|
final EditText newpassword;
|
|
|
|
final EditText renewpassword;
|
|
|
|
Button confirm;
|
|
|
|
|
2019-10-21 22:26:54 +02:00
|
|
|
currentpassword = (EditText) findViewById(R.id.currentpassword);
|
2019-10-19 16:45:05 +02:00
|
|
|
newpassword = (EditText) findViewById(R.id.newpassword);
|
|
|
|
renewpassword = (EditText) findViewById(R.id.renewpassword);
|
|
|
|
confirm = (Button) findViewById(R.id.button);
|
|
|
|
|
|
|
|
confirm.setOnClickListener(new View.OnClickListener() {
|
|
|
|
public void onClick(View arg0) {
|
2019-10-21 22:26:54 +02:00
|
|
|
final String filename = getFilesDir() + "/passwd.txt";
|
|
|
|
File file = new File(filename);
|
2019-10-19 16:45:05 +02:00
|
|
|
|
2019-11-16 23:21:46 +01:00
|
|
|
input = currentpassword.getText().toString();
|
|
|
|
String SHAinput = unSHA512(input);
|
|
|
|
currentpassword.setText("");
|
|
|
|
String password = null;
|
|
|
|
|
2019-10-21 22:26:54 +02:00
|
|
|
try {
|
2019-11-16 23:21:46 +01:00
|
|
|
password = readFile(filename).toString();
|
|
|
|
if(SHAinput.equals(password)){
|
2019-10-21 22:26:54 +02:00
|
|
|
if (newpassword.getText().toString().equals(renewpassword.getText().toString())) {
|
2019-10-15 13:33:13 +02:00
|
|
|
|
2019-11-16 23:21:46 +01:00
|
|
|
String text = SHA512(newpassword.getText().toString());
|
2019-10-21 22:26:54 +02:00
|
|
|
FileOutputStream fos = null;
|
2019-10-15 13:33:13 +02:00
|
|
|
|
2019-10-21 22:26:54 +02:00
|
|
|
Toast toast = Toast.makeText(getApplicationContext(),
|
|
|
|
"Password has been changed",
|
|
|
|
Toast.LENGTH_SHORT);
|
|
|
|
toast.show();
|
2019-10-15 13:33:13 +02:00
|
|
|
|
2019-10-19 16:45:05 +02:00
|
|
|
try {
|
2019-10-21 22:26:54 +02:00
|
|
|
fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
|
|
|
|
fos.write(text.getBytes());
|
|
|
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
e.printStackTrace();
|
2019-10-19 16:45:05 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2019-10-21 22:26:54 +02:00
|
|
|
} finally {
|
|
|
|
if (fos != null) {
|
|
|
|
try {
|
|
|
|
fos.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2019-10-19 16:45:05 +02:00
|
|
|
}
|
2019-10-21 22:26:54 +02:00
|
|
|
onBackPressed();
|
|
|
|
} else {
|
|
|
|
Toast toast = Toast.makeText(getApplicationContext(),
|
|
|
|
"Password has NOT been changed!",
|
|
|
|
Toast.LENGTH_SHORT);
|
|
|
|
toast.show();
|
2019-10-19 16:45:05 +02:00
|
|
|
}
|
2019-10-21 22:26:54 +02:00
|
|
|
} else {
|
2019-10-19 16:45:05 +02:00
|
|
|
Toast toast = Toast.makeText(getApplicationContext(),
|
2019-10-21 22:26:54 +02:00
|
|
|
"Current password is WRONG!",
|
2019-10-19 16:45:05 +02:00
|
|
|
Toast.LENGTH_SHORT);
|
|
|
|
toast.show();
|
|
|
|
}
|
2019-10-21 22:26:54 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2019-11-16 23:21:46 +01:00
|
|
|
|
2019-10-21 22:26:54 +02:00
|
|
|
currentpassword.getText().clear();
|
|
|
|
newpassword.getText().clear();
|
|
|
|
renewpassword.getText().clear();
|
2019-10-19 16:45:05 +02:00
|
|
|
}
|
|
|
|
});
|
2019-10-15 13:33:13 +02:00
|
|
|
}
|
|
|
|
}
|