diff --git a/About.xaml.cs b/About.xaml.cs index 4174e2a..9701fb3 100644 --- a/About.xaml.cs +++ b/About.xaml.cs @@ -11,7 +11,7 @@ namespace AnotherReplayReader public About() { InitializeComponent(); - _idBox.Text = Auth.ID; + _idBox.Text = Auth.Id; } private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e) diff --git a/Auth.cs b/Auth.cs index 1098838..1cd777a 100644 --- a/Auth.cs +++ b/Auth.cs @@ -9,11 +9,11 @@ namespace AnotherReplayReader { internal static class Auth { - public static string? ID { get; } + public static string? Id { get; } static Auth() { - ID = null; + Id = null; string? windowsID; try @@ -56,17 +56,17 @@ namespace AnotherReplayReader using var sha = SHA256.Create(); var hash = sha.ComputeHash(Encoding.UTF8.GetBytes(windowsID + randomKey)); - ID = string.Concat(hash.Skip(3).Take(10).Select(x => $"{x:X2}")); + Id = string.Concat(hash.Skip(3).Take(10).Select(x => $"{x:X2}")); } public static string GetKey() { - if (ID == null) + if (Id == null) { return string.Empty; } - var text = $"{ID}{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}"; + var text = $"{Id}{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}"; var bytes = Encoding.UTF8.GetBytes(text); var pre = Encoding.UTF8.GetBytes("playertable!"); var salt = new byte[9]; @@ -82,5 +82,17 @@ namespace AnotherReplayReader return Convert.ToBase64String(salt.Concat(bytes).Select((x, i) => (byte)(x ^ pre[i % pre.Length])).ToArray()); } + + public static byte[]? IdAsKey() + { + if (string.IsNullOrEmpty(Id)) + { + return null; + } + var bytes = Encoding.UTF8.GetBytes(Id); + var destination = Enumerable.Repeat(0xEA, 24).ToArray(); + Array.Copy(bytes, destination, Math.Min(bytes.Length, destination.Length)); + return destination; + } } } diff --git a/Cache.cs b/Cache.cs index 1365cf2..310f5a6 100644 --- a/Cache.cs +++ b/Cache.cs @@ -13,10 +13,12 @@ namespace AnotherReplayReader public static string CacheFilePath => Path.Combine(CacheDirectory, "AnotherReplayReader.cache"); private readonly ConcurrentDictionary _storage = new(); + + public Task Initialization { get; } public Cache() { - Task.Run(async () => + Initialization = Task.Run(async () => { try { diff --git a/PlayerIdentity.cs b/PlayerIdentity.cs index b5cfba1..b9f8c38 100644 --- a/PlayerIdentity.cs +++ b/PlayerIdentity.cs @@ -86,17 +86,19 @@ namespace AnotherReplayReader _list = converted; } - if (list is null) + if (list is null || Auth.IdAsKey() is not { } encryptKey) { _cache.Set(StoredKey, null); return; } - using var aes = Aes.Create(); - using var encryptor = aes.CreateEncryptor(Encoding.UTF8.GetBytes(Auth.ID), aes.IV); using var memory = new MemoryStream(); - using var decryptorStream = new CryptoStream(memory, encryptor, CryptoStreamMode.Write); - await SerializeAsync(decryptorStream, list, _jsonOptions).ConfigureAwait(false); + using var aes = Aes.Create(); + using (var encryptor = aes.CreateEncryptor(encryptKey, aes.IV)) + using (var decryptorStream = new CryptoStream(memory, encryptor, CryptoStreamMode.Write)) + { + await SerializeAsync(decryptorStream, list, _jsonOptions).ConfigureAwait(false); + } memory.Flush(); _cache.SetValues((StoredKey, Convert.ToBase64String(memory.ToArray())), (IvKey, Convert.ToBase64String(aes.IV))); } @@ -155,15 +157,16 @@ namespace AnotherReplayReader { try { + await _cache.Initialization; var stored = _cache.GetOrDefault(StoredKey, string.Empty); var iv = Convert.FromBase64String(_cache.GetOrDefault(IvKey, string.Empty)); - if (string.IsNullOrWhiteSpace(stored)) + if (string.IsNullOrWhiteSpace(stored) || Auth.IdAsKey() is not { } key) { return; } using var aes = Aes.Create(); - using var decryptor = aes.CreateDecryptor(Encoding.UTF8.GetBytes(Auth.ID), iv); + using var decryptor = aes.CreateDecryptor(key, iv); using var memory = new MemoryStream(Convert.FromBase64String(stored)); using var decryptorStream = new CryptoStream(memory, decryptor, CryptoStreamMode.Read); var cachedTable = await DeserializeAsync>(decryptorStream, _jsonOptions).ConfigureAwait(false); diff --git a/Window1.xaml b/Window1.xaml index fdd9701..7920edd 100644 --- a/Window1.xaml +++ b/Window1.xaml @@ -13,7 +13,11 @@