Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package markgordienko.carcare;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.text.InputFilter;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import java.nio.charset.StandardCharsets;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import android.content.SharedPreferences.Editor;
- import android.widget.Toast;
- public class LogIn extends AppCompatActivity {
- Button logbutt;
- EditText logfield , passfield;
- static String user;
- static SharedPreferences prefs;
- static String saved_text;
- static Editor ed;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_log_in);
- logbutt = (Button) findViewById(R.id.logbutt);
- logfield = (EditText) findViewById(R.id.logfield);
- passfield = (EditText) findViewById(R.id.passfield);
- int maxLength = 16;
- logfield.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)});
- passfield.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)});
- prefs = getPreferences(MODE_PRIVATE);
- ed = prefs.edit();
- user = prefs.getString(user,"");
- if (user != ""){
- Intent myintent = new Intent(LogIn.this, Main.class);
- startActivity(myintent);
- }
- logbutt.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- String login , password, passwordhash;
- login = logfield.getText().toString();
- password = passfield.getText().toString();
- String salt = "salt";
- passwordhash = get_SHA_512_SecurePassword(password, salt);
- DatabaseAccess databaseAccess=DatabaseAccess.getInstance(getApplicationContext());
- databaseAccess.open();
- boolean bool = databaseAccess.LogIn(login,passwordhash);
- if (bool==true){
- Toast.makeText(LogIn.this,"Успешно!",Toast.LENGTH_LONG).show();
- user = logfield.getText().toString();
- prefs = getPreferences(MODE_PRIVATE);
- ed = prefs.edit();
- ed.putString(saved_text,logfield.getText().toString());
- ed.putBoolean("isLog",true);
- ed.commit();
- Intent myintent = new Intent(LogIn.this, Main.class);
- startActivity(myintent);
- }
- else {
- Toast.makeText(LogIn.this,"Неверный логин или пароль",Toast.LENGTH_LONG).show();
- }
- }
- });
- }
- public void sendMessage(View view){
- Intent intent = new Intent(this, registration.class);
- startActivity(intent);
- }
- public String get_SHA_512_SecurePassword(String passwordToHash, String salt){
- String generatedPassword = null;
- 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;
- }
- @Override
- public void onBackPressed() {
- }
- }
Add Comment
Please, Sign In to add comment