Using SharedPreferences to store password : SHA « Security « Android






Using SharedPreferences to store password

 
package app.test;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;

public class Test extends Activity {
  public static final String SETTING_INFOS = "SETTING_Infos";
  public static final String NAME = "NAME";
  public static final String PASSWORD = "PASSWORD";
  
  private EditText field_name;
  private EditText filed_pass;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        field_name = (EditText) findViewById(R.id.name);
        filed_pass = (EditText) findViewById(R.id.password);
        SharedPreferences settings = getSharedPreferences(SETTING_INFOS, 0);
    String name = settings.getString(NAME, "");
    String password = settings.getString(PASSWORD, "");
    field_name.setText(name);
    filed_pass.setText(password);
    }
    protected void onStop(){
        super.onStop();
    SharedPreferences settings = getSharedPreferences(SETTING_INFOS, 0);
    settings.edit()
      .putString(NAME, field_name.getText().toString())
      .putString(PASSWORD, filed_pass.getText().toString())
      .commit();
    }
}
//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="SharedPreferences demo"
    />
<TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="Name:" />
    
<EditText android:id="@+id/name" 
  android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="" />
  
<TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="Password:" />
    
    
<EditText android:id="@+id/password" 
  android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:password="true"
    android:text="" />  
    
</LinearLayout>

   
  








Related examples in the same category

1.SHA-1 string
2.hmac Sha1 Digest
3.Sha1 hashes based on a given String
4.SHA1 Utils
5.Using SharedPreferences
6.Drawing Shapes
7.Animated wallpaper draws a rotating wireframe shape with a choice of 2 shapes
8.Animation: shake
9.Get reference from SharedPreferences
10.Glutes shape
11.Reshaping Arabic Sentences and Text Utilities to deal with Arabic
12.Save SharedPreferences
13.Save value to SharedPreferences
14.SharedPreferences Set and get value
15.Compute the SHA-1 hash of the given byte array
16.compute SHA-1 Hash