static void Main(string[] args) { int i = 1; while (i == 1) { MainFile mainfile = new MainFile(); readInfoAboutFile(mainfile); Console.WriteLine("Start - " + DateTime.Now); FillFile fillFile = new FillFile(); fillFile.fill(mainfile); SplitFile splitFile = new SplitFile(); List <String> filesname = splitFile.split(mainfile); SortOneFile sortOneFile = new SortOneFile(); sortOneFile.sortOne(filesname); SortAllFiles sortAllFiles = new SortAllFiles(); sortAllFiles.sortAll(filesname, mainfile); Console.WriteLine("Stop - " + DateTime.Now + "\n"); Console.Write("1 - One more time\n0 - Exit\nYour decision - "); i = int.Parse(Console.ReadLine()); Console.WriteLine(); } }
public bool Check() { Logger.Info("Will check " + this.Path); bool flag = false; if (!File.Exists(this.Path)) { Logger.Error("File missing"); return(false); } using (Stream stream = (Stream)File.OpenRead(this.Path)) { if (stream.Length != this.Size) { Logger.Error("File size incorrect: " + stream.Length.ToString()); return(false); } if (SplitFile.CheckSum(stream) == this.SHA1) { this.DownloadedSize = this.Size; Logger.Info("File size correct"); flag = true; } } return(flag); }
private void InitFromFiles(List <string> orderedSplitFiles) { _totalLength = 0; for (int i = 0; i < orderedSplitFiles.Count; i++) { var sf = new SplitFile(_fileProvider.GetReadStream(orderedSplitFiles[i])) { Filename = orderedSplitFiles[i], StartOffset = (int)_totalLength }; _totalLength += sf.Length; _files.Add(sf); } }
public static void decryptFile(BigInteger p, BigInteger x, int sizeOfBlock, string iFileName, string oFileName) { var blocks = SplitFile.GetBlocksFromFile(iFileName, sizeOfBlock * 2, false); long lenght = blocks.Length; SetMaximum(lenght); List <BigInteger> newBlocks = new List <BigInteger>(); for (int i = 0; i < blocks.Length; i += 2) { SetValue(i); var a = blocks[i]; var b = blocks[i + 1]; BigInteger m = decrypt(p, x, a, b); newBlocks.Add(m); } SplitFile.WriteBlocksToFile(oFileName, newBlocks.ToArray(), sizeOfBlock, true); EncodeFinished($"Файл\n{iFileName}\nуспешно дешифрован\n\nСоздан файл\n{oFileName}\nс открытым текстом"); }
public static void Split(string path, int size, SplitFile.ProgressCb progressCb) { byte[] buffer = new byte[16384]; using (Stream stream1 = (Stream)File.OpenRead(path)) { int num = 0; string.Format((IFormatProvider)CultureInfo.InvariantCulture, "{0}.manifest", (object)path); while (stream1.Position < stream1.Length) { string path1 = string.Format((IFormatProvider)CultureInfo.InvariantCulture, "{0}_part_{1}", (object)path, (object)num); using (Stream stream2 = (Stream)File.Create(path1)) { int val1 = size; int count; for (; val1 > 0; val1 -= count) { count = stream1.Read(buffer, 0, Math.Min(val1, 16384)); if (count != 0) { stream2.Write(buffer, 0, count); } else { break; } } } string manifest = (string)null; using (Stream stream2 = (Stream)File.OpenRead(path1)) { string str = SplitFile.CheckSum(stream2); long length = stream2.Length; manifest = string.Format((IFormatProvider)CultureInfo.InvariantCulture, "{0} {1} {2}", (object)Path.GetFileName(path1), (object)length, (object)str); } progressCb(manifest); ++num; } } }
public static void cryptFile(BigInteger p, BigInteger g, BigInteger x, int sizeOfBlock, string iFileName, string oFileName) { long i = 0; var y = g.modPow(x, p); var blocks = SplitFile.GetBlocksFromFile(iFileName, sizeOfBlock, true); long lenght = blocks.Length; SetMaximum(lenght); List <BigInteger> newBlocks = new List <BigInteger>(); foreach (var m in blocks) { BigInteger k = (Rand() % (p - 1) + 1); BigInteger a; BigInteger b; crypt(p, g, y, m, out a, out b); newBlocks.Add(a); newBlocks.Add(b); SetValue(i); i++; } SplitFile.WriteBlocksToFile(oFileName, newBlocks.ToArray(), sizeOfBlock * 2, false); EncodeFinished($"Файл\n{iFileName}\nуспешно зашифрован\n\nСоздан файл\n{oFileName}\nс шифротекстом"); }
/// <summary> /// Copies a <paramref name="source"/> file to a <paramref name="destination"/> splitting it into segments each sized <paramref name="splitSize"/> /// </summary> /// <param name="source">Source <see cref="FileInfo"/> to copy</param> /// <param name="destination">Destination <see cref="FileInfo"/> to copy to</param> /// <param name="splitSize">Size of split segments</param> /// <param name="onWrite">Action will be called everytime theres an advance in bytes with the number of bytes as parameter, to track progress. Can be null.</param> public static void Copy(FileInfo source, FileInfo destination, long splitSize, Action <long>?onWrite = null) { var sourceSplit = new SplitFile(source, splitSize); var destSplit = new SplitFile(destination, splitSize); int bound; if (sourceSplit.SplitInfos.Length > destSplit.SplitInfos.Length) { // If source is bigger than destination copy from source at last destination pos to end bound = destSplit.SplitInfos.Length; SplitInfo infoSource = sourceSplit.SplitInfos[bound]; using var sourceStream = new FileStream(sourceSplit.File.FullName, FileMode.Open, FileAccess.Read, FileShare.Read); using var destStream = new FileStream(destSplit.File.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); sourceStream.Position = infoSource.StartIndex; destStream.Position = infoSource.StartIndex; int len = (int)(source.Length - infoSource.StartIndex); StreamCopy(sourceStream, destStream, len, onWrite); } else if (sourceSplit.SplitInfos.Length < destSplit.SplitInfos.Length || sourceSplit.SplitInfos[^ 1].Length < destSplit.SplitInfos[^ 1].Length) { // If destination is bigger than source throw away the diff at the end (set length of dest to src) bound = sourceSplit.SplitInfos.Length; using var destStream = new FileStream(destSplit.File.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); destStream.SetLength(source.Length); } else { // they are the same length w/e bound = sourceSplit.SplitInfos.Length; } // loop to bound, check each split for equality and copy over if needed Parallel.For(0, bound, (i) => { SplitInfo infoSource = sourceSplit.SplitInfos[i]; SplitInfo infoDest = destSplit.SplitInfos[i]; using var sourceStream = new FileStream(sourceSplit.File.FullName, FileMode.Open, FileAccess.Read, FileShare.Read); using var destStream = new FileStream(destSplit.File.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); sourceStream.Position = infoSource.StartIndex; destStream.Position = infoSource.StartIndex; // if they are not the same length or not equal copy from src if (infoSource.Length != infoDest.Length || !FastCompare.Equals(sourceStream, destStream, infoSource.Length)) { sourceStream.Position = infoSource.StartIndex; destStream.Position = infoSource.StartIndex; StreamCopy(sourceStream, destStream, infoSource.Length, onWrite); } else { onWrite?.Invoke(infoSource.Length); } }); }