common/Ui/Common.UI/Models/SQLiteKeyStore.cs
2026-04-23 17:50:07 +02:00

63 lines
1.7 KiB
C#

namespace Common.UI.Models
{
using SQLite;
internal class RadioKey
{
[PrimaryKey]
[AutoIncrement]
public int Id { get; set; }
public uint Address { get; set; }
public string EncryptionKey { get; set; }
}
internal class SQLiteKeyStore
{
private readonly SQLiteConnection connection;
public SQLiteKeyStore()
{
this.connection = new SQLiteConnection(new SQLiteConnectionString("keystore.db"));
_ = this.connection.CreateTable<RadioKey>();
}
public bool TryGetKey(uint address1, uint address2, out string encryptionKey)
{
//var existing = this.connection
// .Table<RadioKey>()
// .Where(x => x.Address == 4291535650)
// .FirstOrDefault();
//if (existing != null)
//{
// existing.EncryptionKey = "5DD3EF5233B4DAA6836721F2866182DA";
// this.connection.Update(existing);
//}
encryptionKey = this.connection
.Table<RadioKey>()
.FirstOrDefault(x => x.Address == address1 || x.Address == address2)
?.EncryptionKey;
return !string.IsNullOrWhiteSpace(encryptionKey);
}
public void AddKey(uint address1, uint address2, string encryptionKey)
{
this.connection.Insert(new RadioKey
{
Address = address1,
EncryptionKey = encryptionKey
});
this.connection.Insert(new RadioKey
{
Address = address2,
EncryptionKey = encryptionKey
});
}
}
}