org.klomp.snark.dht

## Class TokenKey

- java.lang.Object
- - net.i2p.data.SimpleDataStructure
  - - net.i2p.crypto.SHA1Hash
    - - org.klomp.snark.dht.TokenKey

- All Implemented Interfaces:
    DataStructure

      class TokenKey
      extends SHA1Hash

  Indexing key for DHT tokens based on node ID and info hash combination.

  This class generates a unique key for indexing tokens by combining the requesting node's ID with the torrent's info hash. The XOR operation creates a deterministic key that can be reproduced when the same node and torrent combination is used, enabling efficient token lookup and validation.

  Token indexing is crucial for DHT security as it ensures that tokens are bound to both the requesting node and the specific torrent. This prevents token replay attacks where a malicious peer might try to use a token intended for one torrent on another, or use another node's token.

  The resulting SHA1 hash provides a fixed-size key suitable for use in hash maps and other data structures for efficient token storage and retrieval.

- - ### Field Summary

    - ### Fields inherited from class net.i2p.crypto.SHA1Hash

      `HASH_LENGTH`

    - ### Fields inherited from class net.i2p.data.SimpleDataStructure

      `_data`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `TokenKey(NID nID, InfoHash ih)` Creates a new token key by combining a node ID with an info hash.

  - ### Method Summary

    - ### Methods inherited from class net.i2p.crypto.SHA1Hash

      `hashCode, length, readBytes, setData`

    - ### Methods inherited from class net.i2p.data.SimpleDataStructure

      `calculateHash, equals, fromBase64, fromByteArray, getData, read, toBase64, toByteArray, toString, writeBytes`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### TokenKey

          public TokenKey(NID nID,
                          InfoHash ih)

      Creates a new token key by combining a node ID with an info hash.

      The key is generated by XOR-ing the raw bytes of the node ID and info hash, then computing the SHA1 hash of the result. This creates a unique, deterministic identifier for the (node, torrent) pair.

      This method ensures that tokens are properly scoped to both the requesting node and the specific torrent, preventing cross-use of tokens and enhancing DHT security.

      Parameters:
        `nID` - the node ID of the requesting peer
        `ih` - the info hash of the torrent