Package convex.core.data
Class Hash
- All Implemented Interfaces:
IValidated,IWriteable,Comparable<ABlob>
Class used to represent an immutable 32-byte Hash value.
The Hash algorithm used may depend on context.
This is intended to help with type safety vs. regular Blob objects and as a
useful type as a key in relevant data structures.
"Companies spend millions of dollars on firewalls, encryption and secure
access devices, and it's money wasted, because none of these measures address
the weakest link in the security chain." - Kevin Mitnick
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic Hashstatic Hashstatic intStandard length of a Hash in bytesstatic Hashstatic Hashstatic ATypeType of Hash valuesFields inherited from class convex.core.data.AArrayBlob
length, offset, storeFields inherited from class convex.core.data.ABlob
contentHashFields inherited from class convex.core.data.ACell
cachedRef, EMPTY_ARRAY -
Method Summary
Modifier and TypeMethodDescriptionstatic HashComputes the Hash for any ACell value.intencode(byte[] bs, int pos)Writes this Cell's encoding to a byte array, including a tag byte which will be written firstbooleanDetermines if this Blob is equal to another Blob.booleanTests if the Hash value is precisely equal to another non-null Hash value.intEstimate the encoded data size for this Cell.intfirstInt()Get the first 32 bits of this Hash.static HashConstructs a Hash object from a hex stringgetChunk(long i)Gets a chunk of this Blob, as a canonical Blob up to the maximum chunk sizelongMethod to calculate the encoding length of a Cell.bytegetTag()Gets the tag byte for this cell.booleanReturns true if this Cell is in a canonical format for message writing.booleanReturns true if this object represents a first class CVM Value.booleanDetermines if this Cell Represents an embedded object.booleanReturns true if this object is a regular blob (i.e.static HashreadRaw(ByteBuffer bb)Reads a Hash from a ByteBuffer Assumes no Tag, i.e.Converts this Cell to its canonical version.voidValidates the local structure and invariants of this cell.static Hashwrap(byte[] hashBytes)Wraps the specified bytes as a Data object Warning: underlying bytes are used directly.static Hashwrap(byte[] hashBytes, int offset)Wraps the specified bytes as a Data object Warning: underlying bytes are used directly.static Hashwrap(AArrayBlob data)Wraps the specified blob data as a Hash, sharing the underlying byte array.static Hashwrap(AArrayBlob data, int offset, int length)Methods inherited from class convex.core.data.AArrayBlob
append, byteAt, commonHexPrefixLength, compareTo, compareTo, count, encodeRaw, equalsBytes, equalsBytes, getByteBuffer, getBytes, getHexDigit, getInternalArray, getInternalOffset, getRefCount, getUnchecked, hexMatches, hexMatchLength, longValue, rangeMatches, rangeMatches, slice, slice, toBlob, toHexString, toHexString, toLong, updateDigest, validate, writeToBuffer, writeToBufferMethods inherited from class convex.core.data.ABlob
computeHash, empty, equals, get, getBytes, getContentHash, getElementRef, getType, hashCode, hexEquals, hexEquals, hexLength, print, toByteBuffer, toHexStringMethods inherited from class convex.core.data.ACountable
isEmpty, sizeMethods inherited from class convex.core.data.ACell
announce, announce, attachMemorySize, attachRef, cachedEncoding, cachedHash, calcMemorySize, createAnnounced, createEncoding, createPersisted, createPersisted, createRef, equals, getChildRefs, getEncoding, getHash, getMemorySize, getRef, getRef, toString, updateRefs, writeMethods inherited from class convex.core.data.AObject
attachEncoding, print
-
Field Details
-
LENGTH
public static final int LENGTHStandard length of a Hash in bytes- See Also:
- Constant Field Values
-
TYPE
Type of Hash values -
NULL_HASH
-
TRUE_HASH
-
FALSE_HASH
-
EMPTY_HASH
-
-
Method Details
-
wrap
Wraps the specified bytes as a Data object Warning: underlying bytes are used directly. Use only if no external references to the byte array will be retained.- Parameters:
hashBytes- Bytes to wrap- Returns:
- Hash wrapping the given byte array
-
wrap
Wraps the specified blob data as a Hash, sharing the underlying byte array.- Parameters:
data- Blob data of correct size for a Hash. Must have at least enough bytes for a Hash- Returns:
- Wrapped data as a Hash
-
wrap
Wraps the specified bytes as a Data object Warning: underlying bytes are used directly. Use only if no external references to the byte array will be retained.- Parameters:
hashBytes- Byte array containing hash valueoffset- Offset into byte array for start of hash value- Returns:
- Hash wrapping the given byte array segment
-
equals
Description copied from class:ABlobDetermines if this Blob is equal to another Blob. Blobs are defined to be equal if they have the same on-chain representation, i.e. if and only if all of the following are true: - Blob is of the same general type - Blobs are of the same length - All byte values are equal -
equals
Tests if the Hash value is precisely equal to another non-null Hash value.- Parameters:
other- Hash to comapre with- Returns:
- true if Hashes are equal, false otherwise.
-
firstInt
public int firstInt()Get the first 32 bits of this Hash. Used for Java hashCodes- Returns:
- Int representing the first 32 bits
-
fromHex
Constructs a Hash object from a hex string- Parameters:
hexString- Hex String- Returns:
- Hash with the given hex string value, or null is String is not valid
-
wrap
-
compute
Computes the Hash for any ACell value. May return a cached Hash if available in memory.- Parameters:
value- Any Cell- Returns:
- Hash of the encoded data for the given value
-
readRaw
Reads a Hash from a ByteBuffer Assumes no Tag, i.e. just Hash.LENGTH for the hash is read.- Parameters:
bb- ByteBuffer to read from- Returns:
- Hash object read from ByteBuffer
-
encode
public int encode(byte[] bs, int pos)Description copied from class:ACellWrites this Cell's encoding to a byte array, including a tag byte which will be written first- Specified by:
encodein interfaceIWriteable- Specified by:
encodein classACell- Parameters:
bs- A byte array to which to write the encodingpos- The offset into the byte array- Returns:
- New position after writing
-
isCanonical
public boolean isCanonical()Description copied from class:ACellReturns true if this Cell is in a canonical format for message writing. Reading or writing a non-canonical value should be considered illegal, but non-canonical objects may be used on a temporary internal basis.- Specified by:
isCanonicalin classACell- Returns:
- true if the object is in canonical format, false otherwise
-
isCVMValue
public final boolean isCVMValue()Description copied from class:ACellReturns true if this object represents a first class CVM Value. Sub-structural cells that are not themselves first class values should return false. CVM values might not be in a canonical format, e.g. temporary data structures- Specified by:
isCVMValuein classACell- Returns:
- true if the object is a CVM Value, false otherwise
-
estimatedEncodingSize
public int estimatedEncodingSize()Description copied from interface:IWriteableEstimate the encoded data size for this Cell. Used for quickly sizing buffers. Implementations should try to return a size that is likely to contain the entire object when represented in binary format, including the tag byte.- Returns:
- The estimated size for the binary representation of this object.
-
getEncodingLength
public long getEncodingLength()Description copied from class:ACellMethod to calculate the encoding length of a Cell. May be overridden to avoid creating encodings during memory size calculations. This reduces hashing!- Overrides:
getEncodingLengthin classACell- Returns:
- Exact encoding length of this Cell
-
getChunk
Description copied from class:ABlobGets a chunk of this Blob, as a canonical Blob up to the maximum chunk size -
validateCell
Description copied from class:ACellValidates the local structure and invariants of this cell. Called by validate() super implementation. Should validate directly contained data, but should not validate all other structure of this cell. In particular, should not traverse potentially missing child Refs.- Overrides:
validateCellin classAArrayBlob- Throws:
InvalidDataException- If the Cell is invalid
-
isEmbedded
public boolean isEmbedded()Description copied from class:ACellDetermines if this Cell Represents an embedded object. Embedded objects are encoded directly into the encoding of the containing Cell (avoiding the need for a hashed reference). Subclasses should override this if they have a cheap O(1) way to determine if they are embedded or otherwise.- Overrides:
isEmbeddedin classACell- Returns:
- true if Cell is embedded, false otherwise
-
isRegularBlob
public boolean isRegularBlob()Description copied from class:ABlobReturns true if this object is a regular blob (i.e. not a special blob type like Hash or Address)- Overrides:
isRegularBlobin classABlob- Returns:
- True if a regular blob
-
getTag
public byte getTag()Description copied from class:ACellGets the tag byte for this cell. The tag byte will be the first byte of the encoding -
toCanonical
Description copied from class:ACellConverts this Cell to its canonical version. Returns this if already canonical- Specified by:
toCanonicalin classABlob- Returns:
- Canonical version of Cell
-