Ejemplo n.º 1
0
        private TData ReadFromFile()
        {
            try
            {
                if (!File.Exists(_jsonFilePath))
                {
                    this.SaveToFile(default(TData));
                }

                string json;

                using (var f = new FileStream(_jsonFilePath,
                                              FileMode.Open,
                                              FileAccess.Read,
                                              FileShare.ReadWrite))
                {
                    using (var r = new StreamReader(f, Encoding.UTF8))
                    {
                        json = r.ReadToEnd();
                    }
                }

                var data = JsonConvert.DeserializeObject <SedeRootDto>(json);

                if (ReferenceEquals(data, null))
                {
                    File.Delete(_jsonFilePath);
                    return(default(TData));
                }

                var md5 = EncryptionProvider.MD5($"{data.EncryptedData}_{_StorageSalt}");

                if (md5.Equals(data.Md5))
                {
                    var rj = EncryptionProvider.DecryptText(data.EncryptedData, _StorageSalt);
                    return(JsonConvert.DeserializeObject <TData>(rj));
                }

                //todo: Implement safety removing of data file
                File.Delete(_jsonFilePath);
                return(default(TData));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }