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.

104 lines
3.1 KiB

import 'dart:convert';
import "package:pointycastle/export.dart";
import '/utils/encryption/crypto_utils.dart';
import '/utils/storage/database.dart';
Friend findFriendByFriendId(List<Friend> friends, String id) {
for (var friend in friends) {
if (friend.friendIdDecrypted == id) {
return friend;
}
}
// Or return `null`.
throw ArgumentError.value(id, "id", "No element with that id");
}
class Friend{
String id;
String userId;
String? username;
String friendId;
String friendIdDecrypted;
String friendSymmetricKey;
String friendSymmetricKeyDecrypted;
String acceptedAt;
Friend({
required this.id,
required this.userId,
required this.friendId,
required this.friendIdDecrypted,
required this.friendSymmetricKey,
required this.friendSymmetricKeyDecrypted,
required this.acceptedAt,
this.username
});
factory Friend.fromJson(Map<String, dynamic> json, RSAPrivateKey privKey) {
// TODO: Remove encrypted entries
var friendIdDecrypted = CryptoUtils.rsaDecrypt(
base64.decode(json['friend_id']),
privKey,
);
var friendSymmetricKeyDecrypted = CryptoUtils.rsaDecrypt(
base64.decode(json['symmetric_key']),
privKey,
);
return Friend(
id: json['id'],
userId: json['user_id'],
friendId: json['friend_id'],
friendIdDecrypted: String.fromCharCodes(friendIdDecrypted),
friendSymmetricKey: json['symmetric_key'],
friendSymmetricKeyDecrypted: base64.encode(friendSymmetricKeyDecrypted),
acceptedAt: json['accepted_at'],
);
}
@override
String toString() {
return '''
id: $id
userId: $userId
username: $username
friendIdDecrypted: $friendIdDecrypted
accepted_at: $acceptedAt''';
}
Map<String, dynamic> toMap() {
return {
'id': id,
'user_id': userId,
'username': username,
'friend_id': friendId,
'friend_id_decrypted': friendIdDecrypted,
'symmetric_key': friendSymmetricKey,
'symmetric_key_decrypted': friendSymmetricKeyDecrypted,
'accepted_at': acceptedAt,
};
}
}
// A method that retrieves all the dogs from the dogs table.
Future<List<Friend>> getFriends() async {
final db = await getDatabaseConnection();
final List<Map<String, dynamic>> maps = await db.query('friends');
return List.generate(maps.length, (i) {
return Friend(
id: maps[i]['id'],
userId: maps[i]['user_id'],
friendId: maps[i]['friend_id'],
friendIdDecrypted: maps[i]['friend_id_decrypted'],
friendSymmetricKey: maps[i]['symmetric_key'],
friendSymmetricKeyDecrypted: maps[i]['symmetric_key_decrypted'],
acceptedAt: maps[i]['accepted_at'],
username: maps[i]['username'],
);
});
}