Guest User

Untitled

a guest
Dec 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.90 KB | None | 0 0
  1. package markgordienko.carcare;
  2.  
  3. import android.content.Intent;
  4. import android.content.SharedPreferences;
  5. import android.os.Bundle;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.text.InputFilter;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import java.nio.charset.StandardCharsets;
  12. import java.security.MessageDigest;
  13. import java.security.NoSuchAlgorithmException;
  14. import android.content.SharedPreferences.Editor;
  15. import android.widget.Toast;
  16.  
  17.  
  18. public class LogIn extends AppCompatActivity {
  19.     Button logbutt;
  20.     EditText logfield , passfield;
  21.     static String user;
  22.     static SharedPreferences prefs;
  23.     static String saved_text;
  24.     static Editor ed;
  25.  
  26.     @Override
  27.     protected void onCreate(Bundle savedInstanceState) {
  28.         super.onCreate(savedInstanceState);
  29.         setContentView(R.layout.activity_log_in);
  30.         logbutt = (Button) findViewById(R.id.logbutt);
  31.         logfield = (EditText) findViewById(R.id.logfield);
  32.         passfield = (EditText) findViewById(R.id.passfield);
  33.  
  34.         int maxLength = 16;
  35.         logfield.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)});
  36.         passfield.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)});
  37.  
  38.  
  39.             prefs = getPreferences(MODE_PRIVATE);
  40.         ed = prefs.edit();
  41.         user = prefs.getString(user,"");
  42.  
  43.             if (user != ""){
  44.                 Intent myintent = new Intent(LogIn.this, Main.class);
  45.                 startActivity(myintent);
  46.             }
  47.  
  48.         logbutt.setOnClickListener(new View.OnClickListener() {
  49.             @Override
  50.  
  51.  
  52.             public void onClick(View view) {
  53.                 String login , password, passwordhash;
  54.                 login = logfield.getText().toString();
  55.                 password = passfield.getText().toString();
  56.                 String salt = "salt";
  57.                 passwordhash = get_SHA_512_SecurePassword(password, salt);
  58.  
  59.                 DatabaseAccess databaseAccess=DatabaseAccess.getInstance(getApplicationContext());
  60.                 databaseAccess.open();
  61.                 boolean bool = databaseAccess.LogIn(login,passwordhash);
  62.                 if (bool==true){
  63.                     Toast.makeText(LogIn.this,"Успешно!",Toast.LENGTH_LONG).show();
  64.  
  65.                     user = logfield.getText().toString();
  66.                     prefs = getPreferences(MODE_PRIVATE);
  67.                     ed = prefs.edit();
  68.  
  69.                     ed.putString(saved_text,logfield.getText().toString());
  70.                     ed.putBoolean("isLog",true);
  71.                     ed.commit();
  72.  
  73.                     Intent myintent = new Intent(LogIn.this, Main.class);
  74.                     startActivity(myintent);
  75.                 }
  76.                 else {
  77.                     Toast.makeText(LogIn.this,"Неверный логин или пароль",Toast.LENGTH_LONG).show();
  78.                 }
  79.             }
  80.         });
  81.     }
  82.     public void sendMessage(View view){
  83.         Intent intent = new Intent(this, registration.class);
  84.         startActivity(intent);
  85.     }
  86.  
  87.     public String get_SHA_512_SecurePassword(String passwordToHash, String   salt){
  88.         String generatedPassword = null;
  89.         try {
  90.             MessageDigest md = MessageDigest.getInstance("SHA-512");
  91.             md.update(salt.getBytes(StandardCharsets.UTF_8));
  92.             byte[] bytes = md.digest(passwordToHash.getBytes(StandardCharsets.UTF_8));
  93.             StringBuilder sb = new StringBuilder();
  94.             for(int i=0; i< bytes.length ;i++){
  95.                 sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
  96.             }
  97.             generatedPassword = sb.toString();
  98.         }
  99.         catch (NoSuchAlgorithmException e){
  100.             e.printStackTrace();
  101.         }
  102.         return generatedPassword;
  103.     }
  104.     @Override
  105.     public void onBackPressed() {
  106.     }
  107. }
Add Comment
Please, Sign In to add comment