public static void DecryptTagFile(string source, string destination, byte[] key, OpCore core) { int bufferSize = 4096; byte[] buffer = new byte[4096]; // needs to be 4k to packet stream break/resume work string tempPath = (core != null) ? core.GetTempPath() : destination; G2Protocol protocol = (core != null) ? core.Network.Protocol : new G2Protocol(); using (FileStream tempFile = new FileStream(tempPath, FileMode.Create)) using (TaggedStream encFile = new TaggedStream(source, protocol)) using (IVCryptoStream stream = IVCryptoStream.Load(encFile, key)) { int read = bufferSize; while (read == bufferSize) { read = stream.Read(buffer, 0, bufferSize); tempFile.Write(buffer, 0, read); } } // move to official path if (core == null) { return; } File.Copy(tempPath, destination, true); File.Delete(tempPath); }
public void SaveLocal() { // create temp, write buddy list string tempPath = Core.GetTempPath(); byte[] key = Utilities.GenerateKey(Core.StrongRndGen, 256); using (IVCryptoStream crypto = IVCryptoStream.Save(tempPath, key)) { PacketStream stream = new PacketStream(crypto, Network.Protocol, FileAccess.Write); BuddyList.LockReading(() => { foreach (OpBuddy buddy in BuddyList.Values) { stream.WritePacket(buddy); } }); IgnoreList.LockReading(() => // we need to ensure we have name/key for each ignore { foreach (OpBuddy ignore in IgnoreList.Values) { stream.WritePacket(ignore); } }); } byte[] publicEncryptedKey = Core.User.Settings.KeyPair.Encrypt(key, false); //private key required to decrypt buddy list Cache.UpdateLocal(tempPath, publicEncryptedKey, null); }
public void SaveHeaders() { RunSaveHeaders = false; try { string tempPath = Core.GetTempPath(); using (IVCryptoStream stream = IVCryptoStream.Save(tempPath, LocalKey)) { FileMap.LockReading(delegate() { foreach (OpVersionedFile vfile in FileMap.Values) { if (vfile.SignedHeader != null) { stream.Write(vfile.SignedHeader, 0, vfile.SignedHeader.Length); } } }); stream.FlushFinalBlock(); } File.Copy(tempPath, HeaderPath, true); File.Delete(tempPath); } catch (Exception ex) { Core.Network.UpdateLog("VersionedFile", "Error saving data " + ex.Message); } }
public void SaveHeaders() { // save public shared lists try { Local.Key = Utilities.GenerateKey(Core.StrongRndGen, 256); string tempPath = Core.GetTempPath(); using (IVCryptoStream crypto = IVCryptoStream.Save(tempPath, Local.Key)) { PacketStream stream = new PacketStream(crypto, Network.Protocol, FileAccess.Write); Local.Files.LockReading(delegate() { foreach (SharedFile file in Local.Files) { if (file.Hash != null && file.ClientID == Core.Network.Local.ClientID && file.Public) { file.SaveLocal = false; stream.WritePacket(file); } } }); crypto.FlushFinalBlock(); } Utilities.HashTagFile(tempPath, Core.Network.Protocol, ref Local.Hash, ref Local.Size); string finalPath = GetPublicPath(Local); File.Copy(tempPath, finalPath, true); File.Delete(tempPath); } catch (Exception ex) { Network.UpdateLog("Share", "Error saving public: " + ex.Message); } // save private/public shared - this is also whats loaded on startup try { string tempPath = Core.GetTempPath(); using (IVCryptoStream crypto = IVCryptoStream.Save(tempPath, Core.User.Settings.FileKey)) { PacketStream stream = new PacketStream(crypto, Network.Protocol, FileAccess.Write); // save all files Local.Files.LockReading(delegate() { foreach (SharedFile file in Local.Files) { if (file.Hash != null && file.ClientID == Core.Network.Local.ClientID) { file.SaveLocal = true; stream.WritePacket(file); } } }); // save our public header if (Local.Hash != null) { stream.WritePacket(Local); } crypto.FlushFinalBlock(); } File.Copy(tempPath, HeaderPath, true); File.Delete(tempPath); } catch (Exception ex) { Network.UpdateLog("Share", "Error saving headers: " + ex.Message); } }
public void SaveLocal(uint project) { try { string tempPath = Core.GetTempPath(); byte[] key = Utilities.GenerateKey(Core.StrongRndGen, 256); using (IVCryptoStream stream = IVCryptoStream.Save(tempPath, key)) { // write loaded projects WorkingStorage working = null; if (Working.ContainsKey(project)) { working = Working[project]; } if (working != null) { Protocol.WriteToFile(new StorageRoot(working.ProjectID), stream); working.WriteWorkingFile(stream, working.RootFolder, true); working.Modified = false; try { File.Delete(GetWorkingPath(project)); } catch { } } // open old file and copy entries, except for working OpStorage local = GetStorage(Core.UserID); if (local != null) { string oldPath = GetFilePath(local); if (File.Exists(oldPath)) { using (TaggedStream file = new TaggedStream(oldPath, Network.Protocol)) using (IVCryptoStream crypto = IVCryptoStream.Load(file, local.File.Header.FileKey)) { PacketStream oldStream = new PacketStream(crypto, Protocol, FileAccess.Read); bool write = false; G2Header g2header = null; while (oldStream.ReadPacket(ref g2header)) { if (g2header.Name == StoragePacket.Root) { StorageRoot root = StorageRoot.Decode(g2header); write = (root.ProjectID != project); } //copy packet right to new file if (write) //crit test { stream.Write(g2header.Data, g2header.PacketPos, g2header.PacketSize); } } } } } stream.WriteByte(0); // signal last packet stream.FlushFinalBlock(); } SavingLocal = true; // prevents auto-integrate from re-calling saveLocal OpVersionedFile vfile = Cache.UpdateLocal(tempPath, key, BitConverter.GetBytes(Core.TimeNow.ToUniversalTime().ToBinary())); SavingLocal = false; Store.PublishDirect(Core.Trust.GetLocsAbove(), Core.UserID, ServiceID, FileTypeCache, vfile.SignedHeader); } catch (Exception ex) { Core.Network.UpdateLog("Storage", "Error updating local " + ex.Message); } if (StorageUpdate != null) { Core.RunInGuiThread(StorageUpdate, GetStorage(Core.UserID)); } }
public void SaveLocal(string template, Dictionary <string, string> textFields, Dictionary <string, string> fileFields) { try { long embeddedStart = 0; string tempPath = Core.GetTempPath(); byte[] key = Utilities.GenerateKey(Core.StrongRndGen, 256); using (IVCryptoStream stream = IVCryptoStream.Save(tempPath, key)) { int written = 0; // write template info byte[] htmlBytes = UTF8Encoding.UTF8.GetBytes(template); written += Protocol.WriteToFile(new ProfileAttachment("template", htmlBytes.Length), stream); // write fields info (convert into fields into packet list) List <byte[]> fieldPackets = new List <byte[]>(); int fieldsTotalSize = 0; if (textFields != null) { foreach (KeyValuePair <string, string> pair in textFields) { if (pair.Value == null) { continue; } ProfileField field = new ProfileField(); field.Name = pair.Key; field.Value = UTF8Encoding.UTF8.GetBytes(pair.Value); field.FieldType = ProfileFieldType.Text; byte[] packet = field.Encode(Network.Protocol); fieldPackets.Add(packet); fieldsTotalSize += packet.Length; } } if (fileFields != null) { foreach (KeyValuePair <string, string> pair in fileFields) { if (pair.Value == null) { continue; } ProfileField field = new ProfileField(); field.Name = pair.Key; field.Value = UTF8Encoding.UTF8.GetBytes(Path.GetFileName(pair.Value)); field.FieldType = ProfileFieldType.File; byte[] packet = field.Encode(Network.Protocol); fieldPackets.Add(packet); fieldsTotalSize += packet.Length; } } if (fieldsTotalSize > 0) { written += Protocol.WriteToFile(new ProfileAttachment("fields", fieldsTotalSize), stream); } // write files info if (fileFields != null) { foreach (string path in fileFields.Values) { using (FileStream file = File.OpenRead(path)) written += Protocol.WriteToFile(new ProfileAttachment("file=" + Path.GetFileName(path), file.Length), stream); } } stream.WriteByte(0); // end packets embeddedStart = written + 1; // write template bytes stream.Write(htmlBytes, 0, htmlBytes.Length); // write field bytes foreach (byte[] packet in fieldPackets) { stream.Write(packet, 0, packet.Length); } // write file bytes const int buffSize = 4096; byte[] buffer = new byte[buffSize]; if (fileFields != null) { foreach (string path in fileFields.Values) { using (FileStream file = File.OpenRead(path)) { int read = buffSize; while (read == buffSize) { read = file.Read(buffer, 0, buffSize); stream.Write(buffer, 0, read); } } } } stream.FlushFinalBlock(); } OpVersionedFile vfile = Cache.UpdateLocal(tempPath, key, BitConverter.GetBytes(embeddedStart)); Store.PublishDirect(Trust.GetLocsAbove(), Core.UserID, ServiceID, DataTypeFile, vfile.SignedHeader); } catch (Exception ex) { Network.UpdateLog("Profile", "Error updating local " + ex.Message); } }
public void SaveLocal() { try { string tempPath = Core.GetTempPath(); byte[] key = Utilities.GenerateKey(Core.StrongRndGen, 256); using (IVCryptoStream stream = IVCryptoStream.Save(tempPath, key)) { // write dummy block if nothing to write OpPlan plan = GetPlan(Core.UserID, true); if (plan == null || plan.Blocks == null || plan.Blocks.Count == 0) { Protocol.WriteToFile(new PlanBlock(), stream); } if (plan != null) { foreach (List <PlanBlock> list in plan.Blocks.Values) { foreach (PlanBlock block in list) { Protocol.WriteToFile(block, stream); } } foreach (List <PlanGoal> list in plan.GoalMap.Values) { foreach (PlanGoal goal in list) { GetEstimate(goal, ref goal.EstCompleted, ref goal.EstTotal); Protocol.WriteToFile(goal, stream); } } foreach (List <PlanItem> list in plan.ItemMap.Values) { foreach (PlanItem item in list) { Protocol.WriteToFile(item, stream); } } } stream.WriteByte(0); // signal last packet stream.FlushFinalBlock(); } OpVersionedFile file = Cache.UpdateLocal(tempPath, key, null); Store.PublishDirect(Core.Trust.GetLocsAbove(), Core.UserID, ServiceID, DataTypeFile, file.SignedHeader); } catch (Exception ex) { Core.Network.UpdateLog("Plan", "Error updating local " + ex.Message); } }
public void Save() { if (Core != null && Core.InvokeRequired) { Debug.Assert(false); Core.RunInCoreAsync(() => Save()); return; } string backupPath = ProfilePath.Replace(".dop", ".bak"); if (!File.Exists(backupPath) && File.Exists(ProfilePath)) { File.Copy(ProfilePath, backupPath, true); } RijndaelManaged Password = new RijndaelManaged(); Password.Key = PasswordKey; try { // Attach to crypto stream and write file string tempPath = TempPath + Path.DirectorySeparatorChar + "firstsave"; if (Core != null) { tempPath = Core.GetTempPath(); } using (FileStream file = new FileStream(tempPath, FileMode.Create)) { // write encrypted part of file Password.GenerateIV(); file.Write(Password.IV, 0, Password.IV.Length); file.Write(PasswordSalt, 0, PasswordSalt.Length); using (CryptoStream crypto = new CryptoStream(file, Password.CreateEncryptor(), CryptoStreamMode.Write)) { PacketStream stream = new PacketStream(crypto, Protocol, FileAccess.Write); stream.WritePacket(Settings); if (Core != null) { if (Core.Context.Lookup != null) { Core.Context.Lookup.Network.Cache.SaveIPs(stream); Core.Context.Lookup.Network.Cache.SaveWeb(stream); } Core.Network.Cache.SaveIPs(stream); Core.Network.Cache.SaveWeb(stream); Core.SaveKeyIndex(stream); } if (OpIcon != null) { stream.WritePacket(new IconPacket(IdentityPacket.Icon, OpIcon)); } } } // write unencrypted splash using (FileStream file = new FileStream(tempPath, FileMode.Open)) { file.Seek(0, SeekOrigin.End); long startpos = file.Position; PacketStream stream = new PacketStream(file, Protocol, FileAccess.Write); // get right splash image (only used for startup logo, main setting is in link file) if (OpSplash != null) { MemoryStream mem = new MemoryStream(); OpSplash.Save(mem, ImageFormat.Jpeg); LargeDataPacket.Write(stream, IdentityPacket.Splash, mem.ToArray()); } else { LargeDataPacket.Write(stream, IdentityPacket.Splash, null); } file.WriteByte(0); // end packet stream byte[] last = BitConverter.GetBytes(startpos); file.Write(last, 0, last.Length); } File.Copy(tempPath, ProfilePath, true); File.Delete(tempPath); } catch (Exception ex) { if (Core != null) { Core.ConsoleLog("Exception Identity::Save() " + ex.Message); } else { Core.UserMessage("Profile Save Error:\n" + ex.Message + "\nBackup Restored"); } // restore backup if (File.Exists(backupPath)) { File.Copy(backupPath, ProfilePath, true); } } File.Delete(backupPath); }