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.

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