crv.core.hashing
Experimental API
crv.core.hashing
Canonical JSON serialization and hashing helpers for core schemas.
Provides a single canonical JSON policy and SHA-256 helpers to ensure stable, order-insensitive serialization and hashing across runs and consumers. This module is zero-IO and uses only the Python standard library.
Notes
- Canonical JSON:
- sort_keys=True
- separators=(",", ":")
- ensure_ascii=False
- Hashing is performed over the UTF-8 encoded canonical JSON string.
- Used by core/tests/downstream packages to keep identifiers stable.
References
- specs: src/crv/core/.specs/spec-0.1.md, spec-0.2.md
crv.core.hashing.json_dumps_canonical
Serialize an object to a canonical JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
typing.Any
|
JSON-serializable object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Canonical JSON string with sort_keys=True, compact separators, |
str
|
and ensure_ascii=False. |
Notes
This function assumes the input is JSON-serializable and does not perform coercion of unsupported types.
Source code in src/crv/core/hashing.py
crv.core.hashing.hash_row
Compute a stable hash for a row-like mapping by hashing its canonical JSON.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row
|
collections.abc.Mapping[str, typing.Any]
|
Row mapping (e.g., dict) to hash. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
SHA-256 hex digest over the canonical JSON serialization. |
Notes
Re-ordering keys in the mapping does not change the result.
Source code in src/crv/core/hashing.py
crv.core.hashing.hash_context
Hash a context-like mapping using canonical JSON and SHA-256 policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx_json
|
collections.abc.Mapping[str, typing.Any]
|
Context mapping to hash. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
SHA-256 hex digest over the canonical JSON serialization. |
Source code in src/crv/core/hashing.py
crv.core.hashing.hash_state
Hash an agent state mapping using canonical JSON and SHA-256 policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_state
|
collections.abc.Mapping[str, typing.Any]
|
Agent state mapping to hash. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
SHA-256 hex digest over the canonical JSON serialization. |
Examples: