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.

137 lines
3.5 KiB

  1. import 'dart:convert';
  2. import 'dart:typed_data';
  3. import "package:pointycastle/export.dart";
  4. import '../utils/encryption/aes_helper.dart';
  5. import '/utils/encryption/crypto_utils.dart';
  6. import '/utils/storage/database.dart';
  7. Friend findFriendByFriendId(List<Friend> friends, String id) {
  8. for (var friend in friends) {
  9. if (friend.friendId == id) {
  10. return friend;
  11. }
  12. }
  13. // Or return `null`.
  14. throw ArgumentError.value(id, "id", "No element with that id");
  15. }
  16. class Friend{
  17. String id;
  18. String userId;
  19. String username;
  20. String friendId;
  21. String friendSymmetricKey;
  22. String asymmetricPublicKey;
  23. String acceptedAt;
  24. bool? selected;
  25. Friend({
  26. required this.id,
  27. required this.userId,
  28. required this.username,
  29. required this.friendId,
  30. required this.friendSymmetricKey,
  31. required this.asymmetricPublicKey,
  32. required this.acceptedAt,
  33. this.selected,
  34. });
  35. factory Friend.fromJson(Map<String, dynamic> json, RSAPrivateKey privKey) {
  36. Uint8List friendIdDecrypted = CryptoUtils.rsaDecrypt(
  37. base64.decode(json['friend_id']),
  38. privKey,
  39. );
  40. Uint8List friendUsername = CryptoUtils.rsaDecrypt(
  41. base64.decode(json['friend_username']),
  42. privKey,
  43. );
  44. Uint8List friendSymmetricKeyDecrypted = CryptoUtils.rsaDecrypt(
  45. base64.decode(json['symmetric_key']),
  46. privKey,
  47. );
  48. String asymmetricPublicKey = AesHelper.aesDecrypt(
  49. friendSymmetricKeyDecrypted,
  50. base64.decode(json['asymmetric_public_key'])
  51. );
  52. return Friend(
  53. id: json['id'],
  54. userId: json['user_id'],
  55. username: String.fromCharCodes(friendUsername),
  56. friendId: String.fromCharCodes(friendIdDecrypted),
  57. friendSymmetricKey: base64.encode(friendSymmetricKeyDecrypted),
  58. asymmetricPublicKey: asymmetricPublicKey,
  59. acceptedAt: json['accepted_at'],
  60. );
  61. }
  62. @override
  63. String toString() {
  64. return '''
  65. id: $id
  66. userId: $userId
  67. username: $username
  68. friendId: $friendId
  69. accepted_at: $acceptedAt''';
  70. }
  71. Map<String, dynamic> toMap() {
  72. return {
  73. 'id': id,
  74. 'user_id': userId,
  75. 'username': username,
  76. 'friend_id': friendId,
  77. 'symmetric_key': friendSymmetricKey,
  78. 'asymmetric_public_key': asymmetricPublicKey,
  79. 'accepted_at': acceptedAt,
  80. };
  81. }
  82. }
  83. // A method that retrieves all the dogs from the dogs table.
  84. Future<List<Friend>> getFriends() async {
  85. final db = await getDatabaseConnection();
  86. final List<Map<String, dynamic>> maps = await db.query('friends');
  87. return List.generate(maps.length, (i) {
  88. return Friend(
  89. id: maps[i]['id'],
  90. userId: maps[i]['user_id'],
  91. friendId: maps[i]['friend_id'],
  92. friendSymmetricKey: maps[i]['symmetric_key'],
  93. asymmetricPublicKey: maps[i]['asymmetric_public_key'],
  94. acceptedAt: maps[i]['accepted_at'],
  95. username: maps[i]['username'],
  96. );
  97. });
  98. }
  99. Future<Friend> getFriendByFriendId(String userId) async {
  100. final db = await getDatabaseConnection();
  101. final List<Map<String, dynamic>> maps = await db.query(
  102. 'friends',
  103. where: 'friend_id = ?',
  104. whereArgs: [userId],
  105. );
  106. if (maps.length != 1) {
  107. throw ArgumentError('Invalid user id');
  108. }
  109. return Friend(
  110. id: maps[0]['id'],
  111. userId: maps[0]['user_id'],
  112. friendId: maps[0]['friend_id'],
  113. friendSymmetricKey: maps[0]['symmetric_key'],
  114. asymmetricPublicKey: maps[0]['asymmetric_public_key'],
  115. acceptedAt: maps[0]['accepted_at'],
  116. username: maps[0]['username'],
  117. );
  118. }