online_security_project/server/Data.cs

44 lines
No EOL
1.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Security.Cryptography;
namespace server;
public class Data
{
public Dictionary<string, RSA> Keys { set; get; } = [];
public Dictionary<string, Queue<byte[]>> Messages { set; get; } = [];
public RSA? GetKey(string Phone)
{
return Keys.TryGetValue(Phone, out RSA? value) ? value : null;
}
public List<byte[]>? GetMessages(string Phone, int limit = -1)
{
// Check we have a RSA key for the phone and get the messages
if (!Keys.ContainsKey(Phone)) { return null; }
if (Messages.TryGetValue(Phone, out Queue<byte[]>? value))
{
List<byte[]> msgs = new(limit == -1 ? value.Count : Math.Min(value.Count, limit));
int count = 0;
while (count != limit && value.TryDequeue(out byte[]? m))
{
count += 1;
msgs.Add(m);
}
return msgs;
}
else
{
// generate a new queue because one doesnt already exists
Messages[Phone] = new Queue<byte[]>();
return []; // no messages were in the list so no reason to attempt to send any message
}
}
public bool PeekMessages(string Phone)
{
return Messages.TryGetValue(Phone, out Queue<byte[]>? value) && value.TryPeek(out var _);
}
}