- All Implemented Interfaces:
DataStructure
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 SHA1Hash
HASH_LENGTHFields inherited from class SimpleDataStructure
_dataConstructor Summary
Method Summary
Methods inherited from class SHA1Hash
hashCode, length, readBytes, setDataModifier and TypeMethodDescriptioninthashCode()A Hash is a hash, so just use the first 4 bytes for speed.intlength()Length of a SHA-1 hash.voidreadBytes(InputStream in) Read the hash data from a stream.voidsetData(byte[] data) IllegalArgumentException if data is not 20 bytes (null is ok).Methods inherited from class SimpleDataStructure
calculateHash, equals, fromBase64, fromByteArray, getData, read, toBase64, toByteArray, toString, writeBytesModifier and TypeMethodDescriptionSHA256 hash of the byte array, or null if data is null.booleanCompares the raw data only: two different SimpleDataStructure subclasses of the same length holding the same bytes are equal, e.g.voidfromBase64(String data) Data from the base64 string.voidfromByteArray(byte[] data) Does the same thing asSimpleDataStructure.setData(byte[])but null not allowed.byte[]getData()Data reference (not a copy).protected intread(InputStream in, byte[] target) Repeated reads until the buffer is full or IOException is thrown.toBase64()Renders the structure into modified base 64 notation.byte[]Same thing as getData().toString()Returns a string representation of this data structure.voidwriteBytes(OutputStream out) Writes the data to the output stream.
Constructor Details
TokenKey
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 peerih- the info hash of the torrent