Bittorrent种子特征提取算法【Bencode编码】

伪代码算法

$torrentArray = Bencode::load($torrentFile);
$info_hash = sha1(Bencode::encode($torrentArray['info']));
$pieces_hash = sha1($torrentArray['info']['pieces']);

echo $info_hash . BR;
echo $pieces_hash . BR;
echo $torrentArray['info']['name'] . BR;
// 文件结构判断
if ( isset($torrentArray['info']['files']) ) {
    // 多文件
    $files = $torrentArray['info']['files'];
    $length = 0;
    foreach ($torrentArray['info']['files'] as $k => $v) {
        $length = $length + $v['length'];
    }
    $torrentInfo['length'] = $length;
    $torrentInfo['files'] = $files;
    // 计算files_hash
    $files_hash = sha1(Bencode::encode($torrentArray['info']['files']));
    echo '多文件' . BR;
} else {
    // 单文件
    $torrentInfo['length'] = $torrentArray['info']['length'];
    // 计算files_hash
    $Temp = array();
    $fileInfoArray = pathinfo($torrentArray['info']['name']);
    $Temp['extension'] = isset($fileInfoArray['extension']) && $fileInfoArray['extension'] ? $fileInfoArray['extension'] : 'reseed.iyuu.cn';
    $Temp['length'] = $torrentArray['info']['length'];
    $files_hash = sha1(Bencode::encode($Temp));
    echo '单文件' . BR;
}
echo $torrentInfo['length'] . BR;
echo $files_hash . BR;

$torrentInfo['site'] = '';          // 站点
$torrentInfo['torrent_id'] = '';    // 种子ID
$torrentInfo['h1'] = '';            // 主标题
$torrentInfo['title'] = '';         // 副标题
$torrentInfo['info_hash'] = '';     // 算法:sha1(info)
$torrentInfo['name'] = '';          // 种子name
$torrentInfo['files'] = '';         // 种子files
$torrentInfo['length'] = '';        // 种子size
$torrentInfo['files_hash'] = '';    // 算法:多文件sha1(files),单文件sha1(extension+length)
$torrentInfo['pieces_hash'] = '';   // 算法:sha1(pieces)

执行结果

root@ds3617xs:~# /volume1/@appstore/PHP7.3/usr/local/bin/php73 /volume1/ssd/phpspider/app/bc.php
-------------------存在种子:104010.torrent

2850f93f9840b839be79dc0e3b6d6df39677a41a
6706d070f37bac9c368336e7de8345b25ab5807d
周杰伦-《不该》.mp4
单文件
105925633
2a05b59e63d2565079c312e265d331c3c001c698

root@ds3617xs:~# /volume1/@appstore/PHP7.3/usr/local/bin/php73 /volume1/ssd/phpspider/app/bc.php
-------------------存在种子:10401.torrent

ebee680e65a845bad9572bcb101cb7a26c5ddf7a
25b70b5d56b1202e4b3a960843cbf9714e46e8da
哥谭.第1季.Gotham.S01.1080p.Blu-Ray.AC3.x265.10bit-Yumi
多文件
41146119681
d8f75b77a2dff75a04ba827875c2849672d4e410
最后修改:2019 年 12 月 22 日 02 : 50 AM
如果觉得我的文章对你有用,请随意赞赏

发表评论