diff --git a/lib/models/user.dart b/lib/models/user.dart index 17da87c9..b872f240 100644 --- a/lib/models/user.dart +++ b/lib/models/user.dart @@ -4,6 +4,7 @@ import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:test_sa/controllers/notification/firebase_notification_manger.dart'; import 'package:test_sa/models/enums/user_types.dart'; +import 'package:test_sa/new_views/pages/AesHelper.dart'; class User { int? clientId; @@ -92,10 +93,10 @@ class User { Future> toLoginJson() async { String notificationType = (Platform.isAndroid && !(await FirebaseNotificationManger.isGoogleServicesAvailable())) ? "HMC" : "FCM"; if (FirebaseNotificationManger.token == null) { - //calling this agin to check for token as sometimes token is null fresh install.. await FirebaseNotificationManger.getToken(); } - return {"username": userName, "password": password, "fireBaseToken": FirebaseNotificationManger?.token ?? "", "notificationType": notificationType}; + final encryptedPassword = AesHelper.encryptText(password!); + return {"username": userName, "password": encryptedPassword, "fireBaseToken": FirebaseNotificationManger?.token ?? "", "notificationType": notificationType}; } UsersTypes? get type { diff --git a/lib/new_views/pages/AesHelper.dart b/lib/new_views/pages/AesHelper.dart new file mode 100644 index 00000000..a011b7b2 --- /dev/null +++ b/lib/new_views/pages/AesHelper.dart @@ -0,0 +1,42 @@ +import 'dart:convert'; +import 'dart:typed_data'; +import 'package:crypto/crypto.dart'; +import 'package:encrypt/encrypt.dart' as encrypt; + +class AesHelper { + static String secretKey = "BRIL7VWSHDSY5G2SEVTI4SNAUEE66B3E"; + + static Uint8List _deriveKey(String key) { + final keyBytes = utf8.encode(key); + final hash = sha256.convert(keyBytes); + return Uint8List.fromList(hash.bytes); // 32 bytes + } + + static String encryptText(String plainText) { + if (plainText.isEmpty) { + throw ArgumentError('plainText cannot be null or empty'); + } + + final keyBytes = _deriveKey(secretKey); + final key = encrypt.Key(keyBytes); + final iv = encrypt.IV.fromSecureRandom(16); + final encrypter = encrypt.Encrypter(encrypt.AES(key, mode: encrypt.AESMode.cbc, padding: 'PKCS7')); + final encrypted = encrypter.encryptBytes(utf8.encode(plainText), iv: iv); + final combined = Uint8List.fromList(iv.bytes + encrypted.bytes); + + return base64.encode(combined); + } + + static String decryptText(String cipherTextBase64) { + final keyBytes = _deriveKey(secretKey); + final key = encrypt.Key(keyBytes); + final fullBytes = base64.decode(cipherTextBase64); + final ivBytes = fullBytes.sublist(0, 16); + final cipherBytes = fullBytes.sublist(16); + final iv = encrypt.IV(Uint8List.fromList(ivBytes)); + final encrypter = encrypt.Encrypter(encrypt.AES(key, mode: encrypt.AESMode.cbc, padding: 'PKCS7')); + final decrypted = encrypter.decryptBytes(encrypt.Encrypted(Uint8List.fromList(cipherBytes)), iv: iv); + + return utf8.decode(decrypted); + } +} diff --git a/pubspec.lock b/pubspec.lock index 937cec84..558355c6 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -33,6 +33,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.7.0" + asn1lib: + dependency: transitive + description: + name: asn1lib + sha256: "9a8f69025044eb466b9b60ef3bc3ac99b4dc6c158ae9c56d25eeccf5bc56d024" + url: "https://pub.dev" + source: hosted + version: "1.6.5" async: dependency: transitive description: @@ -209,6 +217,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.19.1" + convert: + dependency: transitive + description: + name: convert + sha256: b30acd5944035672bc15c6b7a8b47d773e41e2f17de064350988c5d02adb1c68 + url: "https://pub.dev" + source: hosted + version: "3.1.2" cross_file: dependency: transitive description: @@ -218,7 +234,7 @@ packages: source: hosted version: "0.3.5+2" crypto: - dependency: transitive + dependency: "direct main" description: name: crypto sha256: c8ea0233063ba03258fbcf2ca4d6dadfefe14f02fab57702265467a19f27fadf @@ -273,6 +289,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.0" + encrypt: + dependency: "direct main" + description: + name: encrypt + sha256: "62d9aa4670cc2a8798bab89b39fc71b6dfbacf615de6cf5001fb39f7e4a996a2" + url: "https://pub.dev" + source: hosted + version: "5.0.3" fake_async: dependency: transitive description: @@ -854,6 +878,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.20.2" + js: + dependency: transitive + description: + name: js + sha256: "53385261521cc4a0c4658fd0ad07a7d14591cf8fc33abbceae306ddb974888dc" + url: "https://pub.dev" + source: hosted + version: "0.7.2" json_annotation: dependency: transitive description: @@ -1366,6 +1398,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.10.3" + pointycastle: + dependency: transitive + description: + name: pointycastle + sha256: "4be0097fcf3fd3e8449e53730c631200ebc7b88016acecab2b0da2f0149222fe" + url: "https://pub.dev" + source: hosted + version: "3.9.1" pool: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index a32ffe54..5dff9222 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -88,6 +88,8 @@ dependencies: flutter_timezone: ^3.0.1 device_calendar: ^4.3.3 pie_chart: ^5.4.0 + crypto: ^3.0.7 + encrypt: ^5.0.3 badges: ^3.1.1 # buttons_tabbar: ^1.1.2