root / src / com / rackspacecloud / android / PasswordManager.java @ c476482b
History | View | Annotate | Download (2.1 kB)
1 |
package com.rackspacecloud.android; |
---|---|
2 |
|
3 |
import java.security.MessageDigest; |
4 |
import java.security.NoSuchAlgorithmException; |
5 |
import java.util.Arrays; |
6 |
|
7 |
import android.content.SharedPreferences; |
8 |
|
9 |
public class PasswordManager { |
10 |
|
11 |
private SharedPreferences settings;
|
12 |
|
13 |
public PasswordManager(SharedPreferences sp){
|
14 |
settings = sp; |
15 |
} |
16 |
|
17 |
/*
|
18 |
* checks the parameter string against the stored
|
19 |
* password
|
20 |
*/
|
21 |
public boolean verifyEnteredPassword(String password) { |
22 |
return Arrays.toString(getHash(password)).equals(getStoredPassword()); |
23 |
} |
24 |
|
25 |
/*
|
26 |
* return the hash of the password that is stored in
|
27 |
* shared preferences
|
28 |
*/
|
29 |
private String getStoredPassword(){ |
30 |
return settings.getString(Preferences.PREF_KEY_PASSCODE_HASH, ""); |
31 |
} |
32 |
|
33 |
/*
|
34 |
* turns off password requirement
|
35 |
*/
|
36 |
public void turnOffPassword() { |
37 |
SharedPreferences.Editor editor = settings.edit(); |
38 |
editor.putString(Preferences.PREF_KEY_PASSCODE_HASH, ""); |
39 |
editor.putBoolean(Preferences.PREF_KEY_PASSWORD_LOCK, false); |
40 |
editor.commit(); |
41 |
} |
42 |
|
43 |
/*
|
44 |
* submits a password change into memory
|
45 |
* stores the sha-256 hash of the password
|
46 |
*/
|
47 |
private void storeNewPassword(String hashedPassword) { |
48 |
SharedPreferences.Editor editor = settings.edit(); |
49 |
editor.putString(Preferences.PREF_KEY_PASSCODE_HASH, hashedPassword);
|
50 |
editor.putBoolean(Preferences.PREF_KEY_PASSWORD_LOCK, true); |
51 |
editor.commit(); |
52 |
} |
53 |
|
54 |
/*
|
55 |
* changes the password for
|
56 |
*/
|
57 |
public void changePassword(String password) { |
58 |
storeNewPassword(Arrays.toString(getHash(password)));
|
59 |
} |
60 |
|
61 |
/*
|
62 |
* returns the sha-256 hash for a given
|
63 |
* string
|
64 |
*/
|
65 |
private byte[] getHash(String password) { |
66 |
MessageDigest m = null; |
67 |
try {
|
68 |
m = MessageDigest.getInstance("SHA-256"); |
69 |
m.update(password.getBytes()); |
70 |
} catch (NoSuchAlgorithmException e) { |
71 |
// TODO Auto-generated catch block
|
72 |
e.printStackTrace(); |
73 |
} catch (NullPointerException e) { |
74 |
// TODO Auto-generated catch block
|
75 |
e.printStackTrace(); |
76 |
} |
77 |
return m.digest();
|
78 |
} |
79 |
|
80 |
/*
|
81 |
* returns true if their is a password requirement
|
82 |
*/
|
83 |
public boolean hasPassword() { |
84 |
return settings.getBoolean(Preferences.PREF_KEY_PASSWORD_LOCK, false); |
85 |
} |
86 |
|
87 |
} |