xrpl_binary_codec/
hash.rs

1use sha2::Digest;
2use sha2::Sha512;
3use xrpl_types::Hash256;
4
5/// Unsigned single signer transactions prefix <https://xrpl.org/basic-data-types.html#hash-prefixes>
6pub const HASH_PREFIX_UNSIGNED_TRANSACTION_SINGLE: [u8; 4] = [0x53, 0x54, 0x58, 0x00];
7
8/// Signed transactions prefix <https://xrpl.org/basic-data-types.html#hash-prefixes>
9pub const HASH_PREFIX_SIGNED_TRANSACTION: [u8; 4] = [0x54, 0x58, 0x4E, 0x00];
10
11/// Calculate hash <https://xrpl.org/basic-data-types.html#hashes> of given data
12pub fn hash(prefix: [u8; 4], data: &[u8]) -> Hash256 {
13    // INSIGHT: Sha512Trunc245 does not give same result as Sha512[0..32]
14    let mut hasher = Sha512::new_with_prefix(prefix);
15    hasher.update(data);
16    let hash: [u8; 64] = hasher.finalize().into();
17    Hash256(hash[0..32].try_into().expect("length 64"))
18}