java.lang.Object
net.i2p.router.util.HashDistance

public class HashDistanceextends Object
Utility for calculating XOR distance between router hashes.

Computes the XOR distance between two router hashes by performing a bitwise XOR operation on the hash data. Used in Kademlia-based distributed hash tables for determining key proximity and routing decisions.

Returns the result as a BigInteger to handle the full 256-bit hash space without overflow issues. The distance metric is fundamental to Kademlia DHT operations including key lookup, peer selection, and network topology analysis.

  • Constructor Details

    • HashDistance

      private HashDistance()
      Utility class, not intended for instantiation.
  • Method Details

    • getDistance

      public static BigInteger getDistance(Hash targetKey, Hash routerInQuestion)
      Compute XOR distance between two hashes. Allocates a BigInteger. For comparison-only use cases, prefer compare(Hash, Hash, Hash, Hash).
      Parameters:
      targetKey - one of the two hashes whose XOR distance is wanted
      routerInQuestion - the other of the two hashes
      Returns:
      the unsigned XOR of the two hashes, as a BigInteger over all 256 bits
    • compare

      public static int compare(Hash a, Hash b, Hash c, Hash d)
      Compare two XOR distances without allocating BigInteger. Returns negative, zero, or positive as dist(a,b) < = > dist(c,d).
      Parameters:
      a - the first hash of the left-hand pair
      b - the second hash of the left-hand pair
      c - the first hash of the right-hand pair
      d - the second hash of the right-hand pair
      Returns:
      the first byte where a XOR b and c XOR d differ, or 0 if they are identical