Example #1
0
 internal Piece(byte[] data, byte[] id, uint rounds)
 {
     Data              = data;
     ID                = id;
     RedundancyIndex   = rounds;
     OriginalID        = HashSingleton.Compute(Data);
     OurResponsibility = true;
 }
Example #2
0
        public byte[] GenerateID()
        {
            var ID = HashSingleton.ComputeSerialized(this);

            // current Chordette peer ID coding:
            // 4 bytes IPv4 address
            // 2 bytes TCP listening port
            var offset = 0;

            Array.Copy(Address.GetAddressBytes(), 0, ID, offset, 4);
            Array.Copy(BitConverter.GetBytes(Port), 0, ID, offset + 4, 2);

            return(ID);
        }
Example #3
0
        public static (FileDescriptor, List <byte[]>) FromFile(Stream file, string name, int chunk_size = 65000)
        {
            var pieces     = new List <byte[]>();
            var hasher     = HashSingleton.Duplicate();
            var descriptor = new FileDescriptor()
            {
                Name             = name,
                Length           = file.Length,
                Created          = DateTime.UtcNow,
                Checksum         = new byte[hasher.HashSize / 8],
                PieceIdentifiers = new List <byte[]>(),
                MimeType         = MimeMapper.MapToMime(name)
            };

            byte[] buffer = new byte[chunk_size];

            while (file.Position < file.Length)
            {
                var read  = file.Read(buffer, 0, chunk_size);
                var chunk = new byte[read];

                Array.Copy(buffer, 0, chunk, 0, read);

                if (file.Position == file.Length)
                {
                    hasher.TransformFinalBlock(buffer, 0, read);
                }
                else
                {
                    hasher.TransformBlock(buffer, 0, read, buffer, 0);
                }

                pieces.Add(chunk);
                descriptor.PieceIdentifiers.Add(HashSingleton.Compute(chunk));
            }

            descriptor.Checksum = hasher.Hash;

            return(descriptor, pieces);
        }
Example #4
0
 public Piece(byte[] data, uint rounds = 1) :
     this(data, HashSingleton.ComputeRounds(data, rounds), rounds)
 {
 }