XIVChat/XIVChat Desktop/TrustDialog.xaml.cs

86 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Channels;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
namespace XIVChat_Desktop {
/// <summary>
/// Interaction logic for TrustDialog.xaml
/// </summary>
public partial class TrustDialog {
private readonly ChannelWriter<bool> trustChannel;
private readonly byte[] remoteKey;
private App App => (App)Application.Current;
public TrustDialog(Window owner, ChannelWriter<bool> trustChannel, byte[] remoteKey) {
this.Owner = owner;
this.trustChannel = trustChannel;
this.remoteKey = remoteKey;
this.InitializeComponent();
this.ClientPublicKey.Text = ToHexString(this.App.Config.KeyPair.PublicKey);
var clientColours = BreakIntoColours(this.App.Config.KeyPair.PublicKey);
for (int i = 0; i < this.ClientPublicKeyColours.Children.Count; i++) {
var rect = (Rectangle)this.ClientPublicKeyColours.Children[i];
rect.Fill = new SolidColorBrush(clientColours[i]);
}
this.ServerPublicKey.Text = ToHexString(remoteKey);
var serverColours = BreakIntoColours(remoteKey);
for (int i = 0; i < this.ServerPublicKeyColours.Children.Count; i++) {
var rect = (Rectangle)this.ServerPublicKeyColours.Children[i];
rect.Fill = new SolidColorBrush(serverColours[i]);
}
}
private static List<Color> BreakIntoColours(IEnumerable<byte> key) {
var colours = new List<Color>();
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (var chunk in SplitList(key.ToList(), 3)) {
var r = chunk[0];
var g = chunk.Count > 1 ? chunk[1] : (byte)0;
var b = chunk.Count > 2 ? chunk[2] : (byte)0;
colours.Add(Color.FromRgb(r, g, b));
}
return colours;
}
private static IEnumerable<List<T>> SplitList<T>(List<T> locations, int nSize) {
for (int i = 0; i < locations.Count; i += nSize) {
yield return locations.GetRange(i, Math.Min(nSize, locations.Count - i));
}
}
private static string ToHexString(IEnumerable<byte> bytes) {
return string.Join("", bytes.Select(b => b.ToString("X2")));
}
private async void Yes_Click(object sender, RoutedEventArgs e) {
var keyName = this.KeyName.Text;
if (keyName.Length == 0) {
MessageBox.Show("You must give this key a name.");
return;
}
var trustedKey = new TrustedKey(keyName, this.remoteKey);
this.App.Config.TrustedKeys.Add(trustedKey);
this.App.Config.Save();
await this.trustChannel.WriteAsync(true);
this.Close();
}
private async void No_Click(object sender, RoutedEventArgs e) {
await this.trustChannel.WriteAsync(false);
this.Close();
}
}
}