如何使用C#convert .torrent来制作磁铁? [英] how to use C# convert .torrent to magnet?

查看:98
本文介绍了如何使用C#convert .torrent来制作磁铁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用C#转换.torrent到磁力链接?

有什么例子吗?

谢谢!

how to use C# convert .torrent to magnet link?
have something example?
thank you!

推荐答案

C#将.torrent转换为磁力链接

如果我是正确的,你想在C#中实现磁力链接,就像各种网站上的种子一样。



现在,为此,您所要做的就是从该位置获取torrent文件(torrent URL)并使用它来启动使用torrent文件关联的默认应用程序。

使用 Process.Start



有用的链接: MSDN:Process.Start方法 [ ^ ]


这是我的解决方案:



使用的库:

- NBEncode

- Base32



Here is my solution:

libraries which were used:
-NBEncode
-Base32

static class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 1)
            {
                var inFileStream = new FileStream(args[0], FileMode.Open);
				
                var transform = new BObjectTransform();
				BDictionary bObject = transform.DecodeNext(inFileStream) as BDictionary;
	            IBObject info = bObject.FindSection("info");
	            string fileName = ((info as BDictionary).FindSection("name") as BByteString).ConvertToText(Encoding.ASCII);
	            byte[] infoData = info.ToByteArray();
	            string hashString = Sha1Encode(infoData);
	            string magnetLink = string.Format("magnet:?xt=urn:btih:{0}&db={1}", hashString, fileName);
				Console.WriteLine(magnetLink);

            }
            else
            {
                Console.WriteLine("\nSyntax is:\nOSS.SampleApp.exe <torrent file="" path="">");
            }
        }

	    private static string Sha1Encode(byte[] bytes)
	    {
		    using (SHA1 hasher = new SHA1Managed())
		    {
				return Base32Encoder.Encode(hasher.ComputeHash(bytes));
		    }
	    }

		public static IBObject FindSection(this BDictionary dictionary, string sectionName)
		{
			if (dictionary == null)
				throw new ArgumentNullException();

			foreach (KeyValuePair<bbytestring,> keyValuePair in dictionary.Value)
			{
				string key = keyValuePair.Key.ConvertToText(Encoding.ASCII);
				if (key == sectionName)
					return keyValuePair.Value;
			}

			return null;
		}

		public static byte[] ToByteArray(this IBObject obj)
		{
			using (MemoryStream MS = new MemoryStream())
			{
				var transform = new BObjectTransform();
				transform.EncodeObject(obj, MS);
				MS.Position = 0;
				return MS.ToArray();
			}
		}
    }


不太复杂。

i已经解决了这个问题。< br $>


使用bencode(C#)。

bencode元文件,然后sha1(bencode(元数据['info']))



感谢Sandeep Mewara:)
less complex.
i have solved the question.

use bencode(C#).
bencode the metafile,then sha1(bencode(metadata['info']))

thanks Sandeep Mewara :)


这篇关于如何使用C#convert .torrent来制作磁铁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆