从 torrent 文件中提取 SHA1 哈希值
我已经四处寻找这个问题的答案,但我似乎只能找到可以为您做到这一点的软件。有谁知道如何在 python 中做到这一点?
I've had a look around for the answer to this, but I only seem to be able to find software that does it for you. Does anybody know how to go about doing this in python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我编写了一段 Python 代码,用于根据 .torrent 文件 中的内容验证下载文件的哈希值。假设您想检查下载的内容是否损坏,您可能会发现这很有用。
您需要 bencode 包才能使用它。 Bencode 是 .torrent 文件中使用的序列化格式。它可以像 JSON 一样整理列表、字典、字符串和数字。
该代码采用
info['pieces']
字符串中包含的哈希值:该字符串包含一系列 20 字节哈希值(每个块一个)。然后将这些哈希值与磁盘上文件片段的哈希值进行比较。
此代码中唯一复杂的部分是处理多文件 torrent,因为单个 torrent 片段可以跨越多个文件(BitTorrent 在内部将多文件下载视为单个连续文件)嗯>。我使用生成器函数
pieces_generator()
来抽象它。您可能需要阅读 BitTorrent 规范 以更详细地了解这一点。
完整代码如下:
I wrote a piece of python code that verifies the hashes of downloaded files against what's in a .torrent file. Assuming you want to check a download for corruption you may find this useful.
You need the bencode package to use this. Bencode is the serialization format used in .torrent files. It can marshal lists, dictionaries, strings and numbers somewhat like JSON.
The code takes the hashes contained in the
info['pieces']
string:That string contains a succession of 20 byte hashes (one for each piece). These hashes are then compared with the hash of the pieces of on-disk file(s).
The only complicated part of this code is handling multi-file torrents because a single torrent piece can span more than one file (internally BitTorrent treats multi-file downloads as a single contiguous file). I'm using the generator function
pieces_generator()
to abstract that away.You may want to read the BitTorrent spec to understand this in more details.
Full code bellow:
下面是我如何从 torrent 文件中提取哈希值:
它与运行命令相同:
希望,它有帮助:)
Here how I've extracted HASH value from torrent file:
It is the same as running command:
Hope, it helps :)
如果有人想知道如何从 BitTorrent v2 兼容 torrent 中提取文件哈希值,您可以使用此命令行工具。
In case if anybody wonders how to extract file hashes from BitTorrent v2 compatible torrents, you can use this command line tool.
根据 this,您应该能够通过搜索以下部分找到文件的 md5sums:数据如下:
d[...]6:md5sum32:[hash is here][...]e
(SHA 不是规范的一部分)
According to this, you should be able to find the md5sums of files by searching for the part of the data that looks like:
d[...]6:md5sum32:[hash is here][...]e
(SHA is not part of the spec)