Expand description
Rust hashes library.
This library implements the hash functions needed by Bitcoin. As an ancillary thing, it exposes hexadecimal serialization and deserialization, since these are needed to display hashes.
§Examples
Hashing a single byte slice or a string:
use bitcoin_hashes::Sha256;
let bytes = [0u8; 5];
let _hash_of_bytes = Sha256::hash(&bytes);
let _hash_of_string = Sha256::hash("some string".as_bytes());
Hashing content from a reader:
use bitcoin_hashes::Sha256;
let mut reader: &[u8] = b"hello"; // In real code, this could be a `File` or `TcpStream`.
let mut engine = Sha256::engine();
std::io::copy(&mut reader, &mut engine).expect("engine writes don't error");
let _hash = Sha256::from_engine(engine);
Hashing content using std::io::Write
on a HashEngine
:
use std::io::Write as _;
use bitcoin_hashes::Sha256;
let part1: &[u8] = b"hello";
let part2: &[u8] = b" ";
let part3: &[u8] = b"world";
let mut engine = Sha256::engine();
engine.write_all(part1).expect("engine writes don't error");
engine.write_all(part2).unwrap();
engine.write_all(part3).unwrap();
let _hash = Sha256::from_engine(engine);
Re-exports§
Modules§
- cmp
- Useful comparison functions.
- error
- Error code for the
hashes
crate. - hash160
- HASH160 (SHA256 then RIPEMD160) implementation.
- hkdf
- HMAC-based Extract-and-Expand Key Derivation Function (HKDF).
- hmac
- Hash-based Message Authentication Code (HMAC).
- macros
- Public macros.
- ripemd160
- RIPEMD160 implementation.
- serde_
macros Deprecated - Macros for serde trait implementations, and supporting code.
- sha1
- SHA1 implementation.
- sha256
- SHA256 implementation.
- sha384
- SHA384 implementation.
- sha512
- SHA512 implementation.
- sha256d
- SHA256d implementation (double SHA256).
- sha256t
- SHA256t implementation (tagged SHA256).
- sha512_
256 - SHA512_256 implementation.
- siphash24
- SipHash 2-4 implementation.
Macros§
- hash_
newtype - Creates a new newtype around a
Hash
type. - impl_
debug_ only_ for_ newtype - Implements
fmt::Debug
using hex for a new type crated withcrate::hash_newtype
macro. - impl_
hex_ for_ newtype hex
- Implements string functions using hex for a new type crated with
crate::hash_newtype
macro. - impl_
serde_ for_ newtype serde
- Implements
Serialize
andDeserialize
for a new type created withcrate::hash_newtype
macro. - sha256t_
hash_ newtype Deprecated - Macro used to define a newtype tagged hash.
- sha256t_
tag - Macro used to define a tag.
Structs§
- From
Slice Error - Attempted to create a hash from an invalid length slice.
- Hash160
- HASH-160: Alias for the
hash160::Hash
hash type. Output of the Bitcoin HASH160 hash function. (RIPEMD160(SHA256)) - Hkdf
- HMAC-based Extract-and-Expand Key Derivation Function (HKDF).
- Hmac
- A hash computed from a RFC 2104 HMAC. Parameterized by the underlying hash function.
- Hmac
Engine - Pair of underlying hash engines, used for the inner and outer hash of HMAC.
- Ripemd160
- RIPEMD-160: Alias for the
ripemd160::Hash
hash type. Output of the RIPEMD160 hash function. - Sha1
- SHA-1: Alias for the
sha1::Hash
hash type. Output of the SHA1 hash function. - Sha256
- SHA-256: Alias for the
sha256::Hash
hash type. Output of the SHA256 hash function. - Sha384
- SHA-384: Alias for the
sha384::Hash
hash type. Output of the SHA384 hash function. - Sha512
- SHA-512: Alias for the
sha512::Hash
hash type. Output of the SHA512 hash function. - Sha256d
- Double SHA-256: Alias for the
sha256d::Hash
hash type. Output of the SHA256d hash function. - Sha512_
256 - SHA-512-256: Alias for the
sha512_256::Hash
hash type. Output of the SHA512/256 hash function. - Siphash24
- SipHash-2-4: Alias for the
siphash24::Hash
hash type. Output of the SipHash24 hash function.
Traits§
- General
Hash - Trait describing hash digests which can be constructed by hashing arbitrary data.
- Hash
- Trait which applies to hashes of all types.
- Hash
Engine - A hashing engine which bytes can be serialized into.
- IsByte
Array - Ensures that a type is an array.
Functions§
- debug_
hex - Writes
bytes
as ahex
string to the formatter.
Type Aliases§
- Hkdf
Sha256 - HKDF-HMAC-SHA-256: Type alias for the
Hkdf<Sha256>
type. - Hkdf
Sha512 - HKDF-HMAC-SHA-512: Type alias for the
Hkdf<Sha512>
type. - Hmac
Sha256 - HMAC-SHA-256: Type alias for the
Hmac<Sha256>
type. - Hmac
Sha512 - HMAC-SHA-512: Type alias for the
Hmac<Sha512>
type. - Sha256t
- Tagged SHA-256: Type alias for the
sha256t::Hash
hash type.