Exemplo n.º 1
0
        internal static IBencodingType Decode(BinaryReader inputStream, ref int bytesConsumed)
        {
            char next = (char)inputStream.PeekChar();

            switch (next)
            {
            case 'i':
                // Integer
                return(BInt.Decode(inputStream, ref bytesConsumed));

            case 'l':
                // List
                return(BList.Decode(inputStream, ref bytesConsumed));

            case 'd':
                // List
                return(BDict.Decode(inputStream, ref bytesConsumed));

            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                // String
                return(BString.Decode(inputStream, ref bytesConsumed));
            }

            return(null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Decode the next token as a dictionary.
        /// Assumes the next token is a dictionary.
        /// </summary>
        /// <param name="inputStream"></param>
        /// <param name="bytesConsumed"></param>
        /// <returns>Decoded dictionary</returns>
        public static BDict Decode(BinaryReader inputStream, ref int bytesConsumed)
        {
            // Get past 'd'
            inputStream.ReadByte();

            bytesConsumed++;
            BDict res = new BDict();

            // Read elements till an 'e'
            while (inputStream.PeekChar() != 'e')
            {
                // Key
                BString key = BString.Decode(inputStream, ref bytesConsumed);

                // Value
                IBencodingType value = BencodingUtils.Decode(inputStream, ref bytesConsumed);

                res[key.Value] = value;
            }

            // Get past 'e'
            inputStream.Read();
            bytesConsumed++;

            return(res);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Calculates the InfoHash from a torrent. You must supply the "info" dictionary from the torrent.
        /// </summary>
        /// <param name="torrentInfoDict">The "info" dictionary.</param>
        /// <example>
        /// This example, will load a torrent, take the "info" dictionary out of the root dictionary and hash this.
        /// <code>
        /// BDict torrent = BencodingUtils.DecodeFile(@"torrentFile.torrent") as BDict;
        /// byte[] infoHash = BencodingUtils.CalculateTorrentInfoHash(torrent["info"] as BDict);
        /// </code>
        ///
        /// The "infoHash" byte array now contains 20 bytes with the SHA-1 infoHash.
        /// </example>
        /// <returns></returns>
        public static byte[] CalculateTorrentInfoHash(BDict torrentInfoDict)
        {
            // Take the "info" dictionary provided, and encode it
            byte[] infoBytes = EncodeBytes(torrentInfoDict);

            // Hash the encoded dictionary
            return(new SHA1CryptoServiceProvider().ComputeHash(infoBytes));
        }
Exemplo n.º 4
0
        public bool Equals(BDict obj)
        {
            Dictionary <string, IBencodingType> other = obj;

            return(Equals(other));
        }
Exemplo n.º 5
0
        public override bool Equals(object obj)
        {
            BDict other = obj as BDict;

            return(Equals(other));
        }