如何验证 Torrent Infohash 是否有效?

How to validate if torrent Infohash is valid or not?

在我的项目中,我需要检查 torrent Infohash 是否有效。

谁能建议我如何验证它?

任何 20 个字节(十六进制为 40)的序列在句法上都是有效 infohash。

infohash 是从 .torrent 文件计算的值。

  • 如果您有 .torrent 文件
  • 你可以计算它的 infohash
  • 然后确认你的infohash是否有效

.torrent 文件是一种相当简单的数据编码。它使用编码 becoding。 BEncoding 支持:

  • 整数 (i) (例如 619 → i619e
  • 位串(b)(例如"Hello, world!"→13:Hello, world!)
  • 列表 (l) (l ... e)
  • 字典 (d) (d ... e)

.torrent 以 字典 开头。字典中最重要的元素是 info 元素(尽管可以有其他元素):

{
   "info":  {
      "files": [
         { 
            "length": 1520384463,
            "path": ["Star.Trek.Picard.S01E04.Absolute.Candor.1080p.CBS.WEB-DL.AAC2.0.x264-TEPES.mkv"]
         },
         {
            "length": 689,
            "path": "[TGx]Downloaded from torrentgalaxy.to .txt",
         }
      ],
      "name": "Star.Trek.Picard.S01E04.Absolute.Candor.1080p.CBS.WEBRip.AAC2.0.x264-TEPES[TGx]",
      "piece length": 1048576,
      "pieces": [1450 SHA-1 hashes, one for each piece]
   }
 }

为了计算 info 哈希值,您需要计算 info 元素的所有内容的哈希值。

  • 获得信息的哈希后
  • 您将拥有信息哈希

实际编码

实际编码相当简单:

d
    4:info  d
        5:files l
                d
                    6:length i1520384463e
                    4:path l
                        78:Star.Trek.Picard.S01E04.Absolute.Candor.1080p.CBS.WEB-DL.AAC2.0.x264-TEPES.mkv
                    e
                e
                d
                    6:length i689e
                    4:path l
                        42:[TGx]Downloaded from torrentgalaxy.to .txt
                    e
                e
        e
        4:name 79:Star.Trek.Picard.S01E04.Absolute.Candor.1080p.CBS.WEBRip.AAC2.0.x264-TEPES[TGx]
        12:piece length i1048576e
        6:pieces 29000:0x407C5B3D7B5DBAFC556B8F9366D07A906E05256AA1B616632A40936272306114E83F00B5104C195CEC185D2E333E649913E7FD0BF734EFBAB3A010E1056AA266C5433FC0E07608D0E48AD21919079A403FE178758C0D10CAC6A22C82E4FBCBD0D1F0537B4B824E2E2E9CA88FD4ABAB4BF39E47E448755968BCA88E7D3920EE93C722028CD2B9BEB5D73624D1E9A27AD5C9D9ACBAAE5F778174EE30C990876F1CFD11167ED06D3A548F9142E70B26EE498FE09CE30944117B65D705267EB52918D34B97A1D9AD5A2A29F19AAF405078BD5243B1A027CB472B20B8F75D50DB884448B8B2DF9D6421EE8F31376BAD2186529518BB572C92A6D8D4F2D245EB73A1D419BAE36BE9B33D43805EEE7851FEE9A6C4FD5DF85B2268CB077DEDA2B62B764E4AA92DAF19AE560711C954EEF95B37A3B9F16F72FB92FEEBE8FEED44613DA653ED82AB7B085C4720AFE21EF53B1D5A1D31B5D4E03AB7B69F5BD299E391F5530E2E20868E4EEC4CE90F6511E85C088EFABEFA37A4F22F0149543952832B5486A035859B9C34F1DD9DF73A7C069C83EDA402DB0C6B9BA520925929B7E4F036ACD96EBE503A589034CC1BF5467199F4A26FC3ECF061DE648A54901673E5EFAE6A509182A58B06F72019EA6E19F26C9C9022D3A987B43A8CF38844C95694F5AD2804B02525A62B3BD33B0C256BF042D9DF03BA33FC9966D99129D0A96951D04EF267AFFFF430B8EB75D1D81509E70253DF8815396E9A695D09DDDE0737AE38B639E70E6DD1FBB9F33A57509F4727C584F9712DCD1504BE1650282E8DB52B7E8A1F2CAF769430C7C2D6501B0CB4AA45DCEB72CEB6D70AEA3A5C08BB1EDE1B9C347D88C0335A56A8D898DFA47CBE4C44284CB20CA624738CAB5BA08FCFF94A5982D4AD2A4D0A26B5D9B81F3B8241D724
    e
e