Esempio n. 1
0
        private static InfoHash ComputeInfoHash(BEncoding.Dictionary info)
        {
            using (var stream = new MemoryStream())
            {
                BEncoding.Encode(info, stream);

                stream.Seek(0L, SeekOrigin.Begin);
                byte[] hash = HashHelper.ComputeSHA1(stream);
                return(new InfoHash(hash));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Saves this torrent information to a stream.
        /// </summary>
        /// <param name="stream">The stream to save to.</param>
        public void SaveToStream(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            else if (!stream.CanWrite)
            {
                throw new ArgumentException("The stream cannot be written to.", "stream");
            }

            var torrentInfo = Save();

            BEncoding.Encode(torrentInfo, stream);
        }
Esempio n. 3
0
        public Torrent(string name,
                       string downloadDir,
                       List <FileItem> files,
                       List <string> trackers,
                       int pieceSize,
                       byte[] pieceHashes = null,
                       int blockSize      = 16384,
                       bool?isPrivate     = false)
        {
            Name = name;
            DownloadDirectory = downloadDir;
            Files             = files;

            PieceSize = pieceSize;
            BlockSize = blockSize;
            IsPrivate = isPrivate;

            fileWriteLocks = new object[Files.Count];
            for (var i = 0; i < Files.Count; ++i)
            {
                fileWriteLocks[i] = new object();
            }

            if (trackers != null)
            {
                foreach (var url in trackers)
                {
                    var tracker = new Tracker(url);
                    Trackers.Add(tracker);
                    tracker.PeerListUpdated += HandlePeerListUpdated;
                }
            }

            var piecesCount = Convert.ToInt32(Math.Ceiling(TotalSize / Convert.ToDouble(PieceSize)));

            PieceHashes     = new byte[piecesCount][];
            IsPieceVerified = new bool[piecesCount];
            IsBlockAcquired = new bool[piecesCount][];

            for (var i = 0; i < PieceCount; ++i)
            {
                IsBlockAcquired[i] = new bool[GetBlockCount(i)];
            }

            if (pieceHashes != null)
            {
                for (var i = 0; i < PieceCount; ++i)
                {
                    PieceHashes[i] = GetHash(i);
                }
            }
            else
            {
                for (var i = 0; i < PieceCount; ++i)
                {
                    PieceHashes[i] = new byte[20];
                    Buffer.BlockCopy(pieceHashes, i * 20, PieceHashes[i], 0, 20);
                }
            }

            object info = TorrentInfoToBEncodingObject(this);

            byte[] bytes = BEncoding.Encode(info);
            InfoHash = SHA1.Create().ComputeHash(bytes);

            for (var i = 0; i < PieceCount; ++i)
            {
                CheckIntegrityOfPiece(i);
            }
        }