Example #1
0
        public void Split(string source, long sourceLength, string targetPath, Type readType, Type writeType, Type spliterType, byte numberOfPart)
        {
            if (!readType.GetInterfaces().Contains(typeof(IGenReader)))
                throw new ArgumentException("Incompatible type reader");
            if (!writeType.GetInterfaces().Contains(typeof(IGenWriter)))
                throw new ArgumentException("Incompatible type writer");
            if (!spliterType.GetInterfaces().Contains(typeof(IFileSpliter)))
                throw new ArgumentException("Incompatible type spliter");

            var writers = new IGenWriter[numberOfPart];
            using (var reader = (IGenReader)Activator.CreateInstance(readType))
            {
                reader.Open(source);
                try
                {
                    var spliter = (IFileSpliter)Activator.CreateInstance(spliterType);
                    var filename = Path.GetFileName(source);
                    var filenamePart = SplitFileName(filename, spliter, numberOfPart);
                    Stream[] targets = new Stream[numberOfPart];

                    for (byte i = 0; i < numberOfPart; i++)
                    {
                        writers[i] = (IGenWriter)Activator.CreateInstance(writeType);
                        writers[i].Open(Utils.WriteFileName(source, targetPath, i));
                        targets[i] = writers[i].Writer;
                        var header = new Header()
                        {
                            PartIndex = i,
                            FileName = filenamePart[i],
                            HashFormat = Guid.Empty,
                            PartFileHashLength = 0,
                            OriginalFileLength = sourceLength,
                            PartFileNameLength = (ushort)filenamePart[i].Length,
                            SplitFormat = spliter.Protocol,
                            TotalPartCount = numberOfPart,
                            TotalFileNameLength = (ushort)filename.Length
                        };
                        targets[i].Write(header.WriteHeader(), 0, header.TotalHeaderSize);
                    }

                    spliter.Split(targets, reader.Reader, numberOfPart);
                }
                finally
                {
                    for (byte i = 0; i < numberOfPart; i++)
                        if (writers[i] != null)
                            writers[i].Dispose();
                }

            }
        }
Example #2
0
        public void Shred(string source, Type readType, Type writeType, byte numberOfPart)
        {
            if (!readType.GetInterfaces().Contains(typeof(IGenReader)))
            {
                throw new Exception("Incompatible type reader");
            }
            if (!writeType.GetInterfaces().Contains(typeof(IGenWriter)))
            {
                throw new Exception("Incompatible type writer");
            }

            var    writer       = new IGenWriter[numberOfPart];
            string targetFolder = Path.GetDirectoryName(source);
            long   sourceLength = (new FileInfo(source)).Length; // a déplacer dans une méthode non couplée (ExtFile)

            byte[] curRead = new byte[1024];

            RandomNumberGenerator rng = RandomNumberGenerator.Create();//shouldn't be pseudo random...

            long[] polys = new long[numberOfPart];
            //polys[0] = 0;//secret place

            using (var reader = (IGenReader)Activator.CreateInstance(readType))
            {
                reader.Open(source);
                try
                {
                    for (byte i = 0; i < numberOfPart; i++)
                    {
                        writer[i] = (IGenWriter)Activator.CreateInstance(writeType);
                        writer[i].Open(writeFileName(source, targetFolder, i));
                    }
                    //écriture des metainfos en header.
                    for (byte i = 0; i < numberOfPart; i++)
                    {
                        writer[i].Write(getHeader(sourceLength, numberOfPart, i), 10);
                    }

                    int qtyRead = 0;

                    while ((qtyRead = reader.Read(ref curRead, 1024)) > 0)
                    {
                        for (int i = 0; i < qtyRead; i++)
                        {
                            polys[0] = curRead[i];
                            for (int inum = 1; inum < polys.Length; inum++)
                            {
                                polys[inum] = RandomInRange(rng, 0, RND_UPPER);
                            }


                            for (byte j = 0; j < numberOfPart; j++)
                            {
                                long ev = evalPoly(polys, j + 1);

                                /*for (int k = 0; k < polys.Length; k++)
                                 *  Console.Write(polys[k] + "|");
                                 * Console.WriteLine(ev);*/

                                byte[] share = BitConverter.GetBytes(ev); //Bitev.ToByteArray(); // BitConverter.GetBytes(ev);

                                //byte[] paddedShare = new byte[STORE_BYTE_SIZE];

                                //share.CopyTo(paddedShare, 0);
                                //if (share.Length != STORE_BYTE_SIZE)
                                //    Console.WriteLine("not 16 bytes bigint detected >" + new long(share) +"><"+ new long(paddedShare));

                                writer[j].Write(share, share.Length);
                            }
                        }
                    }
                }
                finally
                {
                    for (byte i = 0; i < numberOfPart; i++)
                    {
                        if (writer != null)
                        {
                            writer[i].Dispose();
                        }
                    }
                }
            }
        }