generatePeerId

fun generatePeerId(fingerprint: String = defaultFingerprint(), random: Random = Random.Default): PeerId

Generate a 20-byte peer id whose leading bytes are the client fingerprint and whose remaining bytes are URL-safe random characters. This is the port of aux::generate_peer_id(session_settings const&) (src/generate_peer_id.cpp).

libtorrent's logic, reproduced here:

  1. take the configured fingerprint string (settings_pack::peer_fingerprint);

  2. if it is longer than 20 bytes, truncate it to 20;

  3. copy it into the front of the id;

  4. fill whatever remains with url_random.

The fingerprint is encoded as ASCII (it is '-', two tag chars and four base-36 version chars, all single-byte). Each fingerprint character occupies exactly one id byte.

Parameters

fingerprint

the client prefix, defaulting to KiteTorrent's defaultFingerprint ("-KT0010-"). Strings longer than PEER_ID_SIZE are truncated to 20 bytes, matching print.resize(ret.size()).

random

the Random source for the tail. Defaults to Random.Default; pass a seeded Random for reproducible ids in tests.


fun generatePeerId(fingerprint: String, randomTail: ByteArray): PeerId

Deterministic peer-id construction: place the fingerprint at the front and use the caller-supplied randomTail for the remaining bytes verbatim. This is the testable counterpart of generatePeerId. It consults no random number generator.

The randomTail must be exactly the number of bytes left after the (possibly truncated) fingerprint, i.e. 20 - min(fingerprint.length, 20), so that the fingerprint and tail together fill all 20 bytes. Unlike generatePeerId the tail bytes are used as-is (they are not constrained to the URL-safe alphabet), which lets tests pin an exact 20-byte value.

Throws

if randomTail is not the required length.