CreateTorrent

class CreateTorrent(storage: FileStorage, v1Only: Boolean = false, v2Only: Boolean = false)

Builds a .torrent file from a set of files, piece geometry and pre-computed piece hashes -- pure-Kotlin port of libtorrent's create_torrent (src/create_torrent.cpp, include/libtorrent/create_torrent.hpp).

In libtorrent a torrent is created in four steps:

  1. determine the files that will be part of the torrent,

  2. set torrent properties (trackers, web seeds, DHT nodes ...),

  3. read all file data, SHA-1/SHA-256 it and set the piece hashes,

  4. bencode the result into a buffer or file.

Step 3 (reading and hashing bytes off disk) is intentionally out of scope of the KiteTorrent core, which has no filesystem access in commonMain. Instead the caller supplies the already-computed hashes:

exactly as libtorrent's set_piece_hashes() helper would feed them in. Once the hashes are set, generate produces the bencode tree and generateBuffer the encoded bytes.

v1 / v2 / hybrid

Like libtorrent, the "flavour" produced depends on the flags and which hashes were set:

  • construct with v1Only = true to force a v1-only torrent (setting v2 hashes is then an error),

  • construct with v2Only = true to force a v2-only torrent (setting v1 hashes is then an error),

  • otherwise, if both v1 and v2 hashes are fully set a hybrid torrent is produced; if only one side's hashes are set, that flavour is produced.

A SHA-1/SHA-256 hash of all zeros is the "unset" sentinel: setting such a hash is ignored, and a torrent whose hashes are not all set will not generate that flavour (mirrors validate_v1_hashes / validate_v2_hashes).

Files

Files are added through the constructor's FileStorage (build it directly) or, more conveniently, through a CreateTorrent.Builder. For a single-file torrent the one file's path is the torrent name. For a multi-file torrent every file's path begins with the torrent's root-directory name; in the generated files[] list the leading (root) component is deliberately dropped, exactly like libtorrent.

Parameters

storage

the files and piece geometry to encode. Must contain at least one file and a non-zero total size.

v1Only

force a v1-only torrent (reject v2 hashes). Mutually exclusive with v2Only.

v2Only

force a v2-only torrent (reject v1 hashes). Mutually exclusive with v1Only.

Constructors

Link copied to clipboard
constructor(storage: FileStorage, v1Only: Boolean = false, v2Only: Boolean = false)

Types

Link copied to clipboard
class Builder(name: String, pieceLength: Int)

Fluent builder that collects files (path + size) and the torrent name, then produces a CreateTorrent. This is the convenience layer over constructing a FileStorage directly, analogous to libtorrent's add_files + file_storage feeding create_torrent.

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

The "creation date" field, a POSIX timestamp in seconds. null (the default) omits the field; libtorrent instead defaults to the wall clock at construction time, but core code has no clock, so omission is the honest default. Set it explicitly via creationDate / setCreationDate.

Functions

Link copied to clipboard

Adds a BEP 38 collection name.

Link copied to clipboard

Adds an HTTP seed (httpseeds).

Link copied to clipboard
fun addNode(host: String, port: Int): CreateTorrent

Adds a DHT bootstrap node as a host/port pair (nodes).

Link copied to clipboard

Adds a BEP 38 "similar torrent" by info-hash.

Link copied to clipboard
fun addTracker(url: String, tier: Int = 0): CreateTorrent

Adds a tracker. Duplicate URLs are ignored. The tier is the fallback priority (all tier-0 trackers are tried first, then tier-1, ...); the URL list is kept sorted by tier, matching create_torrent::add_tracker.

Link copied to clipboard

Adds a BEP 19 web seed (url-list).

Link copied to clipboard

The files and piece geometry used to build the torrent.

Link copied to clipboard

Builds the .torrent as a bencode Entry tree, ready for Bencode.encode or for adding custom keys before encoding. Mirrors create_torrent::generate.

Link copied to clipboard

Bencodes generate into a fresh byte array (= Bencode.encode(generate())).

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
fun numPieces(): Int

Number of pieces in the associated FileStorage.

Link copied to clipboard

Piece size of all pieces but the last. Forwards to the FileStorage.

Link copied to clipboard
fun pieceSize(index: Int): Int

Size of piece index; the last piece is usually shorter.

Link copied to clipboard
fun priv(): Boolean

Whether the private flag is set.

Link copied to clipboard

Sets the (optional) UTF-8 comment. Passing null clears it.

Link copied to clipboard

Sets the "creation date" field (POSIX seconds); 0 or null omits it.

Link copied to clipboard

Sets the (optional) UTF-8 creator string, e.g. the software name.

Link copied to clipboard

Convenience for the common v2 case where the caller already holds a file's 16 KiB block hashes in order. Equivalent to calling setHash2 for each block.

Link copied to clipboard

Sets the SHA-1 hash for piece index (v1, BEP 3). You must set every piece before generate. An all-zero hash is treated as "unset".

Link copied to clipboard
fun setHash2(file: Int, block: Int, h: Sha256Hash): CreateTorrent

Sets the BitTorrent v2 hash for the merkle leaf at block of file file (BEP 52). block is the block index within the file, starting at 0; h is the SHA-256 of that 16 KiB block. An all-zero hash is treated as "unset".

Link copied to clipboard

Sets the private flag (BEP 27). Private torrents ask clients to use only the tracker -- no DHT, no PEX, no other peer sources.

Link copied to clipboard

Sets the X.509 root certificate (PEM) for an SSL torrent. The string is the certificate content, not a path.

Link copied to clipboard

Total number of bytes across all files.