[−][src]Module solana_libra_crypto::hash
This module defines traits and implementations of cryptographic hash functions for the Libra project.
It is designed to help authors protect against two types of real world attacks:
-
Domain Ambiguity: imagine that Alice has a private key and is using two different applications, X and Y. X asks Alice to sign a message saying "I am Alice". naturally, Alice is willing to sign this message, since she is in fact Alice. However, unbeknownst to Alice, in application Y, messages beginning with the letter "I" represent transfers. " am " represents a transfer of 500 coins and "Alice" can be interpreted as a destination address. When Alice signed the message she needed to be aware of how other applications might interpret that message.
-
Format Ambiguity: imagine a program that hashes a pair of strings. To hash the strings
a
andb
it hashesa + "||" + b
. The pair of stringsa="foo||", b = "bar"
anda="foo", b = "||bar"
result in the same input to the hash function and therefore the same hash. This creates a collision.
Examples
use solana_libra_crypto::hash::{CryptoHasher, TestOnlyHasher}; let mut hasher = TestOnlyHasher::default(); hasher.write("Test message".as_bytes()); let hash_value = hasher.finish();
The output is of type HashValue
, which can be used as an input for signing.
Implementing new hashers
For any new structure MyNewStruct
that needs to be hashed, the developer should define a
new hasher with:
define_hasher! { (MyNewStructHasher, MY_NEW_STRUCT_HASHER, b"MyNewStruct") }
Note: The last argument for the define_hasher
macro must be a unique string.
Then, the CryptoHash
trait should be implemented:
struct MyNewStruct; impl CryptoHash for MyNewStruct { type Hasher = MyNewStructHasher; // use the above defined hasher here fn hash(&self) -> HashValue { let mut state = Self::Hasher::default(); state.write(b"Struct serialized into bytes here"); state.finish() } }
Structs
ACCUMULATOR_PLACEHOLDER_HASH | Placeholder hash of |
AccessPathHasher | The hasher used to compute the hash of an AccessPath object. |
AccountAddressHasher | The hasher used to compute the hash of an AccountAddress object. |
AccountStateBlobHasher | The hasher used to compute the hash of the blob content of an account. |
BlockHasher | The hasher used to compute the hash (block_id) of a Block object. |
ContractEventHasher | The hasher used to compute the hash of a ContractEvent object. |
DiscoveryMsgHasher | The hasher used to compute the hash of a DiscoveryMsg object. |
EventAccumulatorHasher | The hasher used to compute the hash of an internal node in the event accumulator. |
GENESIS_BLOCK_ID | Genesis block id is used as a parent of the very first block executed by the executor. |
HashValue | Output value of our hash function. Intentionally opaque for safety and modularity. |
HashValueBitIterator | An iterator over |
LedgerInfoHasher | The hasher used to compute the hash of a LedgerInfo object. |
PRE_GENESIS_BLOCK_ID | Block id reserved as the id of parent block of the genesis block. |
PacemakerTimeoutHasher | The hasher used to compute the hash of a PacemakerTimeout object. |
RawTransactionHasher | The hasher used to compute the hash of a RawTransaction object. |
RoundHasher | The hasher used to compute the hash of a Round |
SPARSE_MERKLE_PLACEHOLDER_HASH | Placeholder hash of |
SignedTransactionHasher | The hasher used to compute the hash of a SignedTransaction object. |
SparseMerkleInternalHasher | The hasher used to compute the hash of an internal node in the Sparse Merkle Tree. |
SparseMerkleLeafHasher | The hasher used to compute the hash of a leaf node in the Sparse Merkle Tree. |
TestOnlyHasher | The hasher used only for testing. It doesn't have a salt. |
TimeoutMsgHasher | The hasher used to compute the hash of a TimeoutMsgHasher object. |
TransactionAccumulatorHasher | The hasher used to compute the hash of an internal node in the transaction accumulator. |
TransactionInfoHasher | The hasher used to compute the hash of a TransactionInfo object. |
VoteDataHasher | The hasher used to compute the hash of a VoteData object. |
Traits
CryptoHash | A type that implements |
CryptoHasher | A trait for generating hash from arbitrary stream of bytes. |
TestOnlyHash | Provides a test_only_hash() method that can be used in tests on types that implement
|