work on SendMessage
This commit is contained in:
parent
edf4f91fa9
commit
145e77e437
3 changed files with 75 additions and 1 deletions
72
client/Message.cs
Normal file
72
client/Message.cs
Normal file
|
@ -0,0 +1,72 @@
|
|||
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using lib;
|
||||
|
||||
namespace client;
|
||||
|
||||
public class Message
|
||||
{
|
||||
public uint Id { set; get; }
|
||||
public string Sender { set; get; }
|
||||
public bool IsAck { set; get; }
|
||||
public string Content { set; get; }
|
||||
public byte[] Signature { set; get; }
|
||||
|
||||
public Message(byte[] bytes)
|
||||
{
|
||||
if (bytes[0] != 0)
|
||||
{
|
||||
Console.WriteLine("Failed parsing message, unsupported version");
|
||||
}
|
||||
// skip the first byte for simplicity
|
||||
bytes = bytes[1..];
|
||||
Id = BitConverter.ToUInt32(bytes, 0);
|
||||
Sender = Utils.BytesToNumber(bytes[4..12]);
|
||||
IsAck = bytes[12] != 0;
|
||||
Content = Encoding.UTF8.GetString(bytes[13..(bytes.Length - 32)]);
|
||||
Signature = bytes[(bytes.Length - 32)..];
|
||||
}
|
||||
|
||||
public Message(uint Id, string Sender, bool IsAck, string Content)
|
||||
{
|
||||
this.Id = Id;
|
||||
this.Sender = Sender;
|
||||
this.IsAck = IsAck;
|
||||
this.Content = Content;
|
||||
Signature = [];
|
||||
}
|
||||
|
||||
public void CalculateSignature(RSA key)
|
||||
{
|
||||
Signature = key.SignData(Bytes(false), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
|
||||
}
|
||||
|
||||
public bool IsSignatureValid(RSA key)
|
||||
{
|
||||
return key.VerifyData(Bytes(false), Signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
|
||||
}
|
||||
|
||||
public byte[] Bytes(bool IncludeSignature = true)
|
||||
{
|
||||
List<byte> msg = [];
|
||||
// 0..4
|
||||
msg.AddRange(BitConverter.GetBytes(Id));
|
||||
// 4..12
|
||||
msg.AddRange(Utils.NumberToBytes(Sender));
|
||||
// 12
|
||||
msg.Add((byte)(IsAck ? 1 : 0));
|
||||
// 13..(len - 32)
|
||||
msg.AddRange(Encoding.UTF8.GetBytes(Content));
|
||||
// (len - 32)..
|
||||
if (IncludeSignature) // we dont want to include the signature when we sign/verify the data
|
||||
{
|
||||
msg.AddRange(Signature);
|
||||
}
|
||||
|
||||
return [0, .. msg];
|
||||
}
|
||||
}
|
|
@ -289,7 +289,7 @@ public class Program
|
|||
// even if we know who sent the message we dont know which message it is unless the server also knows
|
||||
// and i dont want the server to be aware of that, i think it makes more sense for the server to act as a relay
|
||||
// and a buffer than an actual participant, so if the message is failing to decrypt that will go unnoticed.
|
||||
// supposedly the sender will notice the lack of ACK and send it again
|
||||
// supposedly the sender will notice the lack of response and send it again
|
||||
Console.WriteLine("Incoming message failed to decrypt, unknown sender");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -240,6 +240,8 @@ public class Program
|
|||
}
|
||||
break;
|
||||
case RequestType.SendMessage:
|
||||
string recv = Utils.BytesToNumber(msg[3..11]);
|
||||
int msgLen = BitConverter.ToInt32(msg, 11);
|
||||
|
||||
break;
|
||||
default:
|
||||
|
|
Loading…
Reference in a new issue