Exemplo n.º 1
0
        /// <summary>
        /// Computes the "info_hash" of the provided torrent (BDictionary).
        /// </summary>
        /// <param name="torrent">The BDicionary containing the torrent file.</param>
        /// <returns>An InfoHash object with the SHA1 hash.</returns>
        public static InfoHash ComputeInfoHash(BDictionary torrent)
        {
            IBValue info = null;

            //looks for the "info" dictionary
            foreach (KeyValuePair <string, IBValue> item in torrent.Items)
            {
                if (item.Key == "info" && item.Value is BDictionary)
                {
                    info = item.Value;
                    break;
                }
            }

            //if found, then computes the SHA1 hash and returns it
            if (info != null)
            {
                //the info_hash is the sha1 hash of the bencoded "info" dictionary, so gets it
                string      bencoded = info.ToBEncodedString();
                List <byte> bytes    = new List <byte>(bencoded.Length);

                //adds its bytes to a list to be used in the ComputeHash function
                foreach (char c in info.ToBEncodedString())
                {
                    bytes.Add((byte)c);
                }

                SHA1 sha1 = SHA1.Create();
                return(InfoHash.FromByteArray(sha1.ComputeHash(bytes.ToArray())));
            }

            //if the "info" dictionary is not found, then returns an empty InfoHash to avoid exceptions...
            return(InfoHash.FromByteArray(new byte[] { }));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Serializes an IBvalue to the provided stream.
        /// </summary>
        /// <param name="value">The IBValue to be serialized.</param>
        /// <param name="stream">The Stream to receive the serialized data.</param>
        public static void Save(IBValue value, Stream stream)
        {
            BinaryWriter bw = new BinaryWriter(stream);

            foreach (char c in value.ToBEncodedString())
            {
                bw.Write((byte)c);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Serializes an IBvalue to the provided stream.
        /// </summary>
        /// <param name="value">The IBValue to be serialized.</param>
        /// <param name="stream">The Stream to receive the serialized data.</param>
        public static void Save(IBValue value, Stream stream)
        {
            BinaryWriter bw = new BinaryWriter(stream);

            foreach (char c in value.ToBEncodedString())
            {
                bw.Write((byte)c);
            }
        }