示例#1
0
        public static bool CheckTorrentIntegrity(string torrentPath, string downloadPath, CTICB cb)
        {
            CTITryReportStatus(cb, CTICreateParsingStatus());
            TorrentParser torrentParser = new TorrentParser(File.ReadAllBytes(torrentPath));

            for (int i = 0; i < torrentParser.pieceHashes.Length; i++)
            {
                CTITryReportStatus(cb, CTICreateProcessingPieceStatus(i + 1, torrentParser.pieceHashes.Length));
                byte[] piece     = torrentParser.GetPieceFor(torrentParser.tree, i, downloadPath);
                SHA1   sha1      = SHA1.Create();
                string pieceHash = torrentParser.pieceHashes[i];
                string hash      = B2S(sha1.ComputeHash(piece));

                if (!pieceHash.Equals(hash))
                {
                    return(false);
                }
            }

            return(true);
        }
示例#2
0
        public static bool CheckTorrentIntegrityV2(string torrentPath, string downloadPath, CTICB cb)
        {
            CTITryReportStatus(cb, CTICreateParsingStatus());
            TorrentParser torrentParser    = new TorrentParser(File.ReadAllBytes(torrentPath));
            JObject       _tree            = torrentParser.tree;
            int           pieceLength      = (int)_tree["info"]["piece length"];
            JArray        files            = (JArray)_tree["info"]["files"];
            string        name             = (string)_tree["info"]["name"];
            int           pieceHashesIndex = 0;

            CTITryReportStatus(cb, CTICreateProcessingPieceStatus(1, torrentParser.pieceHashes.Length));

            if (files != null)
            {
                int         leftToRead = pieceLength;
                List <byte> piece      = new List <byte>();

                for (int i = 0; i < files.Count; i++)
                {
                    JObject    file       = (JObject)files[i];
                    JArray     path       = (JArray)file["path"];
                    FileStream fileStream = new FileStream(ResolvePath(downloadPath, name, path), FileMode.Open);
                    long       rest       = fileStream.Length;

                    while (rest >= leftToRead)
                    {
                        byte[] buffer = new byte[leftToRead];
                        fileStream.Read(buffer, 0, leftToRead);
                        rest = (fileStream.Length - fileStream.Position);
                        piece.AddRange(buffer);
                        string pieceHash = torrentParser.pieceHashes[pieceHashesIndex];
                        pieceHashesIndex++;
                        SHA1   sha1 = SHA1.Create();
                        string hash = B2S(sha1.ComputeHash(piece.ToArray()));
                        piece.Clear();

                        if (!pieceHash.Equals(hash))
                        {
                            return(false);
                        }

                        CTITryReportStatus(cb, CTICreateProcessingPieceStatus(pieceHashesIndex + 1, torrentParser.pieceHashes.Length));
                        leftToRead = pieceLength;
                    }

                    if (rest != 0)
                    {
                        leftToRead = leftToRead - (int)rest;
                        byte[] buffer = new byte[rest];
                        fileStream.Read(buffer, 0, (int)rest);
                        piece.AddRange(buffer);
                        rest = 0;

                        if (i == files.Count - 1)
                        {
                            Console.WriteLine(pieceLength);
                            Console.WriteLine(buffer.Length);
                            string pieceHash = torrentParser.pieceHashes[pieceHashesIndex];
                            pieceHashesIndex++;
                            SHA1   sha1 = SHA1.Create();
                            string hash = B2S(sha1.ComputeHash(piece.ToArray()));
                            piece.Clear();

                            if (!pieceHash.Equals(hash))
                            {
                                return(false);
                            }
                        }
                    }

                    fileStream.Dispose();
                }

                return(true);
            }
            else
            {
                FileStream fileStream = new FileStream(Path.Combine(downloadPath, (string)_tree["info"]["name"]), FileMode.Open);
                byte[]     piece      = new byte[pieceLength];
                int        read       = 0;

                while ((read = fileStream.Read(piece, 0, pieceLength)) == pieceLength)
                {
                    if (read != pieceLength)
                    {
                        byte[] tmp = new byte[read];
                        Array.Copy(piece, tmp, read);
                        piece = tmp;
                    }

                    string pieceHash = torrentParser.pieceHashes[pieceHashesIndex];
                    pieceHashesIndex++;
                    SHA1   sha1 = SHA1.Create();
                    string hash = B2S(sha1.ComputeHash(piece));

                    if (!pieceHash.Equals(hash))
                    {
                        return(false);
                    }

                    if (read == pieceLength)
                    {
                        CTITryReportStatus(cb, CTICreateProcessingPieceStatus(pieceHashesIndex + 1, torrentParser.pieceHashes.Length));
                    }
                }

                return(true);
            }
        }