Blake2bHash
- class cometa.cryptography.blake2b_hash.Blake2bHash(ptr)[source]
Bases:
objectRepresents a BLAKE2b cryptographic hash.
BLAKE2b is a cryptographic hash function used throughout Cardano for transaction identification, address generation, and cryptographic verification.
- Common hash sizes:
28 bytes (224 bits): Used for credentials and key hashes
32 bytes (256 bits): Used for transaction IDs and script hashes
- __init__(ptr)[source]
Internal constructor. Use class methods like compute, from_bytes, from_hex, or from_cbor instead.
- Return type:
None
- classmethod compute(data, hash_size=32)[source]
Computes a BLAKE2b hash of the given data.
- Parameters:
data (bytes | bytearray) – The input data to hash.
hash_size (int) – The desired hash length in bytes (e.g., 28 or 32).
- Returns:
A new Blake2bHash containing the computed hash.
- Raises:
CardanoError – If the hash computation fails.
- Return type:
Example
>>> hash = Blake2bHash.compute(b"hello world", hash_size=32) >>> len(hash) 32
- classmethod from_bytes(data)[source]
Creates a Blake2bHash from raw hash bytes.
- Parameters:
data (bytes | bytearray) – The raw hash bytes.
- Returns:
A new Blake2bHash containing the provided bytes.
- Raises:
CardanoError – If the hash creation fails.
- Return type:
Example
>>> hash = Blake2bHash.from_bytes(bytes(32)) >>> len(hash) 32
- classmethod from_hex(hex_string)[source]
Creates a Blake2bHash from a hexadecimal string.
- Parameters:
hex_string (str) – The hexadecimal representation of the hash.
- Returns:
A new Blake2bHash containing the decoded bytes.
- Raises:
CardanoError – If the hex string is invalid.
- Return type:
Example
>>> hash = Blake2bHash.from_hex("00" * 32) >>> len(hash) 32
- classmethod from_cbor(reader)[source]
Deserializes a Blake2bHash from CBOR data.
- Parameters:
reader (CborReader) – A CborReader positioned at the hash data.
- Returns:
A new Blake2bHash deserialized from the CBOR data.
- Raises:
CardanoError – If deserialization fails.
- Return type:
- property size: int
Returns the size of the hash in bytes.
- to_bytes()[source]
Returns the raw hash bytes.
- Returns:
The hash as a bytes object.
- Return type:
bytes
Example
>>> hash = Blake2bHash.from_hex("00" * 32) >>> hash.to_bytes() == bytes(32) True
- to_hex()[source]
Returns the hexadecimal string representation of the hash.
- Returns:
The hash as a lowercase hexadecimal string.
- Return type:
str
Example
>>> hash = Blake2bHash.from_bytes(bytes(32)) >>> hash.to_hex() '0000000000000000000000000000000000000000000000000000000000000000'
- to_cbor(writer)[source]
Serializes the hash to CBOR format.
- Parameters:
writer (CborWriter) – A CborWriter to write the serialized data to.
- Raises:
CardanoError – If serialization fails.
- Return type:
None
- compare(other)[source]
Compares this hash with another hash.
- Parameters:
other (Blake2bHash) – The hash to compare with.
- Returns:
A negative value if this hash is less than other, zero if they are equal, a positive value if this hash is greater than other.
- Return type:
int
- __hash__()[source]
Returns a Python hash of this object for use in sets and dicts.
- Return type:
int
- __eq__(other)[source]
Checks equality with another Blake2bHash.
- Parameters:
other (object)
- Return type:
bool
- __lt__(other)[source]
Less than comparison.
- Parameters:
other (Blake2bHash)
- Return type:
bool
- __le__(other)[source]
Less than or equal comparison.
- Parameters:
other (Blake2bHash)
- Return type:
bool
- __gt__(other)[source]
Greater than comparison.
- Parameters:
other (Blake2bHash)
- Return type:
bool
- __ge__(other)[source]
Greater than or equal comparison.
- Parameters:
other (Blake2bHash)
- Return type:
bool