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

  1. import 'dart:convert';
  2. import "package:pointycastle/export.dart";
  3. import '/utils/encryption/crypto_utils.dart';
  4. import '/utils/storage/database.dart';
  5. Friend findFriendByFriendId(List<Friend> friends, String id) {
  6. for (var friend in friends) {
  7. if (friend.friendIdDecrypted == id) {
  8. return friend;
  9. }
  10. }
  11. // Or return `null`.
  12. throw ArgumentError.value(id, "id", "No element with that id");
  13. }
  14. class Friend{
  15. String id;
  16. String userId;
  17. String? username;
  18. String friendId;
  19. String friendIdDecrypted;
  20. String friendSymmetricKey;
  21. String friendSymmetricKeyDecrypted;
  22. String acceptedAt;
  23. Friend({
  24. required this.id,
  25. required this.userId,
  26. required this.friendId,
  27. required this.friendIdDecrypted,
  28. required this.friendSymmetricKey,
  29. required this.friendSymmetricKeyDecrypted,
  30. required this.acceptedAt,
  31. this.username
  32. });
  33. factory Friend.fromJson(Map<String, dynamic> json, RSAPrivateKey privKey) {
  34. // TODO: Remove encrypted entries
  35. var friendIdDecrypted = CryptoUtils.rsaDecrypt(
  36. base64.decode(json['friend_id']),
  37. privKey,
  38. );
  39. var friendSymmetricKeyDecrypted = CryptoUtils.rsaDecrypt(
  40. base64.decode(json['symmetric_key']),
  41. privKey,
  42. );
  43. return Friend(
  44. id: json['id'],
  45. userId: json['user_id'],
  46. friendId: json['friend_id'],
  47. friendIdDecrypted: String.fromCharCodes(friendIdDecrypted),
  48. friendSymmetricKey: json['symmetric_key'],
  49. friendSymmetricKeyDecrypted: base64.encode(friendSymmetricKeyDecrypted),
  50. acceptedAt: json['accepted_at'],
  51. );
  52. }
  53. @override
  54. String toString() {
  55. return '''
  56. id: $id
  57. userId: $userId
  58. username: $username
  59. friendIdDecrypted: $friendIdDecrypted
  60. accepted_at: $acceptedAt''';
  61. }
  62. Map<String, dynamic> toMap() {
  63. return {
  64. 'id': id,
  65. 'user_id': userId,
  66. 'username': username,
  67. 'friend_id': friendId,
  68. 'friend_id_decrypted': friendIdDecrypted,
  69. 'symmetric_key': friendSymmetricKey,
  70. 'symmetric_key_decrypted': friendSymmetricKeyDecrypted,
  71. 'accepted_at': acceptedAt,
  72. };
  73. }
  74. }
  75. // A method that retrieves all the dogs from the dogs table.
  76. Future<List<Friend>> getFriends() async {
  77. final db = await getDatabaseConnection();
  78. final List<Map<String, dynamic>> maps = await db.query('friends');
  79. return List.generate(maps.length, (i) {
  80. return Friend(
  81. id: maps[i]['id'],
  82. userId: maps[i]['user_id'],
  83. friendId: maps[i]['friend_id'],
  84. friendIdDecrypted: maps[i]['friend_id_decrypted'],
  85. friendSymmetricKey: maps[i]['symmetric_key'],
  86. friendSymmetricKeyDecrypted: maps[i]['symmetric_key_decrypted'],
  87. acceptedAt: maps[i]['accepted_at'],
  88. username: maps[i]['username'],
  89. );
  90. });
  91. }