Ejemplo n.º 1
0
 public override bool Equals(object obj)
 {
     if (obj is Sha1Hash)
     {
         Sha1Hash other = (Sha1Hash)obj;
         return(Enumerable.SequenceEqual(Value, other.Value));
     }
     else
     {
         return(false);
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Metainfo"/> class.
 /// </summary>
 /// <param name="name">The name of the torrent.</param>
 /// <param name="infoHash">SHA-1 hash of the metadata.</param>
 /// <param name="files">List of files to include.</param>
 /// <param name="pieces">List of pieces to include.</param>
 /// <param name="trackers">URLs of the trackers.</param>
 /// <param name="metadata">Info section of the metadata file.</param>
 public Metainfo(string name,
                 Sha1Hash infoHash,
                 IEnumerable <ContainedFile> files,
                 IEnumerable <Piece> pieces,
                 IEnumerable <IEnumerable <Uri> > trackers,
                 IReadOnlyList <byte> metadata)
 {
     Name     = name;
     _pieces  = new ReadOnlyCollection <Piece>(pieces.ToList());
     InfoHash = infoHash;
     Files    = new List <ContainedFile>();
     Files.AddRange(files);
     TotalSize = Files.Any() ? Files.Sum(f => f.Size) : 0;
     Trackers  = trackers.Select(x => (IReadOnlyList <Uri>) new ReadOnlyCollection <Uri>(x.ToList())).ToList().AsReadOnly();
     PieceSize = _pieces.First().Size;
     Metadata  = metadata;
 }
Ejemplo n.º 3
0
        public static IList <Piece> ComputePieces(IList <ContainedFile> files, int pieceSize, IFileHandler fileHandler)
        {
            var pieces = new List <Piece>();

            if (files.Count == 0)
            {
                return(pieces);
            }

            long totalLength = files.Sum(f => f.Size);

            int    currentFile  = 0;
            Stream fileStream   = fileHandler.GetFileStream(files[currentFile].Name);
            long   offset       = 0;
            int    pieceCounter = 0;

            using (var sha1 = SHA1.Create())
            {
                // Full pieces
                while (offset <= totalLength - pieceSize && totalLength - pieceSize > 0)
                {
                    byte[] data = GetBlockData(fileHandler, files, pieceSize, ref currentFile, ref fileStream);

                    Sha1Hash pieceHash = new Sha1Hash(sha1.ComputeHash(data));
                    Piece    piece     = new Piece(pieceCounter, pieceSize, pieceHash);
                    pieces.Add(piece);
                    offset += pieceSize;
                    pieceCounter++;
                }

                // Remaining smaller piece
                long remaining = totalLength - offset;
                if (remaining > 0)
                {
                    byte[] remainingData = GetBlockData(fileHandler, files, remaining, ref currentFile, ref fileStream);

                    Sha1Hash pieceHash = new Sha1Hash(sha1.ComputeHash(remainingData));
                    Piece    piece     = new Piece(pieceCounter, (int)remaining, pieceHash);
                    pieces.Add(piece);
                }
            }

            return(pieces);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Piece"/> class
 /// with the specified index, size, files and hash.
 /// </summary>
 /// <param name="index">Zero-based index of the piece.</param>
 /// <param name="size">Size of the piece, in bytes.</param>
 /// <param name="hash">Hash for the piece.</param>
 public Piece(int index, int size, Sha1Hash hash)
 {
     Index = index;
     Size  = size;
     Hash  = hash;
 }
Ejemplo n.º 5
0
 static Sha1Hash()
 {
     Empty = new Sha1Hash(new byte[Length]);
 }