12
12
import java .security .NoSuchProviderException ;
13
13
import java .security .SecureRandom ;
14
14
import java .util .ArrayList ;
15
- import java .util .HashMap ;
16
15
17
16
/**
18
17
* Created by Werfish on 09.04.2017.
@@ -28,6 +27,7 @@ public class UsersDBHelper extends SQLiteOpenHelper {
28
27
public static final String USERS_COLUMN_PASSWORD = "Password" ;
29
28
public static final String USERS_COLUMN_AFTER_SALT = "Password_Salt" ;
30
29
public static final String USERS_COLUMN_SALT = "Salt" ;
30
+ public static final String USERS_COLUMN_ENCRYPTED_NOSALT = "Encrypted_NoSalt" ;
31
31
public static final String USERS_COLUMN_ENCRYPTED_PASS = "Encrypted_Pass" ;
32
32
33
33
@@ -39,7 +39,7 @@ public UsersDBHelper(Context context) {
39
39
public void onCreate (SQLiteDatabase db ) {
40
40
// TODO Auto-generated method stub
41
41
db .execSQL (
42
- "CREATE TABLE IF NOT EXISTS Users (User_ID Integer Primary Key,Name VARCHAR, Email VARCHAR, Phone VARCHAR, Password VARCHAR, Encrypted_Pass VARCHAR);" );
42
+ "CREATE TABLE IF NOT EXISTS Users (User_ID Integer Primary Key,Name VARCHAR, Email VARCHAR, Phone VARCHAR, Password VARCHAR, Password_Salt VARCHAR, Salt VARCHAR, Encrypted_NoSalt VARCHAR, Encrypted_Pass VARCHAR);" );
43
43
}
44
44
45
45
@ Override
@@ -56,7 +56,16 @@ public boolean insertUser(String name, String email, String phone, String passwo
56
56
contentValues .put ("email" , email );
57
57
contentValues .put ("phone" , phone );
58
58
contentValues .put ("password" , password );
59
- contentValues .put ("encrypted_pass" , encryptPass (password ));
59
+
60
+ //Before putting oother values lets create the salt
61
+ byte [] salt = getSalt ();
62
+
63
+ contentValues .put ("password_salt" , salt .toString () + password );
64
+ contentValues .put ("salt" , salt );
65
+ //Enrypting with just SHA-256
66
+ contentValues .put ("encrypted_noSalt" , encryptPass (password ));
67
+ //Adding the salt to the SHA-256
68
+ contentValues .put ("encrypted_pass" , encryptPassWithSalt (password ,salt ));
60
69
db .insert ("Users" , null , contentValues );
61
70
return true ;
62
71
}
@@ -167,17 +176,19 @@ private String encryptPassWithSalt(String pass, byte[] salt){
167
176
}
168
177
169
178
//Add salt
170
- private byte [] getSalt () throws NoSuchAlgorithmException , NoSuchProviderException
171
- {
172
- //Always use a SecureRandom generator
173
- SecureRandom sr = SecureRandom .getInstance ("SHA1PRNG" );
179
+ private byte [] getSalt () {
174
180
//Create array for salt
175
181
byte [] salt = new byte [32 ];
176
- //Get a random salt
177
- sr .nextBytes (salt );
178
- //return salt
182
+ try {
183
+ //Always use a SecureRandom generator
184
+ SecureRandom sr = SecureRandom .getInstance ("SHA1PRNG" );
185
+ //Get a random salt
186
+ sr .nextBytes (salt );
187
+ //return salt
188
+ }catch (Exception e1 ){
189
+ e1 .printStackTrace ();
190
+ }
179
191
return salt ;
180
192
}
181
193
}
182
- }
183
194
0 commit comments