holochain_integrity_types/hash.rs
1use crate::prelude::*;
2use holo_hash::ActionHash;
3use holo_hash::EntryHash;
4use holo_hash::ExternalHash;
5use holochain_secure_primitive::secure_primitive;
6/// 256 Bit generic hash.
7pub struct Hash256Bits([u8; 32]);
8secure_primitive!(Hash256Bits, 32);
9
10/// 512 Bit generic hash.
11pub struct Hash512Bits([u8; 64]);
12secure_primitive!(Hash512Bits, 64);
13
14#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
15#[non_exhaustive]
16/// Input to holochain hash function.
17pub enum HashInput {
18 /// Hash an Entry.
19 Entry(Entry),
20 /// Hash an action.
21 Action(Action),
22 /// Blake2b is the native Holochain hashing algorithm and compatible with
23 /// e.g. Polkadot and Zcash.
24 /// Second value is the output length of the hash in bytes.
25 Blake2B(#[serde(with = "serde_bytes")] Vec<u8>, u8),
26 /// 256 bit SHA-2 a.k.a. SHA-256 used by Bitcoin, IPFS, etc.
27 Sha256(#[serde(with = "serde_bytes")] Vec<u8>),
28 /// 512 bit SHA-2 a.k.a. SHA-512.
29 Sha512(#[serde(with = "serde_bytes")] Vec<u8>),
30 /// Keccak256 is the variant of SHA3 used by the Ethereum Virtual Machine.
31 /// It is slightly different to the NIST standard SHA3-256.
32 /// (i.e. the resulting hashes are completely different)
33 Keccak256(#[serde(with = "serde_bytes")] Vec<u8>),
34 /// NIST standard SHA3-256.
35 Sha3256(#[serde(with = "serde_bytes")] Vec<u8>),
36}
37
38#[derive(Debug, serde::Serialize, serde::Deserialize)]
39#[non_exhaustive]
40/// Output from the holochain hashing host function.
41pub enum HashOutput {
42 /// Hash of an [`Entry`].
43 Entry(EntryHash),
44 /// Hash of a [`Action`].
45 Action(ActionHash),
46 /// Hash of an external type.
47 External(ExternalHash),
48 /// Hash of bytes using Blake2b.
49 Blake2B(#[serde(with = "serde_bytes")] Vec<u8>),
50 /// Hash of bytes using SHA-256.
51 Sha256(Hash256Bits),
52 /// Hash of bytes using SHA-512.
53 Sha512(Hash512Bits),
54 /// Hash of bytes using Keccak256.
55 Keccak256(Hash256Bits),
56 /// Hash of bytes using NIST standard SHA3-256.
57 Sha3256(Hash256Bits),
58}