/// <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[] { })); }
/// <summary> /// Private method that parses and returns the dictionary in the current position of the stream. /// </summary> /// <param name="br">The BinaryReader with the data to parse.</param> /// <returns>The dictionary parsed.</returns> /// <remarks>If the current position does not contain data similar to "dBencoded_stringBencoded_elemente", which is a bencoded dictionary, an exception will be thrown.</remarks> private static BDictionary ParseDictionary(BinaryReader br) { StringBuilder buffer = new StringBuilder(1024); char c = (char)br.ReadByte(); if (c != 'd') { throw new InvalidCharacterException(c, "'d'"); } BDictionary dictionary = new BDictionary(); do { string key = ParseString(br); IBValue value = Parse(br); dictionary.Add(key, value); } while ((char)br.PeekChar() != 'e'); br.ReadByte(); //skips the 'e' char return(dictionary); }
/// <summary> /// Serializes an IBValue to the provided file path. /// </summary> /// <param name="value">The IBValue to be serialized.</param> /// <param name="path">The destination path.</param> public static void Save(IBValue value, string path) { using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write)) { Save(value, fs); fs.Close(); } }
/// <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); } }
/// <summary> /// Adds an object that implements the IBValue interface to the end of this dictionary. /// </summary> /// <param name="key">The key of the element to add.</param> /// <param name="value">The IBValue element to add.</param> public void Add(string key, IBValue value) { this.items.Add(new KeyValuePair <string, IBValue>(key, value)); }
/// <summary> /// Adds a new item to this list object. /// </summary> /// <param name="value">The IBValue object to add.</param> public void Add(IBValue value) { this.items.Add(value); }