java.lang.Object
net.i2p.data.SimpleDataStructure
net.i2p.crypto.SHA1Hash
- All Implemented Interfaces:
DataStructure
I2P data structure for SHA-1 hash values.
This class provides a SimpleDataStructure wrapper for 20-byte SHA-1 hashes used throughout I2P for data integrity and identification purposes. It extends SimpleDataStructure to support I2P's serialization framework and provides efficient caching of hash codes.
Common uses include:
- Router and destination identifiers
- Message integrity verification
- Data structure indexing and lookup
Field Summary
Modifier and TypeFieldDescriptionprivate intHash of the data, computed on demand and then kept.static final intNumber of bytes in a SHA-1 hash.Fields inherited from class SimpleDataStructure
_dataConstructor Summary
Method Summary
All MethodsModifier 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.
Field Details
_cachedHashCode
private int _cachedHashCodeHash of the data, computed on demand and then kept.HASH_LENGTH
Constructor Details
SHA1Hash
public SHA1Hash()Create an empty SHA-1 hash (data is null).SHA1Hash
public SHA1Hash(byte[] data) Create a SHA-1 hash from the given data.- Parameters:
data- the 20-byte SHA-1 hash data- Throws:
IllegalArgumentException- if data is not 20 bytes (null is ok)
Method Details
length
public int length()Length of a SHA-1 hash.- Specified by:
lengthin classSimpleDataStructure- Returns:
- 20, the hash length in bytes
setData
public void setData(byte[] data) IllegalArgumentException if data is not 20 bytes (null is ok).- Overrides:
setDatain classSimpleDataStructure- Parameters:
data- of correct length, or null- Throws:
IllegalArgumentException- if data is not 20 bytes (null is ok)
readBytes
Read the hash data from a stream.- Specified by:
readBytesin interfaceDataStructure- Overrides:
readBytesin classSimpleDataStructure- Parameters:
in- the stream to read- Throws:
DataFormatException- if the data is improperly formattedIOException- if there was a problem reading the stream
hashCode
public int hashCode()A Hash is a hash, so just use the first 4 bytes for speed.- Overrides:
hashCodein classSimpleDataStructure- Returns:
- the hash code