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 final Hash
static final Hash
static final int
Standard length of a Hash in bytesstatic final Hash
static final Hash
static final AType
Type of Hash values is just a regular BlobFields inherited from class convex.core.data.AArrayBlob
length, offset, store
Fields inherited from class convex.core.data.ABlob
contentHash
Fields inherited from class convex.core.data.ACell
cachedRef, EMPTY_ARRAY
-
Method Summary
Modifier and TypeMethodDescriptionfinal int
Optimised compareTo for Hashes.static Hash
Computes the Hash for any ACell value.int
encode
(byte[] bs, int pos) Writes this Cell's encoding to a byte array, including a tag byte which will be written first.boolean
Tests if the Hash value is precisely equal to another non-null Hash value.int
Estimate the encoded data size for this Cell.int
firstInt()
Get the first 32 bits of this Hash.static Hash
Constructs a Hash object from a hex stringgetChunk
(long i) Gets a chunk of this Blob, as a canonical Blob up to the maximum chunk size.int
Method to calculate the encoding length of a Cell.boolean
Returns true if this Cell is in a canonical representation for message writing.boolean
Determines if this Cell Represents an embedded object.static Hash
Best effort attempt to parse a Hash.static Hash
Best effort attempt to parse a Hash.static Hash
readRaw
(ByteBuffer bb) Reads a Hash from a ByteBuffer Assumes no Tag or count, i.e.Converts this Cell to its canonical version.void
Validates the local structure and invariants of this cell.static Hash
wrap
(byte[] hashBytes) Wraps the specified bytes as a Data object Warning: underlying bytes are used directly.static Hash
wrap
(byte[] hashBytes, int offset) Wraps the specified bytes as a Data object Warning: underlying bytes are used directly.static Hash
wrap
(AArrayBlob data) Wraps the specified blob data as a Hash, sharing the underlying byte array.static Hash
wrap
(AArrayBlob data, int pos) Wraps the specified blob data as a Hash, sharing the underlying byte array.static Hash
Wraps the Blob as a Hash if possibleMethods inherited from class convex.core.data.AArrayBlob
append, appendHex, appendSmall, byteAt, commonHexPrefixLength, compareTo, compareTo, count, encodeRaw, equals, equalsBytes, equalsBytes, getByteBuffer, getBytes, getHexDigit, getInternalArray, getInternalOffset, getRefCount, getTag, getUnchecked, hexMatches, hexMatchLength, longAt, longValue, rangeMatches, rangeMatches, shortAt, slice, toExactLong, toFlatBlob, updateDigest, validate, writeToBuffer, writeToBuffer
Methods inherited from class convex.core.data.ABlob
computeHash, empty, equals, get, getBytes, getContentHash, getElementRef, getType, hashCode, hexEquals, hexEquals, hexLength, isCVMValue, isRegularBlob, print, slice, toByteBuffer, toHexString, toHexString
Methods inherited from class convex.core.data.ACountable
isEmpty, size
Methods inherited from class convex.core.data.ACell
announce, announce, attachMemorySize, attachRef, cachedEncoding, cachedHash, calcMemorySize, createAnnounced, createEncoding, createPersisted, createPersisted, createRef, equals, getCanonical, getChildRefs, getEncoding, getHash, getMemorySize, getRef, getRef, isCompletelyEncoded, mark, mark, toCVMString, toString, updateRefs, write
Methods 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
Wraps the Blob as a Hash if possible- Parameters:
a
- Any ABlob instance- Returns:
- Hash instance, or null if argument is null or wrong length
-
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
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
-
parse
Best effort attempt to parse a Hash. Must parse as a blob of correct length- Parameters:
o
- Object expected to contain a Hash value- Returns:
- Hash value, or null is not parseable
-
parse
Best effort attempt to parse a Hash. Must parse as a blob of correct length. Leading "0x" optional.- Parameters:
s
- String expected to contain a Hash value- Returns:
- Hash value, or null is not parseable
-
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 or count, 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:ACell
Writes this Cell's encoding to a byte array, including a tag byte which will be written first. Cell must be canonical, or else an error may occur.- Specified by:
encode
in interfaceIWriteable
- Specified by:
encode
in 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:ACell
Returns true if this Cell is in a canonical representation for message writing. Non-canonical objects may be used on a temporary internal basis, they must always be converted to canonical representations for external use (e.g. Encoding).- Specified by:
isCanonical
in classACell
- Returns:
- true if the object is in canonical format, false otherwise
-
toCanonical
Description copied from class:ACell
Converts this Cell to its canonical version. Must return this Cell if already canonical, may be O(n) in size of value otherwise.- Specified by:
toCanonical
in classABlob
- Returns:
- Canonical version of Cell
-
estimatedEncodingSize
public int estimatedEncodingSize()Description copied from interface:IWriteable
Estimate 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:ACell
Method to calculate the encoding length of a Cell. May be overridden to avoid creating encodings during memory size calculations. This reduces hashing!- Overrides:
getEncodingLength
in classACell
- Returns:
- Exact encoding length of this Cell
-
getChunk
Description copied from class:ABlob
Gets a chunk of this Blob, as a canonical Blob up to the maximum chunk size. Returns empty Blob if and only if referencing the end of a Blob with fully packed chunks -
validateCell
Description copied from class:ACell
Validates 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:
validateCell
in classAArrayBlob
- Throws:
InvalidDataException
- If the Cell is invalid
-
isEmbedded
public boolean isEmbedded()Description copied from class:ACell
Determines 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:
isEmbedded
in 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.
-