-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1194c4b
commit 9350711
Showing
5 changed files
with
149 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,16 @@ | ||
class DBConstants { | ||
DBConstants._(); | ||
// Store Name | ||
static const String STORE_NAME = 'homebox'; | ||
static const String USER_INFO_STORE = 'userInfo'; | ||
static const String EVENT_HISTORY = 'eventHistory'; | ||
static const String FIXEDCODE_STORE = 'fixcode'; | ||
static const String NOTIFICATION_STORE = 'notification'; | ||
static const String STATUS_STORE = 'status'; | ||
static const String ACCESS_STORE = 'access'; | ||
static const String HISTORY_STORE = 'history'; | ||
static const String LOGS_STORE = 'logs'; | ||
|
||
|
||
// DB Name | ||
static const DB_NAME = 'homebox.db'; | ||
static const DB_NAME = 'technologies.db'; | ||
|
||
// Fields | ||
static const FIELD_ID = 'id'; | ||
} | ||
} | ||
|
||
// local db | ||
//view model and provider stuff | ||
// UI and widgets and designs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import 'dart:async'; | ||
import 'package:path/path.dart'; | ||
import 'package:path_provider/path_provider.dart'; | ||
import 'package:sembast/sembast.dart'; | ||
import 'package:sembast/sembast_io.dart'; | ||
import 'package:technologies_login_example/data/local/constants.dart'; | ||
import 'package:technologies_login_example/utils/encryption/xxtea.dart'; | ||
|
||
import 'netwok_module.dart'; | ||
|
||
abstract class LocalModule extends NetworkModule { | ||
/// | ||
/// Calling it multiple times will return the same instance. | ||
static Future<Database> provideDatabase() async { | ||
// Key for encryption | ||
var encryptionKey = ""; | ||
|
||
// Get a platform-specific directory where persistent app data can be stored | ||
final appDocumentDir = await getApplicationDocumentsDirectory(); | ||
|
||
// Path with the form: /platform-specific-directory/demo.db | ||
final dbPath = join(appDocumentDir.path, DBConstants.DB_NAME); | ||
|
||
// Check to see if encryption is set, then provide codec | ||
// else init normal db with path | ||
Database database; | ||
if (encryptionKey.isNotEmpty) { | ||
// Initialize the encryption codec with a user password | ||
var codec = getXXTeaCodec(password: encryptionKey); | ||
database = await databaseFactoryIo.openDatabase(dbPath, codec: codec); | ||
} else { | ||
database = await databaseFactoryIo.openDatabase(dbPath); | ||
} | ||
|
||
// Return database instance | ||
return database; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:sembast/sembast.dart'; | ||
import 'package:xxtea/xxtea.dart'; | ||
|
||
class _XXTeaEncoder extends Converter<Map<String, dynamic>, String> { | ||
final String key; | ||
|
||
_XXTeaEncoder(this.key); | ||
|
||
@override | ||
String convert(Map<String, dynamic> input) => | ||
xxtea.encryptToString(json.encode(input), key)!; | ||
} | ||
|
||
class _XXTeaDecoder extends Converter<String, Map<String, dynamic>> { | ||
final String key; | ||
|
||
_XXTeaDecoder(this.key); | ||
|
||
@override | ||
Map<String, dynamic> convert(String input) { | ||
var result = json.decode(xxtea.decryptToString(input, key)!); | ||
if (result is Map) { | ||
return result.cast<String, dynamic>(); | ||
} | ||
throw FormatException('invalid input $input'); | ||
} | ||
} | ||
|
||
/// Simple encryption codec using xxtea | ||
/// It requires a password to encrypt/decrypt the data | ||
class _XXTeaCodec extends Codec<Map<String, dynamic>, String> { | ||
late _XXTeaEncoder _encoder; | ||
late _XXTeaDecoder _decoder; | ||
|
||
/// A non null [password] to use for the encryption/decryption | ||
_XXTeaCodec(String password) { | ||
_encoder = _XXTeaEncoder(password); | ||
_decoder = _XXTeaDecoder(password); | ||
} | ||
|
||
@override | ||
Converter<String, Map<String, dynamic>> get decoder => _decoder; | ||
|
||
@override | ||
Converter<Map<String, dynamic>, String> get encoder => _encoder; | ||
} | ||
|
||
/// Create a codec to use when opening an encrypted sembast database | ||
/// | ||
/// The usage is then | ||
/// | ||
/// ```dart | ||
/// // Initialize the encryption codec with a user password | ||
/// var codec = getXXTeaCodec(password: '[your_user_password]'); | ||
/// // Open the database with the codec | ||
/// Database db = await factory.openDatabase(dbPath, codec: codec); | ||
/// | ||
/// // ...your database is ready to use as encrypted | ||
/// ``` | ||
SembastCodec getXXTeaCodec({required String password}) => | ||
SembastCodec(signature: 'xxtea', codec: _XXTeaCodec(password)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters