Example #1
0
        ///<summary>
        ///calculates all hashes over the files which should be included in the torrent
        ///</summmary>
        byte[] CalcPiecesHash(string path, TorrentFile[] files, PieceWriter writer)
        {
            var piecesBuffer       = new byte[GetPieceCount(files) * 20];     //holds all the pieces hashes
            var piecesBufferOffset = 0;

            var totalLength = Toolbox.Accumulate <TorrentFile>(files, delegate(TorrentFile f) { return(f.Length); });
            var buffer      = new ArraySegment <byte>(new byte[PieceLength]);

            while (totalLength > 0)
            {
                var bytesToRead = (int)Math.Min(totalLength, PieceLength);
                var io          = new BufferedIO(null, buffer, (piecesBufferOffset / 20) * PieceLength, bytesToRead, bytesToRead, files, path);
                totalLength -= writer.ReadChunk(io);

                // If we are using the synchronous version, result is null
                if (result != null && result.Aborted)
                {
                    return(piecesBuffer);
                }

                var currentHash = hasher.ComputeHash(buffer.Array, 0, io.ActualCount);
                RaiseHashed(new TorrentCreatorEventArgs(0,
                                                        0,
                                                        //reader.CurrentFile.Position, reader.CurrentFile.Length,
                                                        piecesBufferOffset * PieceLength,
                                                        (piecesBuffer.Length - 20) * PieceLength));
                Buffer.BlockCopy(currentHash, 0, piecesBuffer, piecesBufferOffset, currentHash.Length);
                piecesBufferOffset += currentHash.Length;
            }
            return(piecesBuffer);
        }
Example #2
0
        byte [] CalcPiecesHash(List <TorrentFile> files, PieceWriter writer)
        {
            byte []     buffer        = null;
            int         bufferRead    = 0;
            long        fileRead      = 0;
            long        overallRead   = 0;
            long        overallTotal  = 0;
            MD5         md5Hasher     = null;
            SHA1        shaHasher     = null;
            List <byte> torrentHashes = null;

            shaHasher     = HashAlgoFactory.Create <SHA1> ();
            torrentHashes = new List <byte> ();
            overallTotal  = Toolbox.Accumulate <TorrentFile> (files, delegate(TorrentFile m) { return(m.Length); });

            long pieceLength = PieceLength;

            buffer = new byte [pieceLength];

            if (StoreMD5)
            {
                md5Hasher = HashAlgoFactory.Create <MD5> ();
            }

            try {
                foreach (TorrentFile file in files)
                {
                    fileRead = 0;
                    if (md5Hasher != null)
                    {
                        md5Hasher.Initialize();
                    }

                    while (fileRead < file.Length)
                    {
                        int toRead = (int)Math.Min(buffer.Length - bufferRead, file.Length - fileRead);
                        int read   = writer.Read(file, fileRead, buffer, bufferRead, toRead);
                        cts?.Cancel();

                        if (md5Hasher != null)
                        {
                            md5Hasher.TransformBlock(buffer, bufferRead, read, buffer, bufferRead);
                        }
                        shaHasher.TransformBlock(buffer, bufferRead, read, buffer, bufferRead);

                        bufferRead  += read;
                        fileRead    += read;
                        overallRead += read;

                        if (bufferRead == buffer.Length)
                        {
                            bufferRead = 0;
                            shaHasher.TransformFinalBlock(buffer, 0, 0);
                            torrentHashes.AddRange(shaHasher.Hash);
                            shaHasher.Initialize();
                        }
                        RaiseHashed(new TorrentCreatorEventArgs(file.Path, fileRead, file.Length, overallRead, overallTotal));
                    }
                    if (md5Hasher != null)
                    {
                        md5Hasher.TransformFinalBlock(buffer, 0, 0);
                        md5Hasher.Initialize();
                        file.MD5 = md5Hasher.Hash;
                    }
                }
                if (bufferRead > 0)
                {
                    shaHasher.TransformFinalBlock(buffer, 0, 0);
                    torrentHashes.AddRange(shaHasher.Hash);
                }
            } finally {
                if (shaHasher != null)
                {
                    shaHasher.Clear();
                }
                if (md5Hasher != null)
                {
                    md5Hasher.Clear();
                }
            }
            return(torrentHashes.ToArray());
        }