generateIdImpl
Generate a BEP 42 ("secure") node id from an external IP address and a chosen 8-bit "r" value, the deterministic core of id generation. Faithful port of node_id generate_id_impl(address const& ip_, std::uint32_t r).
The construction (https://www.bittorrent.org/beps/bep_0042.html):
Take the first 4 (IPv4) or 8 (IPv6) octets of the address and AND each with the corresponding V4_MASK/V6_MASK byte.
OR
(r & 0x7) << 5into octet 0.CRC32C those masked octets (in network-byte order, see below).
id[0] = crc >> 24,id[1] = crc >> 16,id[2] = ((crc >> 8) & 0xf8) | rand(0..7)(top 5 bits fixed, low 3 random).id[3..18]= random bytes;id[19] = r & 0xff.
CRC32C byte order. libtorrent reads the masked octets back as a std::uint32_t/std::uint64_t and feeds them to its CRC32C, which (on the common little-endian path, and per the BEP test vectors) is equivalent to running CRC32C over the masked octets in their original array order. Our Crc32c.compute processes bytes in array order, so we pass the masked octets straight in. This matches the canonical BEP 42 vectors (e.g. 124.31.75.21, r=1 → prefix 5f bf bf…), which the test suite pins.
Parameters
the node's external address (4-byte IPv4 or 16-byte IPv6).
the 8-bit value placed in id[19]; only its low 3 bits affect the CRC.
RNG for the random portion (defaults to Random.Default; pass a seeded instance for reproducible ids in tests). libtorrent uses random(max) which is inclusive of max, so rand(0..7) and rand(0..255) are realised as nextInt(0, 8) / nextInt(0, 256).