private string BuildUnlockCode(byte[] codeByte) { string hash = EncDecHelper.MD5(codeByte); string code = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(hash)).ToUpper(); for (int i = 1; i < 4; i++) { code = code.Insert(i * 8 + (i - 1), "-"); } return(code); }
/// <summary> /// Crea o aggiorna il file della cache /// </summary> public async Task WriteCache() { try { JObject json = new JObject { { "version", CurrentVersionSettings } }; JArray productsJson = new JArray(); foreach (ProductCache item in ProductsList) { JObject jsonProd = new JObject { { nameof(item.Id), item.Id }, { nameof(item.Purchased), item.Purchased }, { nameof(item.Unlocked), item.Unlocked }, { nameof(item.UnlockCode), item.UnlockCode }, { nameof(item.UnlockCodeExpire), item.UnlockCodeExpire } }; productsJson.Add(jsonProd); } json.Add("products", productsJson); string content = json.ToString(); EncDecHelper enc = new EncDecHelper { PasswordString = Password }; StreamWriter streamWriter = new StreamWriter(AppCacheFileName, false); await streamWriter.WriteAsync(enc.AESEncrypt(content)); streamWriter.Close(); } catch (Exception ex) { Debug.WriteLine("CacheManager error on write: " + ex.Message); } }
/// <summary> /// Legge i dati presenti in cache /// </summary> /// <returns></returns> public async Task ReadCache() { ProductsList.Clear(); try { StreamReader streamReader = new StreamReader(AppCacheFileName); string content = await streamReader.ReadToEndAsync(); streamReader.Close(); if (string.IsNullOrEmpty(content)) { await WriteCache(); return; } EncDecHelper enc = new EncDecHelper { PasswordString = Password }; string json = enc.AESDecrypt(content, out bool warning); if (warning) { Debug.WriteLine("CacheManager read: warning in file reading"); } JObject jObject = JObject.Parse(json); if (Utility.SafeTryGetJSONString(jObject, "version", out string version)) { Debug.WriteLine("CacheManager file version " + version); } if (Utility.SafeTryGetJSON(jObject, "products", out JToken productsToken)) { JArray products = productsToken.ToObject <JArray>(); foreach (JObject item in products) { ProductCache product = new ProductCache { Id = item[nameof(ProductCache.Id)].ToString(), Purchased = item[nameof(ProductCache.Purchased)].ToObject <bool>() }; // Dalla versione 1.1.0 ho il codice di sblocco della versioen PLUS if (CompareVersions(version, "1.1.0") >= 0) { product.Unlocked = item[nameof(ProductCache.Unlocked)].ToObject <bool>(); product.UnlockCode = item[nameof(ProductCache.UnlockCode)].ToObject <string>(); product.UnlockCodeExpire = item[nameof(ProductCache.UnlockCodeExpire)].ToObject <bool>(); } ProductsList.Add(product); } } } catch (FileNotFoundException) { // Ok. Genera il file await WriteCache(); } catch (Exception ex) { Debug.WriteLine("CacheManager error on read: " + ex.Message); } }