This is my attempt at creating a PHP-solution for BEP 35 - torrent signing using openssl extensions.
https://www.bittorrent.org/beps/bep_0035.html
Updated: June 20, 2022
https://www.bittorrent.org/beps/bep_0035.html
Updated: June 20, 2022
<?
// BEP_35 - Torrent Signing
function signtorrent(string $in, string $out, string $certfile, string $keyfile): bool
{
$parsed = Bencode::decode(file_get_contents($in), bigInt: BigInt::INTERNAL);
if (!isset($array['signatures'])) {
$certFile = file_get_contents($certfile);
$keyFile = file_get_contents($keyfile);
$result = openssl_x509_parse($certFile);
$keyPassphrase = 'password';
$privatekey = openssl_get_privatekey($keyFile, $keyPassphrase);
openssl_sign($parse, $signature, $privatekey);
$info = array();
$fields = $result['issuer']['C'] . $result['issuer']['ST'] . $result['issuer']['L'] . $result['issuer']['O'] . $result['issuer']['OU'] . $result['validFrom'] . $result['validTo'] . $result['subject']['O'] . $result['subject']['CN'];
$signatures = array(
'signatures' => array(
'com.bittorrent' => array(
'certificate' => $fields . $signature, // optional, DER encoded x.509 certificate
'info' => $info, // optional
'signature' => sha1(Bencode::encode($parsed['info'])) . (!empty($info) ? Bencode::encode($info) : '') // signed data = info + sig info if present
)
)
);
if (isset($array['url-list'])) {
unset($array['url-list']);
}
$array = array_merge($parsed, $signatures);
if (file_put_contents($out, Bencode::encode($array))) {
return true;
}
}
return false;
}
Last edited by STorrents2020 on 2022-06-20 05:53:49