Ejemplo n.º 1
0
        private static string HashElf(ElfHeader elf, long basePosition, Stream input)
        {
            using (var sha1 = SHA1.Create())
            {
                foreach (var program in elf.ProgramHeaders)
                {
                    var type  = (uint)program.Type;
                    var flags = (uint)program.Flags;

                    sha1.TransformBlock(BitConverter.GetBytes(type.Swap()), 0, 4, null, 0);
                    sha1.TransformBlock(BitConverter.GetBytes(flags.Swap()), 0, 4, null, 0);

                    if (program.Type == Elf.ProgramType.Loadable && program.MemorySize > 0)
                    {
                        sha1.TransformBlock(BitConverter.GetBytes(program.VirtualAddress.Swap()), 0, 8, null, 0);
                        sha1.TransformBlock(BitConverter.GetBytes(program.MemorySize.Swap()), 0, 8, null, 0);

                        if (program.FileSize > 0)
                        {
                            if (basePosition + program.FileOffset + program.FileSize > input.Length)
                            {
                                return(null);
                            }

                            input.Position = basePosition + program.FileOffset;
                            var programBytes = input.ReadBytes((int)program.FileSize);
                            sha1.TransformBlock(programBytes, 0, programBytes.Length, null, 0);
                        }
                    }
                }
                sha1.TransformFinalBlock(new byte[0], 0, 0);
                return(BitConverter.ToString(sha1.Hash).Replace("-", "").ToLowerInvariant());
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets SHA1 implementation.
        /// </summary>
        /// <returns><see cref="Sha1"/>.</returns>
        public static Sha1 GetSha1Provider()
        {
#if NetCore
            return(Sha1.Create());
#else
            return(new Sha1());
#endif
        }
Ejemplo n.º 3
0
        public static string ComputeCodeHash(this byte[] code)
        {
            var sha1 = SHA1.Create();

            sha1.TransformFinalBlock(code, 0, code.Length);
            var digest = sha1.Hash;

            return(BitConverter.ToString(digest).Replace("-", "").ToLower());
        }
Ejemplo n.º 4
0
        public void Imorted_sha1_algo_should_generate_same_hash_as_mscorelib_version()
        {
            byte[] source       = Encoding.UTF8.GetBytes("hashmeplease");
            var    mscorlibSha1 = MsCoreLibSha1.Create();
            var    importedSha1 = SHA1.Create();

            var mscorelibHash = mscorlibSha1.ComputeHash(source);
            var importedHash  = importedSha1.ComputeHash(source);

            mscorelibHash.Should().BeEquivalentTo(importedHash);
        }
Ejemplo n.º 5
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);
            }
        }