Cryptographic hash utilities
The near-crypto-hash crate gives NEAR contract-facing code one shared type for a 32-byte cryptographic hash. It keeps hash values distinct from variable-length byte data and lets higher SDK layers move the same value between raw bytes, typed contract data, and external text forms.
Hash value contract
Fixed-size hash values
CryptoHash is the repository-wide raw hash value for outputs that are exactly 32 bytes long. Its fixed array shape makes the size part of the type contract, so code that accepts a CryptoHash cannot silently receive a shorter or longer byte sequence. The crate does not implement a hash algorithm; it defines the value shape that other layers can share.
References: near-crypto-hash/…/lib.rs• L1-2 near-crypto-hash/Cargo.toml• L10-12
Text boundary representations
The SDK uses Base58CryptoHash when a hash crosses a text boundary such as JSON. This wrapper keeps the underlying CryptoHash while presenting it as a Base58 string, and it rejects input that does not decode to exactly 32 bytes. That boundary check prevents malformed external data from becoming an internal hash value.
References: near-sdk-core/…/hash.rs• L5-11 near-sdk-core/…/hash.rs• L84-117 near-sdk-core/…/hash.rs• L127-155
Hash roles in NEAR systems
SDK type surface
CryptoHash is re-exported through near-sdk-core, which makes the shared hash contract part of the SDK type surface. Contract-facing APIs can therefore use one raw value type while conversion helpers expose byte slices, owned bytes, or the Base58 form required by a caller.
References: near-sdk-core/…/mod.rs• L17-24 near-sdk-core/…/hash.rs• L25-65
Contract identity
Hash values identify code in contract lifecycle data. Global contract identifiers use a CryptoHash for a code-hash choice, while account contract records use the same fixed-size value for local and global code references. This gives code identity a stable binary form and leaves the choice between a code hash and an account identifier explicit at the surrounding type level.
References: near-global-contracts/…/global_contract_identifier.rs• L1-12 near-global-contracts/…/global_contract_identifier.rs• L32-55 near-global-contracts/…/global_contract_identifier.rs• L89-103
Hash production and use
Hashing interfaces
The environment layer produces 32-byte values for SHA-256 and Keccak-256 and returns them as CryptoHash. It also exposes algorithms with other output sizes through array types, so callers do not force every digest into the 32-byte contract. For the relationship between these value types and the algorithm crates, see Digest algorithm implementations; for the runtime boundary used by host-backed hashing, see NEAR host ABI bindings.
The overall flow separates three concerns:
flowchart LR
Input[Input bytes] --> Algorithm[Hashing interface]
Algorithm --> Value[Fixed-size hash value]
Value --> Contract[Contract or protocol data]
Value --> Text[Base58 text boundary]
Text -->|validated decode| Value
References: near-sdk-env/…/hash.rs• L15-30 near-sdk-env/…/hash.rs• L68-132 near-sdk-env/…/hash.rs• L135-168 near-sdk-core/…/hash.rs• L84-117
Change checklist
When these files or sections are changed, remember to consider these:
- If the raw hash size changes, review every text parser and every API that treats 32 bytes as a code or data identifier.
- If the text encoding changes, preserve the JSON contract used by global contract identifiers and by callers that parse
Base58CryptoHash. - If a hashing interface gains or changes an output size, keep its return type aligned with the digest size instead of reusing
CryptoHashby convention. - If the shared type moves between SDK layers, preserve the public re-export so contract code and host-facing code keep one hash type.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A Base58 string decodes to fewer or more than 32 bytes | External input cannot represent a valid CryptoHash | Base58CryptoHash checks the decoded length and returns ParseCryptoHashError |
A caller treats a non-32-byte digest as CryptoHash | The digest may be truncated or placed in the wrong protocol field | Hash interfaces use output-specific array types such as [u8; 64] and [u8; 20] |
| A code hash is serialized without the expected text form | Global contract identifier data no longer matches its caller-facing format | The global contract identifier contract marks code hashes for Base58 serialization |
Key files
| File | Why to read it |
|---|---|
| near-crypto-hash/…/lib.rs | Defines the shared 32-byte CryptoHash value contract. |
| near-sdk-core/…/hash.rs | Shows the validated Base58 boundary and conversions to and from raw hash bytes. |
| near-sdk-core/…/mod.rs | Shows how the shared hash type enters the public SDK type surface. |
| near-sdk-env/…/hash.rs | Shows which environment hash operations return CryptoHash and which use other digest sizes. |
| near-global-contracts/…/global_contract_identifier.rs | Shows how hashes represent contract code identity and cross the serialization boundary. |