2024-12-14 15:17:58 +00:00
|
|
|
|
namespace lib;
|
|
|
|
|
|
2024-12-20 10:13:39 +00:00
|
|
|
|
public enum RequestType
|
|
|
|
|
{
|
2024-12-14 15:17:58 +00:00
|
|
|
|
Register = 1,
|
|
|
|
|
ConfirmRegister = 2,
|
2024-12-17 18:41:30 +00:00
|
|
|
|
Login = 3,
|
|
|
|
|
GetMessages = 4,
|
|
|
|
|
GetUserKey = 5,
|
|
|
|
|
SendMessage = 6,
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-20 10:13:39 +00:00
|
|
|
|
public static class Request
|
|
|
|
|
{
|
|
|
|
|
public static byte[] CreateRequest(RequestType Type, ref byte counter, byte[] data)
|
|
|
|
|
{
|
|
|
|
|
if (data.Length > 13)
|
|
|
|
|
{
|
2024-12-20 09:23:49 +00:00
|
|
|
|
throw new Exception("extra data is too long: " + data.Length.ToString());
|
2024-12-17 18:41:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-20 10:13:39 +00:00
|
|
|
|
byte[] msg = new byte[16];
|
2024-12-17 18:41:30 +00:00
|
|
|
|
msg[0] = 0; // version
|
|
|
|
|
msg[1] = (byte)Type;
|
|
|
|
|
msg[2] = counter;
|
2024-12-20 10:13:39 +00:00
|
|
|
|
if (counter == byte.MaxValue)
|
|
|
|
|
{
|
2024-12-17 18:41:30 +00:00
|
|
|
|
counter = 0;
|
|
|
|
|
}
|
2024-12-20 10:13:39 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2024-12-17 18:41:30 +00:00
|
|
|
|
counter += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// insert data 3..16
|
|
|
|
|
Array.Copy(data, 0, msg, 3, data.Length);
|
|
|
|
|
return msg;
|
|
|
|
|
}
|
2024-12-20 10:13:39 +00:00
|
|
|
|
|
|
|
|
|
public static string RequestToString(byte[] Request)
|
|
|
|
|
{
|
|
|
|
|
if (Request.Length != 16)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Request size is not 128!");
|
|
|
|
|
}
|
|
|
|
|
string res = $"V: {Request[0]}, ";
|
|
|
|
|
res += $"Request type: {(RequestType)Request[1]}, ";
|
|
|
|
|
res += $"Counter: {Request[2]}\n";
|
|
|
|
|
res += $"Extra: {string.Join(' ', Request[3..].Select(b => b.ToString("{b8}")))}";
|
|
|
|
|
// also display extra data based on the request itself
|
|
|
|
|
switch ((RequestType)Request[1])
|
|
|
|
|
{
|
|
|
|
|
case RequestType.Register:
|
|
|
|
|
string phone = Utils.BytesToNumber(Request[3..11]);
|
|
|
|
|
int rsaLen = BitConverter.ToInt32(Request, 11);
|
|
|
|
|
res += $"Phone: {phone}, RSA Key length: {rsaLen}";
|
|
|
|
|
break;
|
|
|
|
|
case RequestType.ConfirmRegister:
|
|
|
|
|
string code = string.Join(' ', Request[3..9]);
|
|
|
|
|
int sigLen = BitConverter.ToInt32(Request, 9);
|
|
|
|
|
res += $"Code: {code}, signature length: {sigLen}";
|
|
|
|
|
break;
|
|
|
|
|
case RequestType.Login:
|
|
|
|
|
phone = Utils.BytesToNumber(Request[3..11]);
|
|
|
|
|
sigLen = BitConverter.ToInt16(Request, 11);
|
|
|
|
|
res += $"Phone: {phone}, signature length: {sigLen}";
|
|
|
|
|
break;
|
|
|
|
|
case RequestType.GetMessages:
|
|
|
|
|
break;
|
|
|
|
|
case RequestType.GetUserKey:
|
|
|
|
|
break;
|
|
|
|
|
case RequestType.SendMessage:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
res += "INVALID REQUEST TYPE";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2024-12-14 15:17:58 +00:00
|
|
|
|
}
|