| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- import 'dart:convert';
- import 'package:e2ee_chat/common/cipher.dart';
- import 'package:json_annotation/json_annotation.dart';
- import 'package:dash_chat/dash_chat.dart';
- import 'package:encrypt/encrypt.dart';
- import 'package:pointycastle/asymmetric/api.dart';
- part 'message.g.dart';
- @JsonSerializable()
- class Message {
- Message(this.from, this.to, this.cipher, this.cipherText, this.publicKey, this.iv);
- factory Message.encrypt(String from, String to, String plaintext, String publicKey) {
- // TODO: encrypt
- String cipher = "";
- String cipherText = "";
- String publicKey = "";
- String iv = "";
- return Message(from, to, cipher, cipherText, publicKey, iv);
- }
- factory Message.fromJson(Map<String, dynamic> json) => _$MessageFromJson(json);
- Map<String, dynamic> toJson() => _$MessageToJson(this);
- String from;
- String to;
- String cipher;
- String cipherText;
- String publicKey;
- String iv;
- Future<ChatMessage?> get chatMessage async {
- ChatMessage? _message;
- final RSAPrivateKey? _privateKey = await getRSAPrivateKey(from);
- if (_privateKey != null) {
- final _publicKey = (await getRSAPublicKey(publicKey))!;
- final _rsaEncrypter = Encrypter(
- RSA(publicKey: _publicKey, privateKey: _privateKey));
- final _keyBase64 = _rsaEncrypter.decrypt64(cipher);
- final _key = Key.fromBase64(_keyBase64);
- final _iv = IV.fromBase64(iv);
- final _aesEncrypter = Encrypter(AES(_key));
- final _decrypted = _aesEncrypter.decrypt64(cipherText, iv: _iv);
- _message = ChatMessage.fromJson(jsonDecode(_decrypted));
- }
- return _message;
- }
- }
- /*
- class GroupMessage {
- int id = 0;
- String plaintext = "";
- final user = ToOne<User>();
- final group = ToOne<Group>();
- @Transient()
- ChatMessage get chatMessage => ChatMessage.fromJson(jsonDecode(plaintext));
- }
- */
|