@@ -3,7 +3,6 @@ import 'package:flutter_secure_storage/flutter_secure_storage.dart';
3
3
import 'package:flutter_string_encryption/flutter_string_encryption.dart' ;
4
4
import 'package:shared_preferences/shared_preferences.dart' ;
5
5
import 'package:crypt/crypt.dart' ;
6
- import 'dart:math' ;
7
6
8
7
// TODO: animated feedback when decrypting (spinning loader or something)
9
8
@@ -28,14 +27,14 @@ class _LoginPageState extends State<LoginPage> {
28
27
29
28
final TextEditingController textEditingController = new TextEditingController ();
30
29
31
- Set <String > _noteIDs = new Set ();
30
+ // Set<String> _noteIDs = new Set();
32
31
33
32
@override
34
33
initState () {
35
34
debugPrint ("LoginPage" );
36
35
super .initState ();
37
36
_checkIfFirstTime ();
38
- _loadIDsFromMemory (); // TODO remove?
37
+ // _loadIDsFromMemory(); // TODO remove?
39
38
}
40
39
41
40
void _checkIfFirstTime () async {
@@ -55,10 +54,11 @@ class _LoginPageState extends State<LoginPage> {
55
54
}
56
55
}
57
56
58
- void _loadIDsFromMemory () async {
59
- SharedPreferences prefs = await SharedPreferences .getInstance ();
60
- setState ( () => _noteIDs = new Set .from (prefs.getStringList ("noteIDs" )) ?? new Set ());
61
- }
57
+ // void _loadIDsFromMemory() async {
58
+ // SharedPreferences prefs = await SharedPreferences.getInstance();
59
+ // setState( () => _noteIDs = new Set.from(prefs.getStringList("noteIDs")) ?? new Set());
60
+ // debugPrint(_noteIDs.toString());
61
+ // }
62
62
63
63
@override
64
64
Widget build (BuildContext context) {
@@ -125,74 +125,96 @@ class _LoginPageState extends State<LoginPage> {
125
125
126
126
void _performLogin () async {
127
127
// USER FEEDBACK: WE'RE DOING STUFF FOR YOU
128
- setState ((){
128
+ debugPrint ("lala" );
129
+
130
+ setState (() {
129
131
_keyIconColor = Colors .amber;
130
132
_errorMessage = "\n Verifying password. Please wait." ;
131
- } );
133
+ });
132
134
133
- debugPrint ("OK!!!!!!!!!!" );
135
+ authenticate ();
136
+ }
134
137
135
- // === AUTHENTICATION FIRST ===
136
- String _passwordHash;
138
+ void authenticate () async {
139
+ // === AUTHENTICATION FIRST ===
140
+ // If the user can authenticate, the same password will be used
141
+ // To decrypt the note titles, and eventually the notes themselves
142
+ // The password will be pushed to the note editor.
137
143
138
- try { // fetch password hash from secure memory
139
- _passwordHash = await _secureStorage.read (key: "passwordHash" );
140
- }
141
- catch (e) { // don't have a password yet -> make one
142
- setState (() {
143
- _errorMessage = "\n Securely storing new password. Please wait." ;
144
- });
145
- Crypt newHashMachine = new Crypt .sha256 (_userSuppliedPassword); // randomly salted (handled by Crypt)
146
- _passwordHash = newHashMachine.toString ();
147
- await _secureStorage.write (key: "passwordHash" , value: _passwordHash); // store password hash
148
- }
149
- if (_passwordHash == null ) { // duplicate code sucks :(
150
- setState (() {
151
- _errorMessage = "\n Securely storing new password. Please wait." ;
152
- });
153
- Crypt newHashMachine = new Crypt .sha256 (_userSuppliedPassword); // randomly salted (handled by Crypt)
154
- _passwordHash = newHashMachine.toString ();
155
- await _secureStorage.write (key: "passwordHash" , value: _passwordHash); // store password hash
156
- }
144
+ String _passwordHash;
157
145
158
- Crypt hashMachine = new Crypt (_passwordHash);
159
- if (! hashMachine.match (_userSuppliedPassword)) { // Wrong password
160
- debugPrint (":( oo" );
161
- setState ((){
162
- _keyIconColor = Colors .red;
163
- _errorMessage = "\n Wrong password. Please retry." ;
164
- });
146
+ debugPrint ("entertainers" );
147
+
148
+ void onHashFetchFail () async {
149
+ // first declare inner function
150
+ setState (() {
151
+ _errorMessage = "\n Securely storing new password. Please wait." ;
152
+ });
153
+ Crypt newHashMachine = new Crypt .sha256 (
154
+ _userSuppliedPassword); // randomly salted (handled by Crypt)
155
+ _passwordHash = newHashMachine.toString ();
156
+ await _secureStorage.write (
157
+ key: "passwordHash" , value: _passwordHash); // store password hash
158
+ }
159
+ try { // fetch password hash from secure memory
160
+ _passwordHash = await _secureStorage.read (key: "passwordHash" );
161
+ }
162
+ catch (e) { // don't have a password yet -> make one
163
+ onHashFetchFail ();
164
+ }
165
+ if (_passwordHash == null ) {
166
+ onHashFetchFail ();
167
+ }
168
+
169
+ Crypt hashMachine = new Crypt (_passwordHash);
170
+ if (! hashMachine.match (_userSuppliedPassword)) { // Wrong password
171
+ setState (() {
172
+ _keyIconColor = Colors .red;
173
+ _errorMessage = "\n Wrong password. Please retry." ;
174
+ });
175
+ }
176
+ else { // Correct password
177
+ setState (() {
178
+ _keyIconColor = Colors .greenAccent;
179
+ _errorMessage = "\n Correct password. Decrypting. Please wait." ;
180
+ });
181
+ // === LOGIN SUCCESSFUL -> DECRYPT NOTE TITLES ===
182
+ decryptNoteTitles ();
183
+ }
184
+
185
+ debugPrint ("hurroo" );
165
186
}
166
- else { // Correct password
167
- setState ((){
168
- _keyIconColor = Colors .greenAccent;
169
- _errorMessage = "\n Correct password. Decrypting. Please wait." ;
170
- });
171
- // === LOGIN SUCCESFUL -> DECRYPT NOTE TITLES ===
187
+
188
+ void decryptNoteTitles () async {
172
189
final PlatformStringCryptor cryptor = new PlatformStringCryptor ();
173
190
debugPrint ("crrct" );
174
191
192
+ // _saltForNotes fetching or generating
175
193
String _saltForNotes;
194
+ void onSaltFetchFail () async {
195
+ _saltForNotes = await cryptor.generateSalt ();
196
+ await _secureStorage.write (key: "saltForNotes" , value: _saltForNotes);
197
+ }
176
198
try { // fetch salt from secure memory
177
199
debugPrint ("here" );
178
200
_saltForNotes = await _secureStorage.read (key: "saltForNotes" );
179
201
}
180
202
catch (e) { // don't have a salt yet -> make one
181
203
debugPrint ("never" );
182
- _saltForNotes = await cryptor.generateSalt ();
183
- await _secureStorage.write (key: "saltForNotes" , value: _saltForNotes);
204
+ onSaltFetchFail ();
184
205
}
185
206
if (_saltForNotes == null ) {
186
207
debugPrint ("here!" );
187
- _saltForNotes = _randomString (16 ); // 16 characters (should be bytes) salt
188
- await _secureStorage.write (key: "saltForNotes" , value: _saltForNotes);
208
+ onSaltFetchFail ();
189
209
}
210
+ debugPrint ("nice" );
190
211
191
- final String _key = await cryptor.generateKeyFromPassword ("p" , _saltForNotes); // TODO p
192
-
193
- final String string = "Note titles fetched from memory: title 1: title 2: title 3" ; // TODO validate titles (no ": ")
212
+ final String _key = await cryptor.generateKeyFromPassword ("p" , _saltForNotes);
213
+ debugPrint ( "nice!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" );
214
+ final String string = "Note titles fetched from memory\$ title 1\$ title 2\$ title 3" ; // TODO validate titles (no "$ ")
194
215
final String encrypted = await cryptor.encrypt (string, _key);
195
- String _userSuppliedKey = await cryptor.generateKeyFromPassword (_userSuppliedPassword, _saltForNotes);
216
+ //String _userSuppliedKey = await cryptor.generateKeyFromPassword(_userSuppliedPassword, _saltForNotes);
217
+ final String _userSuppliedKey = await cryptor.generateKeyFromPassword (_userSuppliedPassword, _saltForNotes);
196
218
String _noteTitlesDecrypted;
197
219
try {
198
220
_noteTitlesDecrypted = await cryptor.decrypt (encrypted, _userSuppliedKey);
@@ -207,29 +229,13 @@ class _LoginPageState extends State<LoginPage> {
207
229
if (_noteTitlesDecrypted != null ) {
208
230
// doesn't matter if hacker sets this to non-null somehow, values aren't decrypted in that case :)
209
231
Navigator .of (context).pushNamed ('/homePage/$_key /$_noteTitlesDecrypted ' ); // TODO: slash probably not best option
210
- // TODO: use question mark or somethin :)
232
+ // TODO: use question mark or somethin :) convert to base64?
211
233
setState (() { _errorMessage = "\n " ;});
212
234
// TODO pass decrypted note titles to homepage
213
235
}
214
236
215
237
}
216
238
217
- debugPrint ("hurroo" );
218
-
219
- }
220
-
221
- String _randomString (int length) {
222
- var rand = new Random .secure ();
223
- var codeUnits = new List .generate (
224
- length,
225
- (index){
226
- return rand.nextInt (33 )+ 89 ;
227
- }
228
- );
229
-
230
- return new String .fromCharCodes (codeUnits);
231
- }
232
-
233
239
234
240
235
241
}
0 commit comments