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.

127 lines
3.2 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.friendId == 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 friendSymmetricKey;
  20. String asymmetricPublicKey;
  21. String acceptedAt;
  22. bool? selected;
  23. Friend({
  24. required this.id,
  25. required this.userId,
  26. required this.username,
  27. required this.friendId,
  28. required this.friendSymmetricKey,
  29. required this.asymmetricPublicKey,
  30. required this.acceptedAt,
  31. this.selected,
  32. });
  33. factory Friend.fromJson(Map<String, dynamic> json, RSAPrivateKey privKey) {
  34. var friendIdDecrypted = CryptoUtils.rsaDecrypt(
  35. base64.decode(json['friend_id']),
  36. privKey,
  37. );
  38. var friendSymmetricKeyDecrypted = CryptoUtils.rsaDecrypt(
  39. base64.decode(json['symmetric_key']),
  40. privKey,
  41. );
  42. return Friend(
  43. id: json['id'],
  44. userId: json['user_id'],
  45. username: '',
  46. friendId: String.fromCharCodes(friendIdDecrypted),
  47. friendSymmetricKey: base64.encode(friendSymmetricKeyDecrypted),
  48. asymmetricPublicKey: '',
  49. acceptedAt: json['accepted_at'],
  50. );
  51. }
  52. @override
  53. String toString() {
  54. return '''
  55. id: $id
  56. userId: $userId
  57. username: $username
  58. friendId: $friendId
  59. accepted_at: $acceptedAt''';
  60. }
  61. Map<String, dynamic> toMap() {
  62. return {
  63. 'id': id,
  64. 'user_id': userId,
  65. 'username': username,
  66. 'friend_id': friendId,
  67. 'symmetric_key': friendSymmetricKey,
  68. 'asymmetric_public_key': asymmetricPublicKey,
  69. 'accepted_at': acceptedAt,
  70. };
  71. }
  72. }
  73. // A method that retrieves all the dogs from the dogs table.
  74. Future<List<Friend>> getFriends() async {
  75. final db = await getDatabaseConnection();
  76. final List<Map<String, dynamic>> maps = await db.query('friends');
  77. return List.generate(maps.length, (i) {
  78. return Friend(
  79. id: maps[i]['id'],
  80. userId: maps[i]['user_id'],
  81. friendId: maps[i]['friend_id'],
  82. friendSymmetricKey: maps[i]['symmetric_key'],
  83. asymmetricPublicKey: maps[i]['asymmetric_public_key'],
  84. acceptedAt: maps[i]['accepted_at'],
  85. username: maps[i]['username'],
  86. );
  87. });
  88. }
  89. Future<Friend> getFriendByFriendId(String userId) async {
  90. final db = await getDatabaseConnection();
  91. List<dynamic> whereArguments = [userId];
  92. final List<Map<String, dynamic>> maps = await db.query(
  93. 'friends',
  94. where: 'friend_id = ?',
  95. whereArgs: whereArguments,
  96. );
  97. if (maps.length != 1) {
  98. throw ArgumentError('Invalid user id');
  99. }
  100. return Friend(
  101. id: maps[0]['id'],
  102. userId: maps[0]['user_id'],
  103. friendId: maps[0]['friend_id'],
  104. friendSymmetricKey: maps[0]['symmetric_key'],
  105. asymmetricPublicKey: maps[0]['asymmetric_public_key'],
  106. acceptedAt: maps[0]['accepted_at'],
  107. username: maps[0]['username'],
  108. );
  109. }