Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.2 KiB

import 'dart:convert';
import 'package:pointycastle/export.dart';
import '/utils/encryption/crypto_utils.dart';
import '/utils/encryption/aes_helper.dart';
import '/utils/storage/database.dart';
Conversation findConversationByDetailId(List<Conversation> conversations, String id) {
for (var conversation in conversations) {
if (conversation.conversationDetailId == id) {
return conversation;
}
}
// Or return `null`.
throw ArgumentError.value(id, "id", "No element with that id");
}
class Conversation {
String id;
String userId;
String conversationDetailId;
String messageThreadKey;
String symmetricKey;
bool admin;
String name;
String? users;
Conversation({
required this.id,
required this.userId,
required this.conversationDetailId,
required this.messageThreadKey,
required this.symmetricKey,
required this.admin,
required this.name,
this.users,
});
factory Conversation.fromJson(Map<String, dynamic> json, RSAPrivateKey privKey) {
var symmetricKeyDecrypted = CryptoUtils.rsaDecrypt(
base64.decode(json['symmetric_key']),
privKey,
);
var detailId = AesHelper.aesDecrypt(
symmetricKeyDecrypted,
base64.decode(json['conversation_detail_id']),
);
var threadKey = AesHelper.aesDecrypt(
symmetricKeyDecrypted,
base64.decode(json['message_thread_key']),
);
var admin = AesHelper.aesDecrypt(
symmetricKeyDecrypted,
base64.decode(json['admin']),
);
return Conversation(
id: json['id'],
userId: json['user_id'],
conversationDetailId: detailId,
messageThreadKey: threadKey,
symmetricKey: base64.encode(symmetricKeyDecrypted),
admin: admin == 'true',
name: 'Unknown',
);
}
@override
String toString() {
return '''
id: $id
userId: $userId
name: $name
admin: $admin''';
}
Map<String, dynamic> toMap() {
return {
'id': id,
'user_id': userId,
'conversation_detail_id': conversationDetailId,
'message_thread_key': messageThreadKey,
'symmetric_key': symmetricKey,
'admin': admin ? 1 : 0,
'name': name,
'users': users,
};
}
}
// A method that retrieves all the dogs from the dogs table.
Future<List<Conversation>> getConversations() async {
final db = await getDatabaseConnection();
final List<Map<String, dynamic>> maps = await db.query('conversations');
return List.generate(maps.length, (i) {
return Conversation(
id: maps[i]['id'],
userId: maps[i]['user_id'],
conversationDetailId: maps[i]['conversation_detail_id'],
messageThreadKey: maps[i]['message_thread_key'],
symmetricKey: maps[i]['symmetric_key'],
admin: maps[i]['admin'] == 1,
name: maps[i]['name'],
users: maps[i]['users'],
);
});
}