title: Secure shared preference android
body: <p>In my app I have to save username,password,token,.. to reuse them. Initially I save this data in normal shared preferences but on rooted device it's easy read all preferences and I think that this isn't a good thing. </p>
  
  <p>Now I'm trying to use SecurePreferences library (<a href="https://github.com/scottyab/secure-preferences" rel="nofollow">this</a>) but if I initialize this preferences in this way:</p>
  
  <pre><code>SharedPreferences prefs = new SecurePreferences(context);     
  </code></pre>
  
  <p>when I open an activity or in a fragment onCreateView the initialize often takes about 2-3 second (I tested it with TraceView) and this slow my activity or fragment opening.</p>
  
  <p>There is a way to put SecurePreferences in a Singleton and instantiate it only one time in all my app?
  Or there is another best method to save this data and hide them to all external application?</p>
  
  <p><strong>UPDATE #1:</strong></p>
  
  <p>I found this solution. To instantiate only one time the SecurePreferences you should create an App class that extends Application in this way:</p>
  
  <pre><code>public class App extends Application {
  
  
  private static final String TAG = "secureprefsample";
  protected static App instance;
  private SecurePreferences mSecurePrefs;
  private SecurePreferences mUserPrefs;
  public App(){
      super();
      instance = this;
  }
  public static App get() {
      return instance;
  }
  
  /**
   * Single point for the app to get the secure prefs object
   * @return
   */
  
  public SharedPreferences getSharedPreferences() {
      if(mSecurePrefs==null){
          mSecurePrefs = new SecurePreferences(this, "", "my_prefs.xml");
          SecurePreferences.setLoggingEnabled(true);
      }
      return mSecurePrefs;
  }
  
  
  /**
   * This is just an example of how you might want to create your own key with less iterations 1,000 rather than default 10,000. This makes it quicker but less secure.
   * @return
   */
  
  public SharedPreferences getSharedPreferences1000() {
      try {
          AesCbcWithIntegrity.SecretKeys myKey = AesCbcWithIntegrity.generateKeyFromPassword(Build.SERIAL,AesCbcWithIntegrity.generateSalt(),1000);
          SharedPreferences securePrefs1000 = new SecurePreferences(this, myKey, "my_prefs_1000.xml");
          return securePrefs1000;
      } catch (GeneralSecurityException e) {
          Log.e(TAG, "Failed to create custom key for SecurePreferences", e);
      }
      return null;
  }
  
  
  public SharedPreferences getDefaultSharedPreferences() {
      return PreferenceManager.getDefaultSharedPreferences(this);
  }
  
  public SecurePreferences getUserPinBasedSharedPreferences(String password){
      if(mUserPrefs==null) {
          mUserPrefs = new SecurePreferences(this, password, "user_prefs.xml");
      }
      return mUserPrefs;
  }
  
  public boolean changeUserPrefPassword(String newPassword){
      if(mUserPrefs!=null){
          try {
              mUserPrefs.handlePasswordChange(newPassword, this);
              return true;
          } catch (GeneralSecurityException e) {
              Log.e(TAG, "Error during password change", e);
          }
      }
      return false;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And then you must get the SecurePreferences in your activity in this way:</p>
  
  <pre><code>SharedPreferences preferences =App.get().getSharedPreferences();
  </code></pre>