-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix(messaging): missing chatId attribute sending localId of the message * feature(chatting): encryption * correctly encrypt and decrypt --------- Co-authored-by: Marwan Alhameedy <[email protected]>
- Loading branch information
1 parent
ad3d2b0
commit 3e50099
Showing
10 changed files
with
236 additions
and
82 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
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
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,66 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:telware_cross_platform/core/models/chat_model.dart'; | ||
import 'package:telware_cross_platform/features/chat/enum/chatting_enums.dart'; | ||
import 'package:encrypt/encrypt.dart'; | ||
|
||
class EncryptionService { | ||
// Private constructor | ||
EncryptionService._internal(); | ||
|
||
// Singleton instance | ||
static EncryptionService? _instance; | ||
|
||
// Getter for the singleton instance | ||
static EncryptionService get instance { | ||
return _instance ??= EncryptionService._internal(); | ||
} | ||
|
||
String encrypt({ | ||
required String msg, | ||
required String? encryptionKey, | ||
required String? initializationVector, | ||
required ChatType chatType, | ||
}) { | ||
if (chatType != ChatType.private || | ||
encryptionKey == null || | ||
initializationVector == null) { | ||
print('(((((((((('); | ||
print(chatType.type); | ||
print(encryptionKey); | ||
print(initializationVector); | ||
print('))))))))))'); | ||
return msg; | ||
} | ||
|
||
final key = Key.fromBase16(encryptionKey); | ||
final iv = IV.fromBase16(initializationVector); | ||
final encrypter = Encrypter(AES(key, mode: AESMode.cbc)); | ||
|
||
final encrypted = encrypter.encrypt(msg, iv: iv); | ||
|
||
return encrypted.base16; | ||
} | ||
|
||
String decrypt({ | ||
required String msg, | ||
required String? encryptionKey, | ||
required String? initializationVector, | ||
required ChatType chatType, | ||
}) { | ||
if (chatType != ChatType.private || | ||
encryptionKey == null || | ||
initializationVector == null) { | ||
return msg; | ||
} | ||
|
||
final key = Key.fromBase16(encryptionKey); | ||
final iv = IV.fromBase16(initializationVector); | ||
final encrypter = Encrypter(AES(key, mode: AESMode.cbc)); | ||
|
||
final decryptedBytes = | ||
encrypter.decryptBytes(Encrypted.fromBase16(msg), iv: iv); | ||
|
||
return utf8.decode(decryptedBytes); | ||
} | ||
} |
Oops, something went wrong.