我该如何阅读 BitTorrent 作品?

发布于 2024-09-24 17:41:51 字数 210 浏览 5 评论 0原文

我目前正在为 Ruby 开发一个 torrent 元信息管理库。

我在读取文件中的片段时遇到问题。我只是不明白我该怎么做。我知道我应该对文件的片段长度字节进行一次SHA1摘要(或多次读取片段长度字节,或者什么?)

我指望你的帮助。 伪/Python/Ruby/PHP 代码优先。

提前致谢。

I'm currently developing a torrent metainfo management library for Ruby.

I'm having trouble reading the pieces from the files. I just don't understand how I'm supposed to go about it. I know I'm supposed to SHA1 digest piece length bytes of a file once (or read piece length bytes multiple times, or what?)

I'm counting on your help.
Pseudo / Python / Ruby / PHP code preferred.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑凤梨 2024-10-01 17:41:51

C#

// Open the file
using (var file = File.Open(...))
{
    // Move to the relevant place in the file where the piece begins
    file.Seek(piece * pieceLength, SeekOrigin.Begin);

    // Attempt to read up to pieceLength bytes from the file into a buffer
    byte[] buffer = new byte[pieceLength];
    int totalRead = 0;
    while (totalRead < pieceLength)
    {
        var read = stream.Read(buffer, totalRead, pieceLength-totalRead);
        if (read == 0)
        {
            // the piece is smaller than the pieceLength,
            // because it’s the last in the file
            Array.Resize(ref buffer, totalRead);
            break;
        }
        totalRead += read;
    }

    // If you want the raw data for the piece:
    return buffer;

    // If you want the SHA1 hashsum:
    return SHA1.Create().ComputeHash(buffer);
}

C#

// Open the file
using (var file = File.Open(...))
{
    // Move to the relevant place in the file where the piece begins
    file.Seek(piece * pieceLength, SeekOrigin.Begin);

    // Attempt to read up to pieceLength bytes from the file into a buffer
    byte[] buffer = new byte[pieceLength];
    int totalRead = 0;
    while (totalRead < pieceLength)
    {
        var read = stream.Read(buffer, totalRead, pieceLength-totalRead);
        if (read == 0)
        {
            // the piece is smaller than the pieceLength,
            // because it’s the last in the file
            Array.Resize(ref buffer, totalRead);
            break;
        }
        totalRead += read;
    }

    // If you want the raw data for the piece:
    return buffer;

    // If you want the SHA1 hashsum:
    return SHA1.Create().ComputeHash(buffer);
}
影子是时光的心 2024-10-01 17:41:51

请在此处查看此发行版:

http://prdownload.berlios.de /torrentparse/TorrentParse.GTK.0.21.zip

用 PHP 编写,它包含一个编码器和解码器以及我相信的输入和输出!

Please taker a look at this distribution here:

http://prdownload.berlios.de/torrentparse/TorrentParse.GTK.0.21.zip

Written in PHP, it contains an Encoder and Decoder and the in's and out I believe!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文