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.

93 lines
2.2 KiB

  1. import 'dart:convert';
  2. import 'dart:typed_data';
  3. import 'package:pointycastle/impl.dart';
  4. import '/utils/encryption/aes_helper.dart';
  5. import '/utils/encryption/crypto_utils.dart';
  6. class ConversationUser {
  7. String id;
  8. String userId;
  9. String conversationId;
  10. String username;
  11. String associationKey;
  12. RSAPublicKey publicKey;
  13. bool admin;
  14. ConversationUser({
  15. required this.id,
  16. required this.userId,
  17. required this.conversationId,
  18. required this.username,
  19. required this.associationKey,
  20. required this.publicKey,
  21. required this.admin,
  22. });
  23. factory ConversationUser.fromJson(Map<String, dynamic> json, Uint8List symmetricKey) {
  24. String userId = AesHelper.aesDecrypt(
  25. symmetricKey,
  26. base64.decode(json['user_id']),
  27. );
  28. String username = AesHelper.aesDecrypt(
  29. symmetricKey,
  30. base64.decode(json['username']),
  31. );
  32. String associationKey = AesHelper.aesDecrypt(
  33. symmetricKey,
  34. base64.decode(json['association_key']),
  35. );
  36. String admin = AesHelper.aesDecrypt(
  37. symmetricKey,
  38. base64.decode(json['admin']),
  39. );
  40. String publicKeyString = AesHelper.aesDecrypt(
  41. symmetricKey,
  42. base64.decode(json['public_key']),
  43. );
  44. RSAPublicKey publicKey = CryptoUtils.rsaPublicKeyFromPem(publicKeyString);
  45. return ConversationUser(
  46. id: json['id'],
  47. conversationId: json['conversation_detail_id'],
  48. userId: userId,
  49. username: username,
  50. associationKey: associationKey,
  51. publicKey: publicKey,
  52. admin: admin == 'true',
  53. );
  54. }
  55. Map<String, dynamic> toJson() {
  56. return {
  57. 'id': id,
  58. 'user_id': userId,
  59. 'username': username,
  60. 'association_key': associationKey,
  61. 'asymmetric_public_key': publicKeyPem(),
  62. 'admin': admin ? 'true' : 'false',
  63. };
  64. }
  65. Map<String, dynamic> toMap() {
  66. return {
  67. 'id': id,
  68. 'user_id': userId,
  69. 'conversation_id': conversationId,
  70. 'username': username,
  71. 'association_key': associationKey,
  72. 'asymmetric_public_key': publicKeyPem(),
  73. 'admin': admin ? 1 : 0,
  74. };
  75. }
  76. String publicKeyPem() {
  77. return CryptoUtils.encodeRSAPublicKeyToPem(publicKey);
  78. }
  79. }