Back to project page trivial-password.
The source code is released under:
MIT License
If you think the Android project trivial-password listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package org.hbabcock.trivialpassword; /* ww w . j av a 2 s.co m*/ import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import android.content.Context; import android.util.Log; public class PasswordManager { private static final String TAG = "PasswordManager"; private static final String FILENAME = "password"; private boolean mFoundPWFile; private byte[] mHint; private String mPassword; private byte[] mPasswordHash; private byte[] mSalt; private static Context mContext; private static PasswordManager sPasswordManager; private PasswordManager(Context c){ mFoundPWFile = false; mPassword = null; mContext = c; ObjectInputStream reader = null; try{ //c.deleteFile(FILENAME); InputStream in = c.openFileInput(FILENAME); reader = new ObjectInputStream(in); List<byte[]> b = (List<byte[]>) reader.readObject(); mHint = b.get(0); mPasswordHash = b.get(1); mSalt = b.get(2); reader.close(); mFoundPWFile = true; } catch(Exception e){ Log.d(TAG, " (PM) Exception: " + e); mFoundPWFile = false; mHint = null; mPasswordHash = null; mSalt = null; } finally { if (reader != null){ try{ reader.close(); } catch(Exception e){ Log.d(TAG, " (PM) Exception: " + e); } } } } public static PasswordManager get(Context c){ if (sPasswordManager == null){ sPasswordManager = new PasswordManager(c.getApplicationContext()); } return sPasswordManager; } public void clearPassword(){ mPassword = null; } public String getPassword(){ return mPassword; } public String getHint(){ String s = new String(mHint); return s; } private byte[] hashPassword(String p){ byte[] hashedP = null; try{ MessageDigest digest = MessageDigest.getInstance("SHA-1"); digest.reset(); digest.update(mSalt); byte[] input = digest.digest(p.getBytes("UTF-8")); digest.reset(); hashedP = digest.digest(input); } catch (Exception e){ Log.d(TAG, " (HP) Exception: " + e); } return hashedP; } public boolean havePassword(){ if (mPassword == null){ return false; } else{ return true; } } public boolean havePasswordFile(){ return mFoundPWFile; } public boolean isPassword(String p){ byte[] hp = hashPassword(p); if (hp != null){ if (Arrays.equals(hp, mPasswordHash)){ return true; } else { return false; } } else{ return false; } } public void savePassword(String password){ savePassword(password, getHint()); } public void savePassword(String password, String hint){ Log.i(TAG, "savePassword," + password + "," + hint); /* Hash password first. */ try{ mPassword = password; SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); mSalt = new byte[8]; random.nextBytes(mSalt); mPasswordHash = hashPassword(password); mHint = hint.getBytes("UTF-8"); Log.i(TAG, mSalt + "," + mPasswordHash + "," + mHint); } catch (Exception e){ Log.d(TAG, " (SP) Exception: " + e); } /* Save hashed password, salt and hint. */ ObjectOutputStream writer = null; try { OutputStream out = mContext.openFileOutput(FILENAME, Context.MODE_PRIVATE); writer = new ObjectOutputStream(out); List<byte[]> l = new ArrayList<byte[]>(); l.add(mHint); l.add(mPasswordHash); l.add(mSalt); writer.writeObject(l); } catch (IOException e){ Log.d(TAG, " (SP) Exception: " + e); } finally { if (writer != null){ try{ writer.close(); } catch(Exception e){ Log.d(TAG, " (SP) Exception: " + e); } } } } }