Package convex.core.data
Class Hash
- All Implemented Interfaces:
IValidated,IWriteable,Comparable<ABlobLike<?>>
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 final Hashstatic final Hashstatic final intStandard length of a Hash in bytesstatic final Hashstatic final Hashstatic final ATypeType of Hash values is just a regular BlobFields inherited from class convex.core.data.AArrayBlob
contentHash, offset, storeFields inherited from class convex.core.data.ACell
cachedRef, memorySize -
Method Summary
Modifier and TypeMethodDescriptionfinal intOptimised compareTo for Hashes.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 stringstatic HashComputes the Hash for any ACell value.getChunk(long i) Gets a chunk of this Blob, as a canonical flat Blob up to the maximum Blob chunk size.intMethod to calculate the encoding length of a Cell.booleanReturns true if this Cell is in a canonical representation.booleanDetermines if this Cell Represents an embedded object.static HashBest effort attempt to parse a Hash.static HashBest effort attempt to parse a Hash.Converts this Cell to a 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 pos) Wraps the specified blob data as a Hash, sharing the underlying byte array.static HashWraps the Blob as a Hash if possibleMethods inherited from class convex.core.data.AArrayBlob
append, appendHex, appendSmall, byteAt, byteAtUnchecked, calcMemorySize, compareTo, compareTo, encodeRaw, equals, equalsBytes, equalsBytes, getByteBuffer, getBytes, getContentHash, getHexDigit, getInternalArray, getInternalOffset, getRef, getRefCount, hexMatch, hexMatch, hexMatches, isChunkPacked, isFullyPacked, longAt, longValue, rangeMatches, rangeMatches, read, shortAt, slice, toFlatBlob, updateDigest, updateRefs, validateMethods inherited from class convex.core.data.ABlob
computeHash, count, empty, encode, equals, get, getElementRef, getTag, getType, hashCode, isCVMValue, print, read, replaceSlice, size, slice, toBlob, toByteBufferMethods inherited from class convex.core.data.ABlobLike
getBytes, hexEquals, hexLength, isDataValue, toHexString, toHexStringMethods inherited from class convex.core.data.ACountable
isEmptyMethods inherited from class convex.core.data.ACell
attachMemorySize, attachRef, cachedEncoding, cachedHash, createEncoding, createRef, equals, genericEquals, getCanonical, getChildRefs, getEncoding, getHash, getMemorySize, getMemorySize, getRef, toCVMString, toStringMethods inherited from class convex.core.data.AObject
attachEncoding, print, print
-
Field Details
-
LENGTH
public static final int LENGTHStandard length of a Hash in bytes- See Also:
-
TYPE
Type of Hash values is just a regular Blob -
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 (must be correct length)- Returns:
- Hash wrapping the given byte array
-
wrap
-
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 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 Hashpos- Position ib Blob to read from- Returns:
- Wrapped data as a Hash, or null if insufficent bytes in source Blob
-
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
-
firstInt
public int firstInt()Get the first 32 bits of this Hash. Used for Java hashCodes- Returns:
- Int representing the first 32 bits
-
fromHex
-
parse
-
parse
-
get
-
isCanonical
public boolean isCanonical()Description copied from class:ACellReturns true if this Cell is in a canonical representation. Non-canonical objects may be used on a temporary internal basis, they should be converted to canonical representations for general purpose use.- Specified by:
isCanonicalin classACell- Returns:
- true if the object is in canonical format, false otherwise
-
toCanonical
Description copied from class:ACellConverts this Cell to a canonical version. Must return this Cell if already canonical, may be O(n) in size of value otherwise. Callers should usually use getCanonical(), which caches canonical instances once created- Specified by:
toCanonicalin classABlob- Returns:
- Canonical version of Cell
-
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 highly likely to contain the entire object when encoded, including the tag byte. Should not traverse soft Refs, i.e. must be usable on arbitrary partial data structures- Returns:
- The estimated size for the binary representation of this object.
-
getEncodingLength
public int 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 flat Blob up to the maximum Blob chunk size. Returns empty Blob if and only if referencing the end of a Blob with fully packed chunks -
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
-
compareTo
Optimised compareTo for Hashes. Needed for MapLeaf, SetLeaf etc.- Parameters:
b- Other Hash to compare with- Returns:
- Negative if this is "smaller", 0 if this "equals" b, positive if this is "larger"
-
equals
Tests if the Hash value is precisely equal to another non-null Hash value.- Parameters:
other- Hash to compare with- Returns:
- true if Hashes are equal, false otherwise.
-