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.

101 lines
2.7 KiB

  1. import 'dart:convert';
  2. import 'package:pointycastle/export.dart';
  3. import '/utils/encryption/crypto_utils.dart';
  4. import '/utils/encryption/aes_helper.dart';
  5. import '/utils/storage/database.dart';
  6. Conversation findConversationByDetailId(List<Conversation> conversations, String id) {
  7. for (var conversation in conversations) {
  8. if (conversation.conversationDetailId == id) {
  9. return conversation;
  10. }
  11. }
  12. // Or return `null`.
  13. throw ArgumentError.value(id, "id", "No element with that id");
  14. }
  15. class Conversation {
  16. String id;
  17. String userId;
  18. String conversationDetailId;
  19. String symmetricKey;
  20. bool admin;
  21. String name;
  22. Conversation({
  23. required this.id,
  24. required this.userId,
  25. required this.conversationDetailId,
  26. required this.symmetricKey,
  27. required this.admin,
  28. required this.name,
  29. });
  30. factory Conversation.fromJson(Map<String, dynamic> json, RSAPrivateKey privKey) {
  31. var symmetricKeyDecrypted = CryptoUtils.rsaDecrypt(
  32. base64.decode(json['symmetric_key']),
  33. privKey,
  34. );
  35. var detailId = AesHelper.aesDecrypt(
  36. symmetricKeyDecrypted,
  37. base64.decode(json['conversation_detail_id']),
  38. );
  39. var admin = AesHelper.aesDecrypt(
  40. symmetricKeyDecrypted,
  41. base64.decode(json['admin']),
  42. );
  43. return Conversation(
  44. id: json['id'],
  45. userId: json['user_id'],
  46. conversationDetailId: detailId,
  47. symmetricKey: base64.encode(symmetricKeyDecrypted),
  48. admin: admin == 'true',
  49. name: 'Unknown',
  50. );
  51. }
  52. @override
  53. String toString() {
  54. return '''
  55. id: $id
  56. userId: $userId
  57. name: $name
  58. admin: $admin''';
  59. }
  60. Map<String, dynamic> toMap() {
  61. return {
  62. 'id': id,
  63. 'user_id': userId,
  64. 'conversation_detail_id': conversationDetailId,
  65. 'symmetric_key': symmetricKey,
  66. 'admin': admin ? 1 : 0,
  67. 'name': name,
  68. };
  69. }
  70. }
  71. // A method that retrieves all the dogs from the dogs table.
  72. Future<List<Conversation>> getConversations() async {
  73. final db = await getDatabaseConnection();
  74. final List<Map<String, dynamic>> maps = await db.query('conversations');
  75. return List.generate(maps.length, (i) {
  76. return Conversation(
  77. id: maps[i]['id'],
  78. userId: maps[i]['user_id'],
  79. conversationDetailId: maps[i]['conversation_detail_id'],
  80. symmetricKey: maps[i]['symmetric_key'],
  81. admin: maps[i]['admin'] == 1,
  82. name: maps[i]['name'],
  83. );
  84. });
  85. }